blob: bfd05bbe2b4a314582fcfb8806b2c5f9c68f702b [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/api.h>
Willy 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 Tarreau0a3bd392020-06-04 08:52:38 +020035#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020036#include <haproxy/debug.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020037#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/fd.h>
39#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020040#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020041#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020042#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020043#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020044#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020045#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020046#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020047#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020048#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020049#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020050#include <haproxy/pipe.h>
51#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020052#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010053#include <haproxy/resolvers.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020054#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020055#include <haproxy/session.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020056#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020057#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020058#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020059#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020060#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020061#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020062#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020063#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020064#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020065#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066
William Lallemand74c24fb2016-11-21 17:18:36 +010067
Christopher Faulet2b9b6782019-02-27 16:42:58 +010068/* status codes available for the stats admin page (strictly 4 chars length) */
69const char *stat_status_codes[STAT_STATUS_SIZE] = {
70 [STAT_STATUS_DENY] = "DENY",
71 [STAT_STATUS_DONE] = "DONE",
72 [STAT_STATUS_ERRP] = "ERRP",
73 [STAT_STATUS_EXCD] = "EXCD",
74 [STAT_STATUS_NONE] = "NONE",
75 [STAT_STATUS_PART] = "PART",
76 [STAT_STATUS_UNKN] = "UNKN",
77 [STAT_STATUS_IVAL] = "IVAL",
78};
79
Willy Tarreau0baac8c2016-11-22 16:36:53 +010080/* These are the field names for each INF_* field position. Please pay attention
81 * to always use the exact same name except that the strings for new names must
82 * be lower case or CamelCase while the enum entries must be upper case.
83 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020084const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020085 [INF_NAME] = { .name = "Name", .desc = "Product name" },
86 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
87 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
88 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
89 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
90 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
91 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
92 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
93 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010094 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020095 [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 +010096 [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 +020097 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +010098 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100100 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200101 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
102 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
103 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
104 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
105 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
106 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
107 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
108 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
109 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
110 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
111 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
112 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
113 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
114 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
115 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
116 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
117 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
118 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
119 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
120 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
121 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
122 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
123 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
124 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
125 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
126 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
127 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
128 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
129 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
130 [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" },
131 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
132 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
133 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
134 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
135 [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)" },
136 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
137 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
138 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
139 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
140 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
141 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
142 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
143 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
144 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
145 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
146 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
147 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
148 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
149 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
150 [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 +0200151 [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 +0200152 [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 +0200153 [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 +0200154 [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 +0100155 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100156};
157
Willy Tarreaueaa55372019-10-09 07:39:11 +0200158const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200159 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
160 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100161 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
162 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100163 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
164 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200165 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
166 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
167 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
168 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
169 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
170 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
171 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
172 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
173 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
174 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
175 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
176 [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 +0200177 [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 +0200178 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
179 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
180 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
181 [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" },
182 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
183 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
184 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
185 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
186 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
187 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
188 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
189 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
190 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
191 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
192 [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)" },
193 [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 +0100194 [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 +0200195 [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" },
196 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
197 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
198 [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" },
199 [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" },
200 [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" },
201 [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" },
202 [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" },
203 [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)" },
204 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
205 [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 +0100206 [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 +0200207 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
208 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
209 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
210 [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" },
211 [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" },
212 [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)" },
213 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
214 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
215 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
216 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
217 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
218 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
219 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
220 [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)" },
221 [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" },
222 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
223 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
224 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
225 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
226 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
227 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
228 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
229 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [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" },
233 [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" },
234 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
235 [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" },
236 [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 +0100237 [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 +0200238 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
239 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
240 [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" },
241 [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" },
242 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
243 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
244 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
245 [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" },
246 [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" },
247 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
248 [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 +0100249 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
250 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
251 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
252 [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 +0100253 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200254 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
255 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
256 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200257 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200258 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100259};
260
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100261/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100262THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200263
264/* description of statistics (static and dynamic) */
265static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
266static size_t stat_count[STATS_DOMAIN_COUNT];
267
268/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100269THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100270
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200271/* list of all registered stats module */
272static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
273 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200274 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_DNS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200275};
276
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100277THREAD_LOCAL void *trash_counters;
278
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200279static inline uint8_t stats_get_domain(uint32_t domain)
280{
281 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
282}
283
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200284static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
285{
286 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
287}
288
Christopher Faulet6338a082019-09-09 15:50:54 +0200289static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100290
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200291int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100292{
293 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100294 if (chk->data >= channel_htx_recv_max(chn, htx))
295 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200296 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100297 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100298 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100299 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100300 }
301 else {
302 if (ci_putchk(chn, chk) == -1)
303 return 0;
304 }
305 return 1;
306}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100307
Christopher Fauleted7a0662019-01-14 11:07:34 +0100308static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
309{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200310 struct channel *req = si_oc(si);
311 struct htx *htx = htxbuf(&req->buf);
312 struct htx_blk *blk;
313 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100314
Christopher Fauletb7f88902019-07-15 21:56:43 +0200315 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100316 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200317 ALREADY_CHECKED(blk);
318 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
319 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100320}
321
William Lallemand74c24fb2016-11-21 17:18:36 +0100322/*
323 * http_stats_io_handler()
324 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
325 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100326 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100327 * -> stats_dump_html_head() // emits the HTML headers
328 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
329 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
330 * -> stats_dump_html_px_hdr()
331 * -> stats_dump_fe_stats()
332 * -> stats_dump_li_stats()
333 * -> stats_dump_sv_stats()
334 * -> stats_dump_be_stats()
335 * -> stats_dump_html_px_end()
336 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100337 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100338 */
339
340
William Lallemand74c24fb2016-11-21 17:18:36 +0100341/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
342 * for clearing it if needed.
343 * NOTE: Some tools happen to rely on the field position instead of its name,
344 * so please only append new fields at the end, never in the middle.
345 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200346static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100347{
348 int field;
349
350 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200351 if (stat_f[domain]) {
352 for (field = 0; field < stat_count[domain]; ++field) {
353 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100354
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200355 /* print special delimiter on proxy stats to mark end of
356 static fields */
357 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
358 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200359 }
360 }
361
William Lallemand74c24fb2016-11-21 17:18:36 +0100362 chunk_appendf(&trash, "\n");
363}
364
William Lallemand74c24fb2016-11-21 17:18:36 +0100365/* Emits a stats field without any surrounding element and properly encoded to
366 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
367 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200368int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100369{
370 switch (field_format(f, 0)) {
371 case FF_EMPTY: return 1;
372 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
373 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
374 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
375 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200376 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100377 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
378 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
379 }
380}
381
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200382const char *field_to_html_str(const struct field *f)
383{
384 switch (field_format(f, 0)) {
385 case FF_S32: return U2H(f->u.s32);
386 case FF_S64: return U2H(f->u.s64);
387 case FF_U64: return U2H(f->u.u64);
388 case FF_U32: return U2H(f->u.u32);
389 case FF_STR: return field_str(f, 0);
390 case FF_EMPTY:
391 default:
392 return "";
393 }
394}
395
William Lallemand74c24fb2016-11-21 17:18:36 +0100396/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
397 * output is supposed to be used on its own line. Returns non-zero on success, 0
398 * if the buffer is full.
399 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200400int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100401{
402 switch (field_format(f, 0)) {
403 case FF_EMPTY: return 1;
404 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
405 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
406 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
407 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200408 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100409 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
410 default: return chunk_appendf(out, "%08x:?", f->type);
411 }
412}
413
Simon Horman05ee2132017-01-04 09:37:25 +0100414/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
415 * the recommendation for interoperable integers in section 6 of RFC 7159.
416 */
417#define JSON_INT_MAX ((1ULL << 53) - 1)
418#define JSON_INT_MIN (0 - JSON_INT_MAX)
419
420/* Emits a stats field value and its type in JSON.
421 * Returns non-zero on success, 0 on error.
422 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200423int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100424{
425 int old_len;
426 char buf[20];
427 const char *type, *value = buf, *quote = "";
428
429 switch (field_format(f, 0)) {
430 case FF_EMPTY: return 1;
431 case FF_S32: type = "\"s32\"";
432 snprintf(buf, sizeof(buf), "%d", f->u.s32);
433 break;
434 case FF_U32: type = "\"u32\"";
435 snprintf(buf, sizeof(buf), "%u", f->u.u32);
436 break;
437 case FF_S64: type = "\"s64\"";
438 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
439 return 0;
440 type = "\"s64\"";
441 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
442 break;
443 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
444 return 0;
445 type = "\"u64\"";
446 snprintf(buf, sizeof(buf), "%llu",
447 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200448 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200449 case FF_FLT: type = "\"flt\"";
450 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100451 break;
452 case FF_STR: type = "\"str\"";
453 value = field_str(f, 0);
454 quote = "\"";
455 break;
456 default: snprintf(buf, sizeof(buf), "%u", f->type);
457 type = buf;
458 value = "unknown";
459 quote = "\"";
460 break;
461 }
462
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200463 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100464 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
465 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100467}
468
William Lallemand74c24fb2016-11-21 17:18:36 +0100469/* Emits an encoding of the field type on 3 characters followed by a delimiter.
470 * Returns non-zero on success, 0 if the buffer is full.
471 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200472int stats_emit_field_tags(struct buffer *out, const struct field *f,
473 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100474{
475 char origin, nature, scope;
476
477 switch (field_origin(f, 0)) {
478 case FO_METRIC: origin = 'M'; break;
479 case FO_STATUS: origin = 'S'; break;
480 case FO_KEY: origin = 'K'; break;
481 case FO_CONFIG: origin = 'C'; break;
482 case FO_PRODUCT: origin = 'P'; break;
483 default: origin = '?'; break;
484 }
485
486 switch (field_nature(f, 0)) {
487 case FN_GAUGE: nature = 'G'; break;
488 case FN_LIMIT: nature = 'L'; break;
489 case FN_MIN: nature = 'm'; break;
490 case FN_MAX: nature = 'M'; break;
491 case FN_RATE: nature = 'R'; break;
492 case FN_COUNTER: nature = 'C'; break;
493 case FN_DURATION: nature = 'D'; break;
494 case FN_AGE: nature = 'A'; break;
495 case FN_TIME: nature = 'T'; break;
496 case FN_NAME: nature = 'N'; break;
497 case FN_OUTPUT: nature = 'O'; break;
498 case FN_AVG: nature = 'a'; break;
499 default: nature = '?'; break;
500 }
501
502 switch (field_scope(f, 0)) {
503 case FS_PROCESS: scope = 'P'; break;
504 case FS_SERVICE: scope = 'S'; break;
505 case FS_SYSTEM: scope = 's'; break;
506 case FS_CLUSTER: scope = 'C'; break;
507 default: scope = '?'; break;
508 }
509
510 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
511}
512
Simon Horman05ee2132017-01-04 09:37:25 +0100513/* Emits an encoding of the field type as JSON.
514 * Returns non-zero on success, 0 if the buffer is full.
515 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200516int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100517{
518 const char *origin, *nature, *scope;
519 int old_len;
520
521 switch (field_origin(f, 0)) {
522 case FO_METRIC: origin = "Metric"; break;
523 case FO_STATUS: origin = "Status"; break;
524 case FO_KEY: origin = "Key"; break;
525 case FO_CONFIG: origin = "Config"; break;
526 case FO_PRODUCT: origin = "Product"; break;
527 default: origin = "Unknown"; break;
528 }
529
530 switch (field_nature(f, 0)) {
531 case FN_GAUGE: nature = "Gauge"; break;
532 case FN_LIMIT: nature = "Limit"; break;
533 case FN_MIN: nature = "Min"; break;
534 case FN_MAX: nature = "Max"; break;
535 case FN_RATE: nature = "Rate"; break;
536 case FN_COUNTER: nature = "Counter"; break;
537 case FN_DURATION: nature = "Duration"; break;
538 case FN_AGE: nature = "Age"; break;
539 case FN_TIME: nature = "Time"; break;
540 case FN_NAME: nature = "Name"; break;
541 case FN_OUTPUT: nature = "Output"; break;
542 case FN_AVG: nature = "Avg"; break;
543 default: nature = "Unknown"; break;
544 }
545
546 switch (field_scope(f, 0)) {
547 case FS_PROCESS: scope = "Process"; break;
548 case FS_SERVICE: scope = "Service"; break;
549 case FS_SYSTEM: scope = "System"; break;
550 case FS_CLUSTER: scope = "Cluster"; break;
551 default: scope = "Unknown"; break;
552 }
553
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200554 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100555 chunk_appendf(out, "\"tags\":{"
556 "\"origin\":\"%s\","
557 "\"nature\":\"%s\","
558 "\"scope\":\"%s\""
559 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200560 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100561}
William Lallemand74c24fb2016-11-21 17:18:36 +0100562
563/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200564static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200565 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200566 unsigned int flags,
567 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100568{
569 int field;
570
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200571 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100572 if (!stats_emit_raw_data_field(out, &stats[field]))
573 return 0;
574 if (!chunk_strcat(out, ","))
575 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200576
577 /* print special delimiter on proxy stats to mark end of
578 static fields */
579 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
580 if (!chunk_strcat(out, "-,"))
581 return 0;
582 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100583 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200584
William Lallemand74c24fb2016-11-21 17:18:36 +0100585 chunk_strcat(out, "\n");
586 return 1;
587}
588
589/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200590static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200591 const struct field *stats,
592 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200593 unsigned int flags,
594 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100595{
596 int field;
597
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200598 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100599 if (!stats[field].type)
600 continue;
601
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200602 switch (domain) {
603 case STATS_DOMAIN_PROXY:
604 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
605 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
606 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
607 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
608 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
609 '?',
610 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
611 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200612 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200613 stats[ST_F_PID].u.u32);
614 break;
615
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200616 case STATS_DOMAIN_DNS:
617 chunk_appendf(out, "D.%d.%s:", field,
618 stat_f[domain][field].name);
619 break;
620
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200621 default:
622 break;
623 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100624
625 if (!stats_emit_field_tags(out, &stats[field], ':'))
626 return 0;
627 if (!stats_emit_typed_data_field(out, &stats[field]))
628 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200629
630 if (flags & STAT_SHOW_FDESC
631 && !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].name)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200632 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200633 }
634
William Lallemand74c24fb2016-11-21 17:18:36 +0100635 if (!chunk_strcat(out, "\n"))
636 return 0;
637 }
638 return 1;
639}
640
Simon Horman05ee2132017-01-04 09:37:25 +0100641/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200642static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200643 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100644{
645 int field;
646 int started = 0;
647
648 if (!chunk_strcat(out, "["))
649 return 0;
650
651 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
652 int old_len;
653
654 if (!field_format(info, field))
655 continue;
656
657 if (started && !chunk_strcat(out, ","))
658 goto err;
659 started = 1;
660
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200661 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100662 chunk_appendf(out,
663 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
664 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200665 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100666 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200667 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100668 goto err;
669
670 if (!stats_emit_json_field_tags(out, &info[field]))
671 goto err;
672
673 if (!stats_emit_json_data_field(out, &info[field]))
674 goto err;
675
676 if (!chunk_strcat(out, "}"))
677 goto err;
678 }
679
680 if (!chunk_strcat(out, "]"))
681 goto err;
682 return 1;
683
684err:
685 chunk_reset(out);
686 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
687 return 0;
688}
689
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200690static void stats_print_proxy_field_json(struct buffer *out,
691 const struct field *stat,
692 const char *name,
693 int pos,
694 uint32_t field_type,
695 uint32_t iid,
696 uint32_t sid,
697 uint32_t pid)
698{
699 const char *obj_type;
700 switch (field_type) {
701 case STATS_TYPE_FE: obj_type = "Frontend"; break;
702 case STATS_TYPE_BE: obj_type = "Backend"; break;
703 case STATS_TYPE_SO: obj_type = "Listener"; break;
704 case STATS_TYPE_SV: obj_type = "Server"; break;
705 default: obj_type = "Unknown"; break;
706 }
707
708 chunk_appendf(out,
709 "{"
710 "\"objType\":\"%s\","
711 "\"proxyId\":%u,"
712 "\"id\":%u,"
713 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
714 "\"processNum\":%u,",
715 obj_type, iid, sid, pos, name, pid);
716}
717
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200718static void stats_print_dns_field_json(struct buffer *out,
719 const struct field *stat,
720 const char *name,
721 int pos)
722{
723 chunk_appendf(out,
724 "{"
725 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
726 pos, name);
727}
728
729
Simon Horman05ee2132017-01-04 09:37:25 +0100730/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200731static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200732 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200733 unsigned int flags,
734 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100735{
736 int field;
737 int started = 0;
738
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200739 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100740 return 0;
741 if (!chunk_strcat(out, "["))
742 return 0;
743
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200744 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100745 int old_len;
746
747 if (!stats[field].type)
748 continue;
749
750 if (started && !chunk_strcat(out, ","))
751 goto err;
752 started = 1;
753
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200754 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200755 if (domain == STATS_DOMAIN_PROXY) {
756 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200757 stat_f[domain][field].name,
758 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200759 stats[ST_F_TYPE].u.u32,
760 stats[ST_F_IID].u.u32,
761 stats[ST_F_SID].u.u32,
762 stats[ST_F_PID].u.u32);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200763 } else if (domain == STATS_DOMAIN_DNS) {
764 stats_print_dns_field_json(out, &stats[field],
765 stat_f[domain][field].name,
766 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200767 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200768
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100770 goto err;
771
772 if (!stats_emit_json_field_tags(out, &stats[field]))
773 goto err;
774
775 if (!stats_emit_json_data_field(out, &stats[field]))
776 goto err;
777
778 if (!chunk_strcat(out, "}"))
779 goto err;
780 }
781
782 if (!chunk_strcat(out, "]"))
783 goto err;
784
785 return 1;
786
787err:
788 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200789 if (flags & STAT_STARTED)
790 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100791 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
792 return 0;
793}
794
William Lallemand74c24fb2016-11-21 17:18:36 +0100795/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200796 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200797 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
798 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
799 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100800 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200801static int stats_dump_fields_html(struct buffer *out,
802 const struct field *stats,
803 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100804{
Willy Tarreau83061a82018-07-13 11:56:34 +0200805 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200806 struct stats_module *mod;
807 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100808
809 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
810 chunk_appendf(out,
811 /* name, queue */
812 "<tr class=\"frontend\">");
813
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200814 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100815 /* Column sub-heading for Enable or Disable server */
816 chunk_appendf(out, "<td></td>");
817 }
818
819 chunk_appendf(out,
820 "<td class=ac>"
821 "<a name=\"%s/Frontend\"></a>"
822 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
823 "<td colspan=3></td>"
824 "",
825 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
826
827 chunk_appendf(out,
828 /* sessions rate : current */
829 "<td><u>%s<div class=tips><table class=det>"
830 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
831 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
832 "",
833 U2H(stats[ST_F_RATE].u.u32),
834 U2H(stats[ST_F_CONN_RATE].u.u32),
835 U2H(stats[ST_F_RATE].u.u32));
836
837 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
838 chunk_appendf(out,
839 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
840 U2H(stats[ST_F_REQ_RATE].u.u32));
841
842 chunk_appendf(out,
843 "</table></div></u></td>"
844 /* sessions rate : max */
845 "<td><u>%s<div class=tips><table class=det>"
846 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
847 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
848 "",
849 U2H(stats[ST_F_RATE_MAX].u.u32),
850 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
851 U2H(stats[ST_F_RATE_MAX].u.u32));
852
853 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
854 chunk_appendf(out,
855 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
856 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
857
858 chunk_appendf(out,
859 "</table></div></u></td>"
860 /* sessions rate : limit */
861 "<td>%s</td>",
862 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
863
864 chunk_appendf(out,
865 /* sessions: current, max, limit, total */
866 "<td>%s</td><td>%s</td><td>%s</td>"
867 "<td><u>%s<div class=tips><table class=det>"
868 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
869 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
870 "",
871 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
872 U2H(stats[ST_F_STOT].u.u64),
873 U2H(stats[ST_F_CONN_TOT].u.u64),
874 U2H(stats[ST_F_STOT].u.u64));
875
876 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
877 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
878 chunk_appendf(out,
879 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
880 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
881 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
882 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
883 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
884 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
885 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
886 "<tr><th>- other responses:</th><td>%s</td></tr>"
887 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100888 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
889 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200890 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100891 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100892 "",
893 U2H(stats[ST_F_REQ_TOT].u.u64),
894 U2H(stats[ST_F_HRSP_1XX].u.u64),
895 U2H(stats[ST_F_HRSP_2XX].u.u64),
896 U2H(stats[ST_F_COMP_RSP].u.u64),
897 stats[ST_F_HRSP_2XX].u.u64 ?
898 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
899 U2H(stats[ST_F_HRSP_3XX].u.u64),
900 U2H(stats[ST_F_HRSP_4XX].u.u64),
901 U2H(stats[ST_F_HRSP_5XX].u.u64),
902 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200903 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100904 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
905 U2H(stats[ST_F_CACHE_HITS].u.u64),
906 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
907 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100908 U2H(stats[ST_F_WREW].u.u64),
909 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100910 }
911
912 chunk_appendf(out,
913 "</table></div></u></td>"
914 /* sessions: lbtot, lastsess */
915 "<td></td><td></td>"
916 /* bytes : in */
917 "<td>%s</td>"
918 "",
919 U2H(stats[ST_F_BIN].u.u64));
920
921 chunk_appendf(out,
922 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
923 "<td>%s%s<div class=tips><table class=det>"
924 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
925 "<tr><th>Compression in:</th><td>%s</td></tr>"
926 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
927 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
928 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
929 "</table></div>%s</td>",
930 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
931 U2H(stats[ST_F_BOUT].u.u64),
932 U2H(stats[ST_F_BOUT].u.u64),
933 U2H(stats[ST_F_COMP_IN].u.u64),
934 U2H(stats[ST_F_COMP_OUT].u.u64),
935 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
936 U2H(stats[ST_F_COMP_BYP].u.u64),
937 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
938 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,
939 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
940
941 chunk_appendf(out,
942 /* denied: req, resp */
943 "<td>%s</td><td>%s</td>"
944 /* errors : request, connect, response */
945 "<td>%s</td><td></td><td></td>"
946 /* warnings: retries, redispatches */
947 "<td></td><td></td>"
948 /* server status : reflect frontend status */
949 "<td class=ac>%s</td>"
950 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200951 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100952 "",
953 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
954 U2H(stats[ST_F_EREQ].u.u64),
955 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200956
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200957 if (flags & STAT_SHMODULES) {
958 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100959 chunk_appendf(out, "<td>");
960
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200961 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100962 chunk_appendf(out,
963 "<u>%s<div class=tips><table class=det>",
964 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200965 for (j = 0; j < mod->stats_count; ++j) {
966 chunk_appendf(out,
967 "<tr><th>%s</th><td>%s</td></tr>",
968 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
969 ++i;
970 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100971 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200972 } else {
973 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200974 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200975
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100976 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200977 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200978 }
979
980 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100981 }
982 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
983 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200984 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100985 /* Column sub-heading for Enable or Disable server */
986 chunk_appendf(out, "<td></td>");
987 }
988
989 chunk_appendf(out,
990 /* frontend name, listener name */
991 "<td class=ac><a name=\"%s/+%s\"></a>%s"
992 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
993 "",
994 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200995 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100996 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
997
Willy Tarreau708c4162019-10-09 10:19:16 +0200998 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100999 chunk_appendf(out, "<div class=tips>");
1000
Willy Tarreau90807112020-02-25 08:16:33 +01001001 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001002 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1003 else if (*field_str(stats, ST_F_ADDR) == '[')
1004 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1005 else if (*field_str(stats, ST_F_ADDR))
1006 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1007
1008 /* id */
1009 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1010 }
1011
1012 chunk_appendf(out,
1013 /* queue */
1014 "%s</td><td colspan=3></td>"
1015 /* sessions rate: current, max, limit */
1016 "<td colspan=3>&nbsp;</td>"
1017 /* sessions: current, max, limit, total, lbtot, lastsess */
1018 "<td>%s</td><td>%s</td><td>%s</td>"
1019 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1020 /* bytes: in, out */
1021 "<td>%s</td><td>%s</td>"
1022 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001023 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001024 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1025 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1026
1027 chunk_appendf(out,
1028 /* denied: req, resp */
1029 "<td>%s</td><td>%s</td>"
1030 /* errors: request, connect, response */
1031 "<td>%s</td><td></td><td></td>"
1032 /* warnings: retries, redispatches */
1033 "<td></td><td></td>"
1034 /* server status: reflect listener status */
1035 "<td class=ac>%s</td>"
1036 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001037 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001038 "",
1039 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1040 U2H(stats[ST_F_EREQ].u.u64),
1041 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001042
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001043 if (flags & STAT_SHMODULES) {
1044 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001045 chunk_appendf(out, "<td>");
1046
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001047 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001048 chunk_appendf(out,
1049 "<u>%s<div class=tips><table class=det>",
1050 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001051 for (j = 0; j < mod->stats_count; ++j) {
1052 chunk_appendf(out,
1053 "<tr><th>%s</th><td>%s</td></tr>",
1054 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1055 ++i;
1056 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001057 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001058 } else {
1059 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001060 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001061
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001062 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001063 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001064 }
1065
1066 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001067 }
1068 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1069 const char *style;
1070
1071 /* determine the style to use depending on the server's state,
1072 * its health and weight. There isn't a 1-to-1 mapping between
1073 * state and styles for the cases where the server is (still)
1074 * up. The reason is that we don't want to report nolb and
1075 * drain with the same color.
1076 */
1077
1078 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1079 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1080 style = "down";
1081 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001082 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001083 style = "going_up";
1084 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001085 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1086 style = "draining";
1087 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001088 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001089 style = "going_down";
1090 }
1091 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1092 style = "nolb";
1093 }
1094 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1095 style = "no_check";
1096 }
1097 else if (!stats[ST_F_CHKFAIL].type ||
1098 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1099 /* no check or max health = UP */
1100 if (stats[ST_F_WEIGHT].u.u32)
1101 style = "up";
1102 else
1103 style = "draining";
1104 }
1105 else {
1106 style = "going_down";
1107 }
1108
Willy Tarreau7b524852020-08-11 10:26:36 +02001109 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001110 chunk_appendf(out, "<tr class=\"maintain\">");
1111 else
1112 chunk_appendf(out,
1113 "<tr class=\"%s_%s\">",
1114 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1115
1116
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001117 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001118 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001119 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1120 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001121 field_str(stats, ST_F_SVNAME));
1122
1123 chunk_appendf(out,
1124 "<td class=ac><a name=\"%s/%s\"></a>%s"
1125 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1126 "",
1127 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001128 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001129 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1130
Willy Tarreau708c4162019-10-09 10:19:16 +02001131 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001132 chunk_appendf(out, "<div class=tips>");
1133
Willy Tarreau90807112020-02-25 08:16:33 +01001134 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001135 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1136 else if (*field_str(stats, ST_F_ADDR) == '[')
1137 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1138 else if (*field_str(stats, ST_F_ADDR))
1139 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1140
1141 /* id */
1142 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1143
1144 /* cookie */
1145 if (stats[ST_F_COOKIE].type) {
1146 chunk_appendf(out, ", cookie: '");
1147 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1148 chunk_htmlencode(out, &src);
1149 chunk_appendf(out, "'");
1150 }
1151
1152 chunk_appendf(out, "</div>");
1153 }
1154
1155 chunk_appendf(out,
1156 /* queue : current, max, limit */
1157 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1158 /* sessions rate : current, max, limit */
1159 "<td>%s</td><td>%s</td><td></td>"
1160 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001161 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001162 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1163 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1164
1165 chunk_appendf(out,
1166 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001167 "<td><u>%s<div class=tips>"
1168 "<table class=det>"
1169 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001170 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001171 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001172 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1173 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001174 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001175 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1176 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1177 "</table></div></u>"
1178 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001179 "<td><u>%s<div class=tips><table class=det>"
1180 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1181 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001182 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001183 U2H(stats[ST_F_SCUR].u.u32),
1184 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1185 U2H(stats[ST_F_SRV_ICUR].u.u32),
1186 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1187 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001188 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001189
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001190 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1191 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1192 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001193 U2H(stats[ST_F_STOT].u.u64),
1194 U2H(stats[ST_F_STOT].u.u64));
1195
1196 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1197 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001198 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001199 "<tr><th>New connections:</th><td>%s</td></tr>"
1200 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001201 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001202 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1203 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1204 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1205 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1206 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1207 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001208 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001209 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001210 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001211 U2H(stats[ST_F_CONNECT].u.u64),
1212 U2H(stats[ST_F_REUSE].u.u64),
1213 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1214 (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 +02001215 U2H(stats[ST_F_REQ_TOT].u.u64),
1216 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1217 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1218 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1219 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1220 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1221 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1222 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1223 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1224 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1225 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1226 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1227 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001228 U2H(stats[ST_F_WREW].u.u64),
1229 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001230 }
1231
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001232 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1233 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1234 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1235 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1236 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001237 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001238 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1239 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1240 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1241 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001242
1243 chunk_appendf(out,
1244 "</table></div></u></td>"
1245 /* sessions: lbtot, last */
1246 "<td>%s</td><td>%s</td>",
1247 U2H(stats[ST_F_LBTOT].u.u64),
1248 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1249
1250 chunk_appendf(out,
1251 /* bytes : in, out */
1252 "<td>%s</td><td>%s</td>"
1253 /* denied: req, resp */
1254 "<td></td><td>%s</td>"
1255 /* errors : request, connect */
1256 "<td></td><td>%s</td>"
1257 /* errors : response */
1258 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1259 /* warnings: retries, redispatches */
1260 "<td>%lld</td><td>%lld</td>"
1261 "",
1262 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1263 U2H(stats[ST_F_DRESP].u.u64),
1264 U2H(stats[ST_F_ECON].u.u64),
1265 U2H(stats[ST_F_ERESP].u.u64),
1266 (long long)stats[ST_F_CLI_ABRT].u.u64,
1267 (long long)stats[ST_F_SRV_ABRT].u.u64,
1268 (long long)stats[ST_F_WRETR].u.u64,
1269 (long long)stats[ST_F_WREDIS].u.u64);
1270
1271 /* status, last change */
1272 chunk_appendf(out, "<td class=ac>");
1273
1274 /* FIXME!!!!
1275 * LASTCHG should contain the last change for *this* server and must be computed
1276 * properly above, as was done below, ie: this server if maint, otherwise ref server
1277 * if tracking. Note that ref is either local or remote depending on tracking.
1278 */
1279
1280
Willy Tarreau7b524852020-08-11 10:26:36 +02001281 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001282 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1283 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001284 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001285 chunk_strcat(out, "<i>no check</i>");
1286 }
1287 else {
1288 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 +02001289 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001290 if (stats[ST_F_CHECK_HEALTH].u.u32)
1291 chunk_strcat(out, " &uarr;");
1292 }
1293 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1294 chunk_strcat(out, " &darr;");
1295 }
1296
Willy Tarreau7b524852020-08-11 10:26:36 +02001297 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001298 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1299 chunk_appendf(out,
1300 "</td><td class=ac><u> %s",
1301 field_str(stats, ST_F_AGENT_STATUS));
1302
1303 if (stats[ST_F_AGENT_CODE].type)
1304 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1305
1306 if (stats[ST_F_AGENT_DURATION].type)
1307 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1308
1309 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1310
1311 if (*field_str(stats, ST_F_LAST_AGT)) {
1312 chunk_appendf(out, ": ");
1313 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1314 chunk_htmlencode(out, &src);
1315 }
1316 chunk_appendf(out, "</div></u>");
1317 }
1318 else if (stats[ST_F_CHECK_STATUS].type) {
1319 chunk_appendf(out,
1320 "</td><td class=ac><u> %s",
1321 field_str(stats, ST_F_CHECK_STATUS));
1322
1323 if (stats[ST_F_CHECK_CODE].type)
1324 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1325
1326 if (stats[ST_F_CHECK_DURATION].type)
1327 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1328
1329 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1330
1331 if (*field_str(stats, ST_F_LAST_CHK)) {
1332 chunk_appendf(out, ": ");
1333 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1334 chunk_htmlencode(out, &src);
1335 }
1336 chunk_appendf(out, "</div></u>");
1337 }
1338 else
1339 chunk_appendf(out, "</td><td>");
1340
1341 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001342 /* weight / uweight */
1343 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001344 /* act, bck */
1345 "<td class=ac>%s</td><td class=ac>%s</td>"
1346 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001347 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001348 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1349 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1350
1351 /* check failures: unique, fatal, down time */
1352 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1353 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1354 }
1355 else if (stats[ST_F_CHKFAIL].type) {
1356 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1357
1358 if (stats[ST_F_HANAFAIL].type)
1359 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1360
1361 chunk_appendf(out,
1362 "<div class=tips>Failed Health Checks%s</div></u></td>"
1363 "<td>%lld</td><td>%s</td>"
1364 "",
1365 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1366 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1367 }
1368 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1369 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1370 chunk_appendf(out,
1371 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1372 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1373 }
1374 else
1375 chunk_appendf(out, "<td colspan=3></td>");
1376
1377 /* throttle */
1378 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001379 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001380 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001381 chunk_appendf(out, "<td class=ac>-</td>");
1382
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001383 if (flags & STAT_SHMODULES) {
1384 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001385 chunk_appendf(out, "<td>");
1386
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001387 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001388 chunk_appendf(out,
1389 "<u>%s<div class=tips><table class=det>",
1390 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001391 for (j = 0; j < mod->stats_count; ++j) {
1392 chunk_appendf(out,
1393 "<tr><th>%s</th><td>%s</td></tr>",
1394 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1395 ++i;
1396 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001397 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001398 } else {
1399 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001400 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001401
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001402 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001403 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001404 }
1405
1406 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001407 }
1408 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1409 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001410 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001411 /* Column sub-heading for Enable or Disable server */
1412 chunk_appendf(out, "<td></td>");
1413 }
1414 chunk_appendf(out,
1415 "<td class=ac>"
1416 /* name */
1417 "%s<a name=\"%s/Backend\"></a>"
1418 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1419 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001420 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001421 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1422
Willy Tarreau708c4162019-10-09 10:19:16 +02001423 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001424 /* balancing */
1425 chunk_appendf(out, "<div class=tips>balancing: %s",
1426 field_str(stats, ST_F_ALGO));
1427
1428 /* cookie */
1429 if (stats[ST_F_COOKIE].type) {
1430 chunk_appendf(out, ", cookie: '");
1431 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1432 chunk_htmlencode(out, &src);
1433 chunk_appendf(out, "'");
1434 }
1435 chunk_appendf(out, "</div>");
1436 }
1437
1438 chunk_appendf(out,
1439 "%s</td>"
1440 /* queue : current, max */
1441 "<td>%s</td><td>%s</td><td></td>"
1442 /* sessions rate : current, max, limit */
1443 "<td>%s</td><td>%s</td><td></td>"
1444 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001445 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001446 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1447 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1448
1449 chunk_appendf(out,
1450 /* sessions: current, max, limit, total */
1451 "<td>%s</td><td>%s</td><td>%s</td>"
1452 "<td><u>%s<div class=tips><table class=det>"
1453 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1454 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001455 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 +01001456 U2H(stats[ST_F_STOT].u.u64),
1457 U2H(stats[ST_F_STOT].u.u64));
1458
1459 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1460 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1461 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001462 "<tr><th>New connections:</th><td>%s</td></tr>"
1463 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001464 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1465 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1466 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1467 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1468 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1469 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1470 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1471 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001472 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1473 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001474 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001475 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001476 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001477 U2H(stats[ST_F_CONNECT].u.u64),
1478 U2H(stats[ST_F_REUSE].u.u64),
1479 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1480 (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 +01001481 U2H(stats[ST_F_REQ_TOT].u.u64),
1482 U2H(stats[ST_F_HRSP_1XX].u.u64),
1483 U2H(stats[ST_F_HRSP_2XX].u.u64),
1484 U2H(stats[ST_F_COMP_RSP].u.u64),
1485 stats[ST_F_HRSP_2XX].u.u64 ?
1486 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1487 U2H(stats[ST_F_HRSP_3XX].u.u64),
1488 U2H(stats[ST_F_HRSP_4XX].u.u64),
1489 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001490 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001491 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1492 U2H(stats[ST_F_CACHE_HITS].u.u64),
1493 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1494 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001495 U2H(stats[ST_F_WREW].u.u64),
1496 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001497 }
1498
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001499 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1500 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1501 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1502 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1503 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001504 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001505 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1506 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1507 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1508 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001509
1510 chunk_appendf(out,
1511 "</table></div></u></td>"
1512 /* sessions: lbtot, last */
1513 "<td>%s</td><td>%s</td>"
1514 /* bytes: in */
1515 "<td>%s</td>"
1516 "",
1517 U2H(stats[ST_F_LBTOT].u.u64),
1518 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1519 U2H(stats[ST_F_BIN].u.u64));
1520
1521 chunk_appendf(out,
1522 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1523 "<td>%s%s<div class=tips><table class=det>"
1524 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1525 "<tr><th>Compression in:</th><td>%s</td></tr>"
1526 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1527 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1528 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1529 "</table></div>%s</td>",
1530 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1531 U2H(stats[ST_F_BOUT].u.u64),
1532 U2H(stats[ST_F_BOUT].u.u64),
1533 U2H(stats[ST_F_COMP_IN].u.u64),
1534 U2H(stats[ST_F_COMP_OUT].u.u64),
1535 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1536 U2H(stats[ST_F_COMP_BYP].u.u64),
1537 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1538 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,
1539 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1540
1541 chunk_appendf(out,
1542 /* denied: req, resp */
1543 "<td>%s</td><td>%s</td>"
1544 /* errors : request, connect */
1545 "<td></td><td>%s</td>"
1546 /* errors : response */
1547 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1548 /* warnings: retries, redispatches */
1549 "<td>%lld</td><td>%lld</td>"
1550 /* backend status: reflect backend status (up/down): we display UP
1551 * if the backend has known working servers or if it has no server at
1552 * all (eg: for stats). Then we display the total weight, number of
1553 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001554 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001555 "<td class=ac>%d</td><td class=ac>%d</td>"
1556 "",
1557 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1558 U2H(stats[ST_F_ECON].u.u64),
1559 U2H(stats[ST_F_ERESP].u.u64),
1560 (long long)stats[ST_F_CLI_ABRT].u.u64,
1561 (long long)stats[ST_F_SRV_ABRT].u.u64,
1562 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1563 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1564 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 +02001565 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001566 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1567
1568 chunk_appendf(out,
1569 /* rest of backend: nothing, down transitions, total downtime, throttle */
1570 "<td class=ac>&nbsp;</td><td>%d</td>"
1571 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001572 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001573 stats[ST_F_CHKDOWN].u.u32,
1574 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001575
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001576 if (flags & STAT_SHMODULES) {
1577 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001578 chunk_appendf(out, "<td>");
1579
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001580 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001581 chunk_appendf(out,
1582 "<u>%s<div class=tips><table class=det>",
1583 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001584 for (j = 0; j < mod->stats_count; ++j) {
1585 chunk_appendf(out,
1586 "<tr><th>%s</th><td>%s</td></tr>",
1587 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1588 ++i;
1589 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001590 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001591 } else {
1592 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001593 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001594
1595 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001596 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001597 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001598
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001599 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001600 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001601
William Lallemand74c24fb2016-11-21 17:18:36 +01001602 return 1;
1603}
1604
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001605int stats_dump_one_line(const struct field *stats, size_t stats_count,
1606 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001607{
Simon Horman05ee2132017-01-04 09:37:25 +01001608 int ret;
1609
William Lallemand74c24fb2016-11-21 17:18:36 +01001610 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001611 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001612 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001613 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001614 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001615 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001616 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001617 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001618
1619 if (ret)
1620 appctx->ctx.stats.flags |= STAT_STARTED;
1621
1622 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001623}
1624
William Dauchy0ef54392021-01-17 18:27:45 +01001625/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001626 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1627 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1628 * this value, or if the selected field is not implemented for frontends, the
1629 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001630 */
William Dauchy0ef54392021-01-17 18:27:45 +01001631int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1632 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001633{
William Dauchy0ef54392021-01-17 18:27:45 +01001634 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001635
William Lallemand74c24fb2016-11-21 17:18:36 +01001636 if (len < ST_F_TOTAL_FIELDS)
1637 return 0;
1638
William Dauchy0ef54392021-01-17 18:27:45 +01001639 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001640 struct field metric = { 0 };
1641
William Dauchy0ef54392021-01-17 18:27:45 +01001642 switch (current_field) {
1643 case ST_F_PXNAME:
1644 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1645 break;
1646 case ST_F_SVNAME:
1647 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1648 break;
1649 case ST_F_MODE:
1650 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1651 break;
1652 case ST_F_SCUR:
1653 metric = mkf_u32(0, px->feconn);
1654 break;
1655 case ST_F_SMAX:
1656 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1657 break;
1658 case ST_F_SLIM:
1659 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1660 break;
1661 case ST_F_STOT:
1662 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1663 break;
1664 case ST_F_BIN:
1665 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1666 break;
1667 case ST_F_BOUT:
1668 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1669 break;
1670 case ST_F_DREQ:
1671 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1672 break;
1673 case ST_F_DRESP:
1674 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1675 break;
1676 case ST_F_EREQ:
1677 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1678 break;
1679 case ST_F_DCON:
1680 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1681 break;
1682 case ST_F_DSES:
1683 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1684 break;
1685 case ST_F_STATUS:
1686 metric = mkf_str(FO_STATUS, px->disabled ? "STOP" : "OPEN");
1687 break;
1688 case ST_F_PID:
1689 metric = mkf_u32(FO_KEY, relative_pid);
1690 break;
1691 case ST_F_IID:
1692 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1693 break;
1694 case ST_F_SID:
1695 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1696 break;
1697 case ST_F_TYPE:
1698 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1699 break;
1700 case ST_F_RATE:
1701 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1702 break;
1703 case ST_F_RATE_LIM:
1704 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1705 break;
1706 case ST_F_RATE_MAX:
1707 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1708 break;
1709 case ST_F_WREW:
1710 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1711 break;
1712 case ST_F_EINT:
1713 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1714 break;
1715 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001716 if (px->mode == PR_MODE_HTTP)
1717 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001718 break;
1719 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001720 if (px->mode == PR_MODE_HTTP)
1721 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001722 break;
1723 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001724 if (px->mode == PR_MODE_HTTP)
1725 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001726 break;
1727 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001728 if (px->mode == PR_MODE_HTTP)
1729 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001730 break;
1731 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001732 if (px->mode == PR_MODE_HTTP)
1733 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001734 break;
1735 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001736 if (px->mode == PR_MODE_HTTP)
1737 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001738 break;
1739 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001740 if (px->mode == PR_MODE_HTTP)
1741 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001742 break;
1743 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001744 if (px->mode == PR_MODE_HTTP)
1745 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001746 break;
1747 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001748 if (px->mode == PR_MODE_HTTP)
1749 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001750 break;
1751 case ST_F_REQ_RATE:
1752 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1753 break;
1754 case ST_F_REQ_RATE_MAX:
1755 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1756 break;
1757 case ST_F_REQ_TOT:
1758 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1759 break;
1760 case ST_F_COMP_IN:
1761 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1762 break;
1763 case ST_F_COMP_OUT:
1764 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1765 break;
1766 case ST_F_COMP_BYP:
1767 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1768 break;
1769 case ST_F_COMP_RSP:
1770 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1771 break;
1772 case ST_F_CONN_RATE:
1773 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1774 break;
1775 case ST_F_CONN_RATE_MAX:
1776 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1777 break;
1778 case ST_F_CONN_TOT:
1779 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1780 break;
1781 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001782 /* not used for frontends. If a specific metric
1783 * is requested, return an error. Otherwise continue.
1784 */
1785 if (selected_field != NULL)
1786 return 0;
1787 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001788 }
1789 stats[current_field] = metric;
1790 if (selected_field != NULL)
1791 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001792 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001793 return 1;
1794}
1795
1796/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1797 * the state from stream interface <si>. The caller is responsible for clearing
1798 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1799 */
1800static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1801{
1802 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001803 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1804 struct stats_module *mod;
1805 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001806
1807 if (!(px->cap & PR_CAP_FE))
1808 return 0;
1809
1810 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1811 return 0;
1812
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001813 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1814
William Dauchy0ef54392021-01-17 18:27:45 +01001815 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001816 return 0;
1817
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001818 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1819 void *counters;
1820
1821 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1822 stats_count += mod->stats_count;
1823 continue;
1824 }
1825
1826 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1827 mod->fill_stats(counters, stats + stats_count);
1828 stats_count += mod->stats_count;
1829 }
1830
1831 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001832}
1833
William Dauchy655e14e2021-02-14 23:22:54 +01001834/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1835 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1836 * this length is less then this value, the function returns 0, otherwise, it
1837 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1838 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001839 */
1840int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001841 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001842{
William Dauchy655e14e2021-02-14 23:22:54 +01001843 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001844 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001845
1846 if (len < ST_F_TOTAL_FIELDS)
1847 return 0;
1848
1849 if (!l->counters)
1850 return 0;
1851
1852 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001853
William Dauchy655e14e2021-02-14 23:22:54 +01001854 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1855 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001856
William Dauchy655e14e2021-02-14 23:22:54 +01001857 switch (current_field) {
1858 case ST_F_PXNAME:
1859 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1860 break;
1861 case ST_F_SVNAME:
1862 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1863 break;
1864 case ST_F_MODE:
1865 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1866 break;
1867 case ST_F_SCUR:
1868 metric = mkf_u32(0, l->nbconn);
1869 break;
1870 case ST_F_SMAX:
1871 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1872 break;
1873 case ST_F_SLIM:
1874 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1875 break;
1876 case ST_F_STOT:
1877 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1878 break;
1879 case ST_F_BIN:
1880 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1881 break;
1882 case ST_F_BOUT:
1883 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1884 break;
1885 case ST_F_DREQ:
1886 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1887 break;
1888 case ST_F_DRESP:
1889 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1890 break;
1891 case ST_F_EREQ:
1892 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1893 break;
1894 case ST_F_DCON:
1895 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1896 break;
1897 case ST_F_DSES:
1898 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1899 break;
1900 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001901 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001902 break;
1903 case ST_F_PID:
1904 metric = mkf_u32(FO_KEY, relative_pid);
1905 break;
1906 case ST_F_IID:
1907 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1908 break;
1909 case ST_F_SID:
1910 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1911 break;
1912 case ST_F_TYPE:
1913 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1914 break;
1915 case ST_F_WREW:
1916 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1917 break;
1918 case ST_F_EINT:
1919 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1920 break;
1921 case ST_F_ADDR:
1922 if (flags & STAT_SHLGNDS) {
1923 char str[INET6_ADDRSTRLEN];
1924 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001925
William Dauchy655e14e2021-02-14 23:22:54 +01001926 port = get_host_port(&l->rx.addr);
1927 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1928 case AF_INET:
1929 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1930 chunk_appendf(out, "%s:%d", str, port);
1931 break;
1932 case AF_INET6:
1933 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1934 chunk_appendf(out, "[%s]:%d", str, port);
1935 break;
1936 case AF_UNIX:
1937 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1938 break;
1939 case -1:
1940 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1941 chunk_strcat(out, strerror(errno));
1942 break;
1943 default: /* address family not supported */
1944 break;
1945 }
1946 }
1947 break;
1948 default:
1949 /* not used for listen. If a specific metric
1950 * is requested, return an error. Otherwise continue.
1951 */
1952 if (selected_field != NULL)
1953 return 0;
1954 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001955 }
William Dauchy655e14e2021-02-14 23:22:54 +01001956 stats[current_field] = metric;
1957 if (selected_field != NULL)
1958 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001959 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001960 return 1;
1961}
1962
1963/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001964 * from stream interface <si>. The caller is responsible for clearing the trash
1965 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001966 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001967static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001968{
1969 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001970 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1971 struct stats_module *mod;
1972 size_t stats_count = ST_F_TOTAL_FIELDS;
1973
1974 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001975
William Dauchy655e14e2021-02-14 23:22:54 +01001976 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats,
1977 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001978 return 0;
1979
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001980 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1981 void *counters;
1982
1983 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1984 stats_count += mod->stats_count;
1985 continue;
1986 }
1987
1988 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
1989 mod->fill_stats(counters, stats + stats_count);
1990 stats_count += mod->stats_count;
1991 }
1992
1993 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001994}
1995
1996enum srv_stats_state {
1997 SRV_STATS_STATE_DOWN = 0,
1998 SRV_STATS_STATE_DOWN_AGENT,
1999 SRV_STATS_STATE_GOING_UP,
2000 SRV_STATS_STATE_UP_GOING_DOWN,
2001 SRV_STATS_STATE_UP,
2002 SRV_STATS_STATE_NOLB_GOING_DOWN,
2003 SRV_STATS_STATE_NOLB,
2004 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2005 SRV_STATS_STATE_DRAIN,
2006 SRV_STATS_STATE_DRAIN_AGENT,
2007 SRV_STATS_STATE_NO_CHECK,
2008
2009 SRV_STATS_STATE_COUNT, /* Must be last */
2010};
2011
2012static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2013 [SRV_STATS_STATE_DOWN] = "DOWN",
2014 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2015 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2016 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2017 [SRV_STATS_STATE_UP] = "UP",
2018 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2019 [SRV_STATS_STATE_NOLB] = "NOLB",
2020 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2021 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2022 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2023 [SRV_STATS_STATE_NO_CHECK] = "no check"
2024};
2025
William Dauchyd3a9a492021-01-25 17:29:03 +01002026/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002027 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002028static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2029 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002030{
Emeric Brun52a91d32017-08-31 14:41:55 +02002031 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002032 if ((ref->check.state & CHK_ST_ENABLED) &&
2033 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002034 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002035 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002036 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002037 }
2038
Emeric Brun52a91d32017-08-31 14:41:55 +02002039 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002040 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002041 *state = SRV_STATS_STATE_DRAIN_AGENT;
2042 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2043 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002044 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002045 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002046 }
2047
William Dauchyd3a9a492021-01-25 17:29:03 +01002048 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2049 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002050 }
2051 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002052 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002053 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2054 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002055 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002056 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002057 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002058 }
2059 }
2060 else { /* stopped */
2061 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002062 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002063 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002064 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002065 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002066 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002067 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002068 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002069 }
2070 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002071}
William Lallemand74c24fb2016-11-21 17:18:36 +01002072
William Dauchyd3a9a492021-01-25 17:29:03 +01002073/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2074 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2075 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2076 * this value, or if the selected field is not implemented for servers, the
2077 * function returns 0, otherwise, it returns 1. <flags> can take the value
2078 * STAT_SHLGNDS.
2079 */
2080int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2081 struct field *stats, int len,
2082 enum stat_field *selected_field)
2083{
2084 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2085 struct server *via = sv->track ? sv->track : sv;
2086 struct server *ref = via;
2087 enum srv_stats_state state = 0;
2088 char str[INET6_ADDRSTRLEN];
2089 struct buffer *out = get_trash_chunk();
2090 char *fld_status;
2091 long long srv_samples_counter;
2092 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002093
William Dauchyd3a9a492021-01-25 17:29:03 +01002094 if (len < ST_F_TOTAL_FIELDS)
2095 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002096
William Dauchyd3a9a492021-01-25 17:29:03 +01002097 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002098
William Dauchyd3a9a492021-01-25 17:29:03 +01002099 /* compute state for later use */
2100 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2101 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2102 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2103 /* we have "via" which is the tracked server as described in the configuration,
2104 * and "ref" which is the checked server and the end of the chain.
2105 */
2106 while (ref->track)
2107 ref = ref->track;
2108 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002109 }
2110
William Dauchyd3a9a492021-01-25 17:29:03 +01002111 /* compue time values for later use */
2112 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2113 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2114 *selected_field == ST_F_TTIME) {
2115 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2116 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2117 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002118 }
2119
William Dauchyd3a9a492021-01-25 17:29:03 +01002120 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2121 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002122
William Dauchyd3a9a492021-01-25 17:29:03 +01002123 switch (current_field) {
2124 case ST_F_PXNAME:
2125 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2126 break;
2127 case ST_F_SVNAME:
2128 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2129 break;
2130 case ST_F_MODE:
2131 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002132 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002133 case ST_F_QCUR:
William Dauchyd3a9a492021-01-25 17:29:03 +01002134 metric = mkf_u32(0, sv->nbpend);
2135 break;
2136 case ST_F_QMAX:
2137 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2138 break;
2139 case ST_F_SCUR:
2140 metric = mkf_u32(0, sv->cur_sess);
2141 break;
2142 case ST_F_SMAX:
2143 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2144 break;
2145 case ST_F_SLIM:
2146 if (sv->maxconn)
2147 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2148 break;
2149 case ST_F_SRV_ICUR:
2150 metric = mkf_u32(0, sv->curr_idle_conns);
2151 break;
2152 case ST_F_SRV_ILIM:
2153 if (sv->max_idle_conns != -1)
2154 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2155 break;
2156 case ST_F_STOT:
2157 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2158 break;
2159 case ST_F_BIN:
2160 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2161 break;
2162 case ST_F_BOUT:
2163 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2164 break;
2165 case ST_F_DRESP:
2166 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2167 break;
2168 case ST_F_ECON:
2169 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2170 break;
2171 case ST_F_ERESP:
2172 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2173 break;
2174 case ST_F_WRETR:
2175 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2176 break;
2177 case ST_F_WREDIS:
2178 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2179 break;
2180 case ST_F_WREW:
2181 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2182 break;
2183 case ST_F_EINT:
2184 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2185 break;
2186 case ST_F_CONNECT:
2187 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2188 break;
2189 case ST_F_REUSE:
2190 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2191 break;
2192 case ST_F_IDLE_CONN_CUR:
2193 metric = mkf_u32(0, sv->curr_idle_nb);
2194 break;
2195 case ST_F_SAFE_CONN_CUR:
2196 metric = mkf_u32(0, sv->curr_safe_nb);
2197 break;
2198 case ST_F_USED_CONN_CUR:
2199 metric = mkf_u32(0, sv->curr_used_conns);
2200 break;
2201 case ST_F_NEED_CONN_EST:
2202 metric = mkf_u32(0, sv->est_need_conns);
2203 break;
2204 case ST_F_STATUS:
2205 fld_status = chunk_newstr(out);
2206 if (sv->cur_admin & SRV_ADMF_RMAINT)
2207 chunk_appendf(out, "MAINT (resolution)");
2208 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2209 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2210 else if (sv->cur_admin & SRV_ADMF_MAINT)
2211 chunk_appendf(out, "MAINT");
2212 else
2213 chunk_appendf(out,
2214 srv_hlt_st[state],
2215 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2216 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002217
William Dauchyd3a9a492021-01-25 17:29:03 +01002218 metric = mkf_str(FO_STATUS, fld_status);
2219 break;
2220 case ST_F_LASTCHG:
2221 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2222 break;
2223 case ST_F_WEIGHT:
2224 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2225 break;
2226 case ST_F_UWEIGHT:
2227 metric = mkf_u32(FN_AVG, sv->uweight);
2228 break;
2229 case ST_F_ACT:
2230 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2231 break;
2232 case ST_F_BCK:
2233 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2234 break;
2235 case ST_F_CHKFAIL:
2236 if (sv->check.state & CHK_ST_ENABLED)
2237 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2238 break;
2239 case ST_F_CHKDOWN:
2240 if (sv->check.state & CHK_ST_ENABLED)
2241 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2242 break;
2243 case ST_F_DOWNTIME:
2244 if (sv->check.state & CHK_ST_ENABLED)
2245 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2246 break;
2247 case ST_F_QLIMIT:
2248 if (sv->maxqueue)
2249 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2250 break;
2251 case ST_F_PID:
2252 metric = mkf_u32(FO_KEY, relative_pid);
2253 break;
2254 case ST_F_IID:
2255 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2256 break;
2257 case ST_F_SID:
2258 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2259 break;
2260 case ST_F_THROTTLE:
2261 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2262 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2263 break;
2264 case ST_F_LBTOT:
2265 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2266 break;
2267 case ST_F_TRACKED:
2268 if (sv->track) {
2269 char *fld_track = chunk_newstr(out);
2270 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2271 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2272 }
2273 break;
2274 case ST_F_TYPE:
2275 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2276 break;
2277 case ST_F_RATE:
2278 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2279 break;
2280 case ST_F_RATE_MAX:
2281 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2282 break;
2283 case ST_F_CHECK_STATUS:
2284 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2285 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002286
William Dauchyd3a9a492021-01-25 17:29:03 +01002287 fld_chksts = chunk_newstr(out);
2288 chunk_strcat(out, "* "); // for check in progress
2289 chunk_strcat(out, get_check_status_info(sv->check.status));
2290 if (!(sv->check.state & CHK_ST_INPROGRESS))
2291 fld_chksts += 2; // skip "* "
2292 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
2293 }
2294 break;
2295 case ST_F_CHECK_CODE:
2296 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2297 sv->check.status >= HCHK_STATUS_L57DATA)
2298 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2299 break;
2300 case ST_F_CHECK_DURATION:
2301 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2302 sv->check.status >= HCHK_STATUS_CHECKED)
2303 metric = mkf_u64(FN_DURATION, sv->check.duration);
2304 break;
2305 case ST_F_CHECK_DESC:
2306 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2307 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2308 break;
2309 case ST_F_LAST_CHK:
2310 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2311 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2312 break;
2313 case ST_F_CHECK_RISE:
2314 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2315 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2316 break;
2317 case ST_F_CHECK_FALL:
2318 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2319 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2320 break;
2321 case ST_F_CHECK_HEALTH:
2322 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2323 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2324 break;
2325 case ST_F_REQ_TOT:
2326 if (px->mode == PR_MODE_HTTP)
2327 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2328 break;
2329 case ST_F_HRSP_1XX:
2330 if (px->mode == PR_MODE_HTTP)
2331 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2332 break;
2333 case ST_F_HRSP_2XX:
2334 if (px->mode == PR_MODE_HTTP)
2335 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2336 break;
2337 case ST_F_HRSP_3XX:
2338 if (px->mode == PR_MODE_HTTP)
2339 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2340 break;
2341 case ST_F_HRSP_4XX:
2342 if (px->mode == PR_MODE_HTTP)
2343 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2344 break;
2345 case ST_F_HRSP_5XX:
2346 if (px->mode == PR_MODE_HTTP)
2347 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2348 break;
2349 case ST_F_HRSP_OTHER:
2350 if (px->mode == PR_MODE_HTTP)
2351 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2352 break;
2353 case ST_F_HANAFAIL:
2354 if (ref->observe)
2355 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2356 break;
2357 case ST_F_CLI_ABRT:
2358 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2359 break;
2360 case ST_F_SRV_ABRT:
2361 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2362 break;
2363 case ST_F_LASTSESS:
2364 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2365 break;
2366 case ST_F_QTIME:
2367 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2368 break;
2369 case ST_F_CTIME:
2370 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2371 break;
2372 case ST_F_RTIME:
2373 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2374 break;
2375 case ST_F_TTIME:
2376 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2377 break;
2378 case ST_F_QT_MAX:
2379 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2380 break;
2381 case ST_F_CT_MAX:
2382 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2383 break;
2384 case ST_F_RT_MAX:
2385 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2386 break;
2387 case ST_F_TT_MAX:
2388 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2389 break;
2390 case ST_F_ADDR:
2391 if (flags & STAT_SHLGNDS) {
2392 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2393 case AF_INET:
2394 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2395 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2396 break;
2397 case AF_INET6:
2398 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2399 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2400 break;
2401 case AF_UNIX:
2402 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2403 break;
2404 case -1:
2405 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2406 chunk_strcat(out, strerror(errno));
2407 break;
2408 default: /* address family not supported */
2409 break;
2410 }
2411 }
2412 break;
2413 case ST_F_COOKIE:
2414 if (flags & STAT_SHLGNDS && sv->cookie)
2415 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2416 break;
2417 default:
2418 /* not used for servers. If a specific metric
2419 * is requested, return an error. Otherwise continue.
2420 */
2421 if (selected_field != NULL)
2422 return 0;
2423 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002424 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002425 stats[current_field] = metric;
2426 if (selected_field != NULL)
2427 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002428 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002429 return 1;
2430}
2431
2432/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02002433 * from stream interface <si>, and server state <state>. The caller is
2434 * responsible for clearing the trash if needed. Returns non-zero if it emits
2435 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002436 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002437static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002438{
2439 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002440 struct stats_module *mod;
2441 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2442 size_t stats_count = ST_F_TOTAL_FIELDS;
2443
2444 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002445
William Dauchyd3a9a492021-01-25 17:29:03 +01002446 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats,
2447 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002448 return 0;
2449
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002450 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2451 void *counters;
2452
2453 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2454 continue;
2455
2456 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2457 stats_count += mod->stats_count;
2458 continue;
2459 }
2460
2461 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2462 mod->fill_stats(counters, stats + stats_count);
2463 stats_count += mod->stats_count;
2464 }
2465
2466 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002467}
2468
William Dauchyda3b4662021-01-25 17:29:01 +01002469/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002470 */
William Dauchyda3b4662021-01-25 17:29:01 +01002471static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002472{
William Dauchyda3b4662021-01-25 17:29:01 +01002473 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002474 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002475
William Dauchyda3b4662021-01-25 17:29:01 +01002476 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002477 for (srv = px->srv; srv; srv = srv->next) {
2478 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002479 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002480 if (srv_currently_usable(srv) &&
2481 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002482 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002483 }
William Dauchyda3b4662021-01-25 17:29:01 +01002484 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002485 }
2486
Willy Tarreaubd715102020-10-23 22:44:30 +02002487 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2488 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002489 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002490 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2491
William Dauchyda3b4662021-01-25 17:29:01 +01002492 /* use tmp variable then assign result to make gcc happy */
2493 *nbup = nbup_tmp;
2494 *nbsrv = nbsrv_tmp;
2495 *totuw = totuw_tmp;
2496}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002497
William Dauchyda3b4662021-01-25 17:29:01 +01002498/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2499 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2500 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2501 * this value, or if the selected field is not implemented for backends, the
2502 * function returns 0, otherwise, it returns 1. <flags> can take the value
2503 * STAT_SHLGNDS.
2504 */
2505int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2506 enum stat_field *selected_field)
2507{
2508 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2509 long long be_samples_counter;
2510 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2511 struct buffer *out = get_trash_chunk();
2512 int nbup, nbsrv, totuw;
2513 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002514
William Dauchyda3b4662021-01-25 17:29:01 +01002515 if (len < ST_F_TOTAL_FIELDS)
2516 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002517
William Dauchyda3b4662021-01-25 17:29:01 +01002518 nbup = nbsrv = totuw = 0;
2519 /* some srv values compute for later if we either select all fields or
2520 * need them for one of the mentioned ones */
2521 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2522 *selected_field == ST_F_UWEIGHT)
2523 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002524
William Dauchyda3b4662021-01-25 17:29:01 +01002525 /* same here but specific to time fields */
2526 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2527 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2528 *selected_field == ST_F_TTIME) {
2529 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2530 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2531 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002532 }
2533
William Dauchyda3b4662021-01-25 17:29:01 +01002534 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2535 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002536
William Dauchyda3b4662021-01-25 17:29:01 +01002537 switch (current_field) {
2538 case ST_F_PXNAME:
2539 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2540 break;
2541 case ST_F_SVNAME:
2542 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2543 break;
2544 case ST_F_MODE:
2545 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2546 break;
2547 case ST_F_QCUR:
2548 metric = mkf_u32(0, px->nbpend);
2549 break;
2550 case ST_F_QMAX:
2551 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2552 break;
2553 case ST_F_SCUR:
2554 metric = mkf_u32(0, px->beconn);
2555 break;
2556 case ST_F_SMAX:
2557 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2558 break;
2559 case ST_F_SLIM:
2560 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2561 break;
2562 case ST_F_STOT:
2563 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2564 break;
2565 case ST_F_BIN:
2566 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2567 break;
2568 case ST_F_BOUT:
2569 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2570 break;
2571 case ST_F_DREQ:
2572 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2573 break;
2574 case ST_F_DRESP:
2575 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2576 break;
2577 case ST_F_ECON:
2578 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2579 break;
2580 case ST_F_ERESP:
2581 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2582 break;
2583 case ST_F_WRETR:
2584 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2585 break;
2586 case ST_F_WREDIS:
2587 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2588 break;
2589 case ST_F_WREW:
2590 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2591 break;
2592 case ST_F_EINT:
2593 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2594 break;
2595 case ST_F_CONNECT:
2596 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2597 break;
2598 case ST_F_REUSE:
2599 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2600 break;
2601 case ST_F_STATUS:
2602 fld = chunk_newstr(out);
2603 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2604 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2605 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2606 metric = mkf_str(FO_STATUS, fld);
2607 break;
2608 case ST_F_WEIGHT:
2609 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2610 break;
2611 case ST_F_UWEIGHT:
2612 metric = mkf_u32(FN_AVG, totuw);
2613 break;
2614 case ST_F_ACT:
2615 metric = mkf_u32(0, px->srv_act);
2616 break;
2617 case ST_F_BCK:
2618 metric = mkf_u32(0, px->srv_bck);
2619 break;
2620 case ST_F_CHKDOWN:
2621 metric = mkf_u64(FN_COUNTER, px->down_trans);
2622 break;
2623 case ST_F_LASTCHG:
2624 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2625 break;
2626 case ST_F_DOWNTIME:
2627 if (px->srv)
2628 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2629 break;
2630 case ST_F_PID:
2631 metric = mkf_u32(FO_KEY, relative_pid);
2632 break;
2633 case ST_F_IID:
2634 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2635 break;
2636 case ST_F_SID:
2637 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2638 break;
2639 case ST_F_LBTOT:
2640 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2641 break;
2642 case ST_F_TYPE:
2643 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2644 break;
2645 case ST_F_RATE:
2646 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2647 break;
2648 case ST_F_RATE_MAX:
2649 metric = mkf_u32(0, px->be_counters.sps_max);
2650 break;
2651 case ST_F_COOKIE:
2652 if (flags & STAT_SHLGNDS && px->cookie_name)
2653 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2654 break;
2655 case ST_F_ALGO:
2656 if (flags & STAT_SHLGNDS)
2657 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2658 break;
2659 case ST_F_REQ_TOT:
2660 if (px->mode == PR_MODE_HTTP)
2661 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2662 break;
2663 case ST_F_HRSP_1XX:
2664 if (px->mode == PR_MODE_HTTP)
2665 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2666 break;
2667 case ST_F_HRSP_2XX:
2668 if (px->mode == PR_MODE_HTTP)
2669 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2670 break;
2671 case ST_F_HRSP_3XX:
2672 if (px->mode == PR_MODE_HTTP)
2673 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2674 break;
2675 case ST_F_HRSP_4XX:
2676 if (px->mode == PR_MODE_HTTP)
2677 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2678 break;
2679 case ST_F_HRSP_5XX:
2680 if (px->mode == PR_MODE_HTTP)
2681 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2682 break;
2683 case ST_F_HRSP_OTHER:
2684 if (px->mode == PR_MODE_HTTP)
2685 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2686 break;
2687 case ST_F_CACHE_LOOKUPS:
2688 if (px->mode == PR_MODE_HTTP)
2689 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2690 break;
2691 case ST_F_CACHE_HITS:
2692 if (px->mode == PR_MODE_HTTP)
2693 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2694 break;
2695 case ST_F_CLI_ABRT:
2696 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2697 break;
2698 case ST_F_SRV_ABRT:
2699 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2700 break;
2701 case ST_F_COMP_IN:
2702 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2703 break;
2704 case ST_F_COMP_OUT:
2705 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2706 break;
2707 case ST_F_COMP_BYP:
2708 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2709 break;
2710 case ST_F_COMP_RSP:
2711 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2712 break;
2713 case ST_F_LASTSESS:
2714 metric = mkf_s32(FN_AGE, be_lastsession(px));
2715 break;
2716 case ST_F_QTIME:
2717 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2718 break;
2719 case ST_F_CTIME:
2720 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2721 break;
2722 case ST_F_RTIME:
2723 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2724 break;
2725 case ST_F_TTIME:
2726 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2727 break;
2728 case ST_F_QT_MAX:
2729 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2730 break;
2731 case ST_F_CT_MAX:
2732 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2733 break;
2734 case ST_F_RT_MAX:
2735 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2736 break;
2737 case ST_F_TT_MAX:
2738 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2739 break;
2740 default:
2741 /* not used for backends. If a specific metric
2742 * is requested, return an error. Otherwise continue.
2743 */
2744 if (selected_field != NULL)
2745 return 0;
2746 continue;
2747 }
2748 stats[current_field] = metric;
2749 if (selected_field != NULL)
2750 break;
2751 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002752 return 1;
2753}
2754
2755/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002756 * interface <si>. The caller is responsible for clearing the trash if needed.
2757 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002758 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002759static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002760{
2761 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002762 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2763 struct stats_module *mod;
2764 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002765
2766 if (!(px->cap & PR_CAP_BE))
2767 return 0;
2768
2769 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2770 return 0;
2771
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002772 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2773
William Dauchyda3b4662021-01-25 17:29:01 +01002774 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002775 return 0;
2776
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002777 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2778 struct extra_counters *counters;
2779
2780 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2781 continue;
2782
2783 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2784 stats_count += mod->stats_count;
2785 continue;
2786 }
2787
2788 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2789 mod->fill_stats(counters, stats + stats_count);
2790 stats_count += mod->stats_count;
2791 }
2792
2793 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002794}
2795
2796/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2797 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2798 * for clearing the trash if needed.
2799 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002800static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002801{
2802 struct appctx *appctx = __objt_appctx(si->end);
2803 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002804 struct stats_module *mod;
2805 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002806
2807 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2808 /* A form to enable/disable this proxy servers */
2809
2810 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2811 scope_txt[0] = 0;
2812 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002813 const char *scope_ptr = stats_scope_ptr(appctx, si);
2814
William Lallemand74c24fb2016-11-21 17:18:36 +01002815 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002816 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002817 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2818 }
2819
2820 chunk_appendf(&trash,
2821 "<form method=\"post\">");
2822 }
2823
2824 /* print a new table */
2825 chunk_appendf(&trash,
2826 "<table class=\"tbl\" width=\"100%%\">\n"
2827 "<tr class=\"titre\">"
2828 "<th class=\"pxname\" width=\"10%%\">");
2829
2830 chunk_appendf(&trash,
2831 "<a name=\"%s\"></a>%s"
2832 "<a class=px href=\"#%s\">%s</a>",
2833 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002834 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002835 px->id, px->id);
2836
Willy Tarreau676c29e2019-10-09 10:50:01 +02002837 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002838 /* cap, mode, id */
2839 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2840 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2841 px->uuid);
2842 chunk_appendf(&trash, "</div>");
2843 }
2844
2845 chunk_appendf(&trash,
2846 "%s</th>"
2847 "<th class=\"%s\" width=\"90%%\">%s</th>"
2848 "</tr>\n"
2849 "</table>\n"
2850 "<table class=\"tbl\" width=\"100%%\">\n"
2851 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002852 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002853 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2854
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002855 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002856 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002857 if ((px->cap & PR_CAP_BE) && px->srv)
2858 chunk_appendf(&trash,
2859 "<th rowspan=2 width=1><input type=\"checkbox\" "
2860 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2861 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2862 px->id,
2863 px->id);
2864 else
2865 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002866 }
2867
2868 chunk_appendf(&trash,
2869 "<th rowspan=2></th>"
2870 "<th colspan=3>Queue</th>"
2871 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2872 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2873 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002874 "<th colspan=9>Server</th>");
2875
2876 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2877 // calculate the count of module for colspan attribute
2878 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2879 ++stats_module_len;
2880 }
2881 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2882 stats_module_len);
2883 }
2884
2885 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002886 "</tr>\n"
2887 "<tr class=\"titre\">"
2888 "<th>Cur</th><th>Max</th><th>Limit</th>"
2889 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2890 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2891 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2892 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2893 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2894 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002895 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002896
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002897 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2898 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2899 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2900 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002901 }
2902
2903 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002904}
2905
2906/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2907 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2908 */
2909static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2910{
2911 struct appctx *appctx = __objt_appctx(si->end);
2912 chunk_appendf(&trash, "</table>");
2913
2914 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2915 /* close the form used to enable/disable this proxy servers */
2916 chunk_appendf(&trash,
2917 "Choose the action to perform on the checked servers : "
2918 "<select name=action>"
2919 "<option value=\"\"></option>"
2920 "<option value=\"ready\">Set state to READY</option>"
2921 "<option value=\"drain\">Set state to DRAIN</option>"
2922 "<option value=\"maint\">Set state to MAINT</option>"
2923 "<option value=\"dhlth\">Health: disable checks</option>"
2924 "<option value=\"ehlth\">Health: enable checks</option>"
2925 "<option value=\"hrunn\">Health: force UP</option>"
2926 "<option value=\"hnolb\">Health: force NOLB</option>"
2927 "<option value=\"hdown\">Health: force DOWN</option>"
2928 "<option value=\"dagent\">Agent: disable checks</option>"
2929 "<option value=\"eagent\">Agent: enable checks</option>"
2930 "<option value=\"arunn\">Agent: force UP</option>"
2931 "<option value=\"adown\">Agent: force DOWN</option>"
2932 "<option value=\"shutdown\">Kill Sessions</option>"
2933 "</select>"
2934 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2935 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2936 "</form>",
2937 px->uuid);
2938 }
2939
2940 chunk_appendf(&trash, "<p>\n");
2941}
2942
2943/*
2944 * Dumps statistics for a proxy. The output is sent to the stream interface's
2945 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2946 * buffer space, or non-zero if everything completed. This function is used
2947 * both by the CLI and the HTTP entry points, and is able to dump the output
2948 * in HTML or CSV formats. If the later, <uri> must be NULL.
2949 */
Christopher Fauletef779222018-10-31 08:47:01 +01002950int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2951 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002952{
2953 struct appctx *appctx = __objt_appctx(si->end);
2954 struct stream *s = si_strm(si);
2955 struct channel *rep = si_ic(si);
2956 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2957 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002958
2959 chunk_reset(&trash);
2960
2961 switch (appctx->ctx.stats.px_st) {
2962 case STAT_PX_ST_INIT:
2963 /* we are on a new proxy */
2964 if (uri && uri->scope) {
2965 /* we have a limited scope, we have to check the proxy name */
2966 struct stat_scope *scope;
2967 int len;
2968
2969 len = strlen(px->id);
2970 scope = uri->scope;
2971
2972 while (scope) {
2973 /* match exact proxy name */
2974 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2975 break;
2976
2977 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002978 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01002979 break;
2980 scope = scope->next;
2981 }
2982
2983 /* proxy name not found : don't dump anything */
2984 if (scope == NULL)
2985 return 1;
2986 }
2987
2988 /* if the user has requested a limited output and the proxy
2989 * name does not match, skip it.
2990 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002991 if (appctx->ctx.stats.scope_len) {
2992 const char *scope_ptr = stats_scope_ptr(appctx, si);
2993
2994 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2995 return 1;
2996 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002997
2998 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2999 (appctx->ctx.stats.iid != -1) &&
3000 (px->uuid != appctx->ctx.stats.iid))
3001 return 1;
3002
3003 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
3004 /* fall through */
3005
3006 case STAT_PX_ST_TH:
3007 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02003008 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003009 if (!stats_putchk(rep, htx, &trash))
3010 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003011 }
3012
3013 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
3014 /* fall through */
3015
3016 case STAT_PX_ST_FE:
3017 /* print the frontend */
3018 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003019 if (!stats_putchk(rep, htx, &trash))
3020 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003021 }
3022
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003023 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01003024 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
3025 /* fall through */
3026
3027 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003028 /* obj2 points to listeners list as initialized above */
3029 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003030 if (htx) {
3031 if (htx_almost_full(htx))
3032 goto full;
3033 }
3034 else {
3035 if (buffer_almost_full(&rep->buf))
3036 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003037 }
3038
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003039 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003040 if (!l->counters)
3041 continue;
3042
3043 if (appctx->ctx.stats.flags & STAT_BOUND) {
3044 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
3045 break;
3046
3047 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
3048 continue;
3049 }
3050
3051 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02003052 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003053 if (!stats_putchk(rep, htx, &trash))
3054 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003055 }
3056 }
3057
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003058 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01003059 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
3060 /* fall through */
3061
3062 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003063 /* obj2 points to servers list as initialized above */
3064 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01003065 if (htx) {
3066 if (htx_almost_full(htx))
3067 goto full;
3068 }
3069 else {
3070 if (buffer_almost_full(&rep->buf))
3071 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003072 }
3073
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003074 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01003075
3076 if (appctx->ctx.stats.flags & STAT_BOUND) {
3077 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
3078 break;
3079
3080 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
3081 continue;
3082 }
3083
Willy Tarreau3e320362020-10-23 17:28:57 +02003084 /* do not report disabled servers */
3085 if (appctx->ctx.stats.flags & STAT_HIDE_MAINT &&
3086 sv->cur_admin & SRV_ADMF_MAINT) {
3087 continue;
3088 }
3089
William Lallemand74c24fb2016-11-21 17:18:36 +01003090 svs = sv;
3091 while (svs->track)
3092 svs = svs->track;
3093
3094 /* do not report servers which are DOWN and not changing state */
3095 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003096 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3097 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003098 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3099 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3100 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3101 continue;
3102 }
3103
Willy Tarreau43241ff2019-10-09 11:27:51 +02003104 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003105 if (!stats_putchk(rep, htx, &trash))
3106 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003107 }
3108 } /* for sv */
3109
3110 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
3111 /* fall through */
3112
3113 case STAT_PX_ST_BE:
3114 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02003115 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003116 if (!stats_putchk(rep, htx, &trash))
3117 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003118 }
3119
3120 appctx->ctx.stats.px_st = STAT_PX_ST_END;
3121 /* fall through */
3122
3123 case STAT_PX_ST_END:
3124 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3125 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003126 if (!stats_putchk(rep, htx, &trash))
3127 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003128 }
3129
3130 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
3131 /* fall through */
3132
3133 case STAT_PX_ST_FIN:
3134 return 1;
3135
3136 default:
3137 /* unknown state, we should put an abort() here ! */
3138 return 1;
3139 }
Christopher Fauletef779222018-10-31 08:47:01 +01003140
3141 full:
3142 si_rx_room_blk(si);
3143 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003144}
3145
3146/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3147 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3148 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003149static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003150{
3151 /* WARNING! This must fit in the first buffer !!! */
3152 chunk_appendf(&trash,
3153 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3154 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3155 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03003156 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003157 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3158 "<style type=\"text/css\"><!--\n"
3159 "body {"
3160 " font-family: arial, helvetica, sans-serif;"
3161 " font-size: 12px;"
3162 " font-weight: normal;"
3163 " color: black;"
3164 " background: white;"
3165 "}\n"
3166 "th,td {"
3167 " font-size: 10px;"
3168 "}\n"
3169 "h1 {"
3170 " font-size: x-large;"
3171 " margin-bottom: 0.5em;"
3172 "}\n"
3173 "h2 {"
3174 " font-family: helvetica, arial;"
3175 " font-size: x-large;"
3176 " font-weight: bold;"
3177 " font-style: italic;"
3178 " color: #6020a0;"
3179 " margin-top: 0em;"
3180 " margin-bottom: 0em;"
3181 "}\n"
3182 "h3 {"
3183 " font-family: helvetica, arial;"
3184 " font-size: 16px;"
3185 " font-weight: bold;"
3186 " color: #b00040;"
3187 " background: #e8e8d0;"
3188 " margin-top: 0em;"
3189 " margin-bottom: 0em;"
3190 "}\n"
3191 "li {"
3192 " margin-top: 0.25em;"
3193 " margin-right: 2em;"
3194 "}\n"
3195 ".hr {margin-top: 0.25em;"
3196 " border-color: black;"
3197 " border-bottom-style: solid;"
3198 "}\n"
3199 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3200 ".total {background: #20D0D0;color: #ffff80;}\n"
3201 ".frontend {background: #e8e8d0;}\n"
3202 ".socket {background: #d0d0d0;}\n"
3203 ".backend {background: #e8e8d0;}\n"
3204 ".active_down {background: #ff9090;}\n"
3205 ".active_going_up {background: #ffd020;}\n"
3206 ".active_going_down {background: #ffffa0;}\n"
3207 ".active_up {background: #c0ffc0;}\n"
3208 ".active_nolb {background: #20a0ff;}\n"
3209 ".active_draining {background: #20a0FF;}\n"
3210 ".active_no_check {background: #e0e0e0;}\n"
3211 ".backup_down {background: #ff9090;}\n"
3212 ".backup_going_up {background: #ff80ff;}\n"
3213 ".backup_going_down {background: #c060ff;}\n"
3214 ".backup_up {background: #b0d0ff;}\n"
3215 ".backup_nolb {background: #90b0e0;}\n"
3216 ".backup_draining {background: #cc9900;}\n"
3217 ".backup_no_check {background: #e0e0e0;}\n"
3218 ".maintain {background: #c07820;}\n"
3219 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3220 "\n"
3221 "a.px:link {color: #ffff40; text-decoration: none;}"
3222 "a.px:visited {color: #ffff40; text-decoration: none;}"
3223 "a.px:hover {color: #ffffff; text-decoration: none;}"
3224 "a.lfsb:link {color: #000000; text-decoration: none;}"
3225 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3226 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3227 "\n"
3228 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3229 "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"
3230 "table.tbl td.ac { text-align: center;}\n"
3231 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3232 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3233 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3234 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3235 "\n"
3236 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3237 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3238 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3239 "table.det { border-collapse: collapse; border-style: none; }\n"
3240 "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"
3241 "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"
3242 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
3243 "div.tips {\n"
3244 " display:block;\n"
3245 " visibility:hidden;\n"
3246 " z-index:2147483647;\n"
3247 " position:absolute;\n"
3248 " padding:2px 4px 3px;\n"
3249 " background:#f0f060; color:#000000;\n"
3250 " border:1px solid #7040c0;\n"
3251 " white-space:nowrap;\n"
3252 " font-style:normal;font-size:11px;font-weight:normal;\n"
3253 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3254 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3255 "}\n"
3256 "u:hover div.tips {visibility:visible;}\n"
3257 "-->\n"
3258 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003259 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01003260 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003261 );
3262}
3263
3264/* Dumps the HTML stats information block to the trash for and uses the state from
3265 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
3266 * for clearing the trash if needed.
3267 */
3268static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
3269{
3270 struct appctx *appctx = __objt_appctx(si->end);
3271 unsigned int up = (now.tv_sec - start_date.tv_sec);
3272 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01003273 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02003274 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3275
3276 /* Turn the bytes per second to bits per second and take care of the
3277 * usual ethernet overhead in order to help figure how far we are from
3278 * interface saturation since it's the only case which usually matters.
3279 * For this we count the total size of an Ethernet frame on the wire
3280 * including preamble and IFG (1538) for the largest TCP segment it
3281 * transports (1448 with TCP timestamps). This is not valid for smaller
3282 * packets (under-estimated), but it gives a reasonably accurate
3283 * estimation of how far we are from uplink saturation.
3284 */
3285 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003286
3287 /* WARNING! this has to fit the first packet too.
3288 * We are around 3.5 kB, add adding entries will
3289 * become tricky if we want to support 4kB buffers !
3290 */
3291 chunk_appendf(&trash,
3292 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3293 PRODUCT_NAME "%s</a></h1>\n"
3294 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3295 "<hr width=\"100%%\" class=\"hr\">\n"
3296 "<h3>&gt; General process information</h3>\n"
3297 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003298 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003299 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3300 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3301 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003302 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003303 "Running tasks: %d/%d; idle = %d %%<br>\n"
3304 "</td><td align=\"center\" nowrap>\n"
3305 "<table class=\"lgd\"><tr>\n"
3306 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3307 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3308 "</tr><tr>\n"
3309 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3310 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3311 "</tr><tr>\n"
3312 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3313 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3314 "</tr><tr>\n"
3315 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3316 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3317 "</tr><tr>\n"
3318 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3319 "</tr><tr>\n"
3320 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3321 "</tr></table>\n"
3322 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3323 "</td>"
3324 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3325 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3326 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003327 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
3328 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
3329 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3330 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
3331 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01003332 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003333 up / 86400, (up % 86400) / 3600,
3334 (up % 3600) / 60, (up % 60),
3335 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3336 global.rlimit_memmax ? " MB" : "",
3337 global.rlimit_nofile,
3338 global.maxsock, global.maxconn, global.maxpipes,
3339 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003340 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3341 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau955a11e2021-02-24 17:03:30 +01003342 total_run_queues(), total_allocated_tasks(), ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01003343 );
3344
3345 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003346 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003347 scope_txt[appctx->ctx.stats.scope_len] = '\0';
3348
3349 chunk_appendf(&trash,
3350 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
3351 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
3352 STAT_SCOPE_TXT_MAXLEN);
3353
3354 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3355 scope_txt[0] = 0;
3356 if (appctx->ctx.stats.scope_len) {
3357 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003358 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003359 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3360 }
3361
3362 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
3363 chunk_appendf(&trash,
3364 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3365 uri->uri_prefix,
3366 "",
3367 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3368 scope_txt);
3369 else
3370 chunk_appendf(&trash,
3371 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3372 uri->uri_prefix,
3373 ";up",
3374 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3375 scope_txt);
3376
3377 if (uri->refresh > 0) {
3378 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
3379 chunk_appendf(&trash,
3380 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3381 uri->uri_prefix,
3382 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3383 "",
3384 scope_txt);
3385 else
3386 chunk_appendf(&trash,
3387 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3388 uri->uri_prefix,
3389 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3390 ";norefresh",
3391 scope_txt);
3392 }
3393
3394 chunk_appendf(&trash,
3395 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3396 uri->uri_prefix,
3397 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3398 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3399 scope_txt);
3400
3401 chunk_appendf(&trash,
3402 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3403 uri->uri_prefix,
3404 (uri->refresh > 0) ? ";norefresh" : "",
3405 scope_txt);
3406
3407 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003408 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3409 uri->uri_prefix,
3410 (uri->refresh > 0) ? ";norefresh" : "",
3411 scope_txt, uri->uri_prefix);
3412
3413 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003414 "</ul></td>"
3415 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3416 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3417 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3418 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3419 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3420 "</ul>"
3421 "</td>"
3422 "</tr></table>\n"
3423 ""
3424 );
3425
3426 if (appctx->ctx.stats.st_code) {
3427 switch (appctx->ctx.stats.st_code) {
3428 case STAT_STATUS_DONE:
3429 chunk_appendf(&trash,
3430 "<p><div class=active_up>"
3431 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3432 "Action processed successfully."
3433 "</div>\n", uri->uri_prefix,
3434 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3435 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3436 scope_txt);
3437 break;
3438 case STAT_STATUS_NONE:
3439 chunk_appendf(&trash,
3440 "<p><div class=active_going_down>"
3441 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3442 "Nothing has changed."
3443 "</div>\n", uri->uri_prefix,
3444 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3445 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3446 scope_txt);
3447 break;
3448 case STAT_STATUS_PART:
3449 chunk_appendf(&trash,
3450 "<p><div class=active_going_down>"
3451 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3452 "Action partially processed.<br>"
3453 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3454 "</div>\n", uri->uri_prefix,
3455 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3456 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3457 scope_txt);
3458 break;
3459 case STAT_STATUS_ERRP:
3460 chunk_appendf(&trash,
3461 "<p><div class=active_down>"
3462 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3463 "Action not processed because of invalid parameters."
3464 "<ul>"
3465 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003466 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003467 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3468 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3469 "</ul>"
3470 "</div>\n", uri->uri_prefix,
3471 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3472 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3473 scope_txt);
3474 break;
3475 case STAT_STATUS_EXCD:
3476 chunk_appendf(&trash,
3477 "<p><div class=active_down>"
3478 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3479 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3480 "You should retry with less servers at a time.</b>"
3481 "</div>\n", uri->uri_prefix,
3482 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3483 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3484 scope_txt);
3485 break;
3486 case STAT_STATUS_DENY:
3487 chunk_appendf(&trash,
3488 "<p><div class=active_down>"
3489 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3490 "<b>Action denied.</b>"
3491 "</div>\n", uri->uri_prefix,
3492 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3493 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3494 scope_txt);
3495 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003496 case STAT_STATUS_IVAL:
3497 chunk_appendf(&trash,
3498 "<p><div class=active_down>"
3499 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3500 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3501 "</div>\n", uri->uri_prefix,
3502 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3503 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3504 scope_txt);
3505 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003506 default:
3507 chunk_appendf(&trash,
3508 "<p><div class=active_no_check>"
3509 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3510 "Unexpected result."
3511 "</div>\n", uri->uri_prefix,
3512 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3513 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3514 scope_txt);
3515 }
3516 chunk_appendf(&trash, "<p>\n");
3517 }
3518}
3519
3520/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3521 * for clearing the trash if needed.
3522 */
3523static void stats_dump_html_end()
3524{
3525 chunk_appendf(&trash, "</body></html>\n");
3526}
3527
Simon Horman05ee2132017-01-04 09:37:25 +01003528/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3529 * for clearing it if needed.
3530 */
3531static void stats_dump_json_header()
3532{
3533 chunk_strcat(&trash, "[");
3534}
3535
3536
3537/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3538 * for clearing the trash if needed.
3539 */
3540static void stats_dump_json_end()
3541{
3542 chunk_strcat(&trash, "]");
3543}
3544
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003545/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3546 * a pointer to the current server/listener.
3547 */
3548static int stats_dump_proxies(struct stream_interface *si,
3549 struct htx *htx,
3550 struct uri_auth *uri)
3551{
3552 struct appctx *appctx = __objt_appctx(si->end);
3553 struct channel *rep = si_ic(si);
3554 struct proxy *px;
3555
3556 /* dump proxies */
3557 while (appctx->ctx.stats.obj1) {
3558 if (htx) {
3559 if (htx_almost_full(htx))
3560 goto full;
3561 }
3562 else {
3563 if (buffer_almost_full(&rep->buf))
3564 goto full;
3565 }
3566
3567 px = appctx->ctx.stats.obj1;
3568 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02003569 if (!px->disabled && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003570 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
3571 return 0;
3572 }
3573
3574 appctx->ctx.stats.obj1 = px->next;
3575 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3576 }
3577
3578 return 1;
3579
3580 full:
3581 si_rx_room_blk(si);
3582 return 0;
3583}
3584
William Lallemand74c24fb2016-11-21 17:18:36 +01003585/* This function dumps statistics onto the stream interface's read buffer in
3586 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3587 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3588 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3589 * and the stream must be closed, or -1 in case of any error. This function is
3590 * used by both the CLI and the HTTP handlers.
3591 */
Christopher Fauletef779222018-10-31 08:47:01 +01003592static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
3593 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003594{
3595 struct appctx *appctx = __objt_appctx(si->end);
3596 struct channel *rep = si_ic(si);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003597 enum stats_domain domain = appctx->ctx.stats.domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003598
3599 chunk_reset(&trash);
3600
3601 switch (appctx->st2) {
3602 case STAT_ST_INIT:
3603 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3604 /* fall through */
3605
3606 case STAT_ST_HEAD:
3607 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003608 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003609 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3610 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003611 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003612 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003613 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003614 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003615
Christopher Fauletef779222018-10-31 08:47:01 +01003616 if (!stats_putchk(rep, htx, &trash))
3617 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003618
Christopher Faulet6338a082019-09-09 15:50:54 +02003619 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3620 appctx->st2 = STAT_ST_FIN;
3621 return 1;
3622 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003623 appctx->st2 = STAT_ST_INFO;
3624 /* fall through */
3625
3626 case STAT_ST_INFO:
3627 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3628 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003629 if (!stats_putchk(rep, htx, &trash))
3630 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003631 }
3632
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003633 if (domain == STATS_DOMAIN_PROXY)
3634 appctx->ctx.stats.obj1 = proxies_list;
3635
William Lallemand74c24fb2016-11-21 17:18:36 +01003636 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3637 appctx->st2 = STAT_ST_LIST;
3638 /* fall through */
3639
3640 case STAT_ST_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003641 switch (domain) {
3642 case STATS_DOMAIN_DNS:
Emeric Brund3b44952021-01-06 16:01:02 +01003643 if (!stats_dump_resolvers(si, stat_l[domain],
3644 stat_count[domain],
3645 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003646 return 0;
3647 }
3648 break;
3649
3650 case STATS_DOMAIN_PROXY:
3651 default:
3652 /* dump proxies */
3653 if (!stats_dump_proxies(si, htx, uri))
3654 return 0;
3655 break;
3656 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003657
3658 appctx->st2 = STAT_ST_END;
3659 /* fall through */
3660
3661 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003662 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3663 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3664 stats_dump_html_end();
3665 else
3666 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003667 if (!stats_putchk(rep, htx, &trash))
3668 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003669 }
3670
3671 appctx->st2 = STAT_ST_FIN;
3672 /* fall through */
3673
3674 case STAT_ST_FIN:
3675 return 1;
3676
3677 default:
3678 /* unknown state ! */
3679 appctx->st2 = STAT_ST_FIN;
3680 return -1;
3681 }
Christopher Fauletef779222018-10-31 08:47:01 +01003682
3683 full:
3684 si_rx_room_blk(si);
3685 return 0;
3686
William Lallemand74c24fb2016-11-21 17:18:36 +01003687}
3688
3689/* We reached the stats page through a POST request. The appctx is
3690 * expected to have already been allocated by the caller.
3691 * Parse the posted data and enable/disable servers if necessary.
3692 * Returns 1 if request was parsed or zero if it needs more data.
3693 */
3694static int stats_process_http_post(struct stream_interface *si)
3695{
3696 struct stream *s = si_strm(si);
3697 struct appctx *appctx = objt_appctx(si->end);
3698
3699 struct proxy *px = NULL;
3700 struct server *sv = NULL;
3701
3702 char key[LINESIZE];
3703 int action = ST_ADM_ACTION_NONE;
3704 int reprocess = 0;
3705
3706 int total_servers = 0;
3707 int altered_servers = 0;
3708
3709 char *first_param, *cur_param, *next_param, *end_params;
3710 char *st_cur_param = NULL;
3711 char *st_next_param = NULL;
3712
Christopher Fauleta3618372018-12-13 21:59:56 +01003713 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003714
Christopher Fauletb7f88902019-07-15 21:56:43 +02003715 struct htx *htx = htxbuf(&s->req.buf);
3716 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003717
Christopher Fauletb7f88902019-07-15 21:56:43 +02003718 /* we need more data */
3719 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3720 /* check if we can receive more */
3721 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3722 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3723 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003724 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003725 goto wait;
3726 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003727
Christopher Fauletb7f88902019-07-15 21:56:43 +02003728 /* The request was fully received. Copy data */
3729 blk = htx_get_head_blk(htx);
3730 while (blk) {
3731 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003732
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003733 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003734 break;
3735 if (type == HTX_BLK_DATA) {
3736 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003737
Christopher Fauletb7f88902019-07-15 21:56:43 +02003738 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003739 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3740 goto out;
3741 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003742 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003743 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003744 }
3745
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003746 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003747 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003748 cur_param = next_param = end_params;
3749 *end_params = '\0';
3750
3751 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3752
3753 /*
3754 * Parse the parameters in reverse order to only store the last value.
3755 * From the html form, the backend and the action are at the end.
3756 */
3757 while (cur_param > first_param) {
3758 char *value;
3759 int poffset, plen;
3760
3761 cur_param--;
3762
3763 if ((*cur_param == '&') || (cur_param == first_param)) {
3764 reprocess_servers:
3765 /* Parse the key */
3766 poffset = (cur_param != first_param ? 1 : 0);
3767 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3768 if ((plen > 0) && (plen <= sizeof(key))) {
3769 strncpy(key, cur_param + poffset, plen);
3770 key[plen - 1] = '\0';
3771 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003772 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003773 goto out;
3774 }
3775
3776 /* Parse the value */
3777 value = key;
3778 while (*value != '\0' && *value != '=') {
3779 value++;
3780 }
3781 if (*value == '=') {
3782 /* Ok, a value is found, we can mark the end of the key */
3783 *value++ = '\0';
3784 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003785 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003786 break;
3787
3788 /* Now we can check the key to see what to do */
3789 if (!px && (strcmp(key, "b") == 0)) {
3790 if ((px = proxy_be_by_name(value)) == NULL) {
3791 /* the backend name is unknown or ambiguous (duplicate names) */
3792 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3793 goto out;
3794 }
3795 }
3796 else if (!action && (strcmp(key, "action") == 0)) {
3797 if (strcmp(value, "ready") == 0) {
3798 action = ST_ADM_ACTION_READY;
3799 }
3800 else if (strcmp(value, "drain") == 0) {
3801 action = ST_ADM_ACTION_DRAIN;
3802 }
3803 else if (strcmp(value, "maint") == 0) {
3804 action = ST_ADM_ACTION_MAINT;
3805 }
3806 else if (strcmp(value, "shutdown") == 0) {
3807 action = ST_ADM_ACTION_SHUTDOWN;
3808 }
3809 else if (strcmp(value, "dhlth") == 0) {
3810 action = ST_ADM_ACTION_DHLTH;
3811 }
3812 else if (strcmp(value, "ehlth") == 0) {
3813 action = ST_ADM_ACTION_EHLTH;
3814 }
3815 else if (strcmp(value, "hrunn") == 0) {
3816 action = ST_ADM_ACTION_HRUNN;
3817 }
3818 else if (strcmp(value, "hnolb") == 0) {
3819 action = ST_ADM_ACTION_HNOLB;
3820 }
3821 else if (strcmp(value, "hdown") == 0) {
3822 action = ST_ADM_ACTION_HDOWN;
3823 }
3824 else if (strcmp(value, "dagent") == 0) {
3825 action = ST_ADM_ACTION_DAGENT;
3826 }
3827 else if (strcmp(value, "eagent") == 0) {
3828 action = ST_ADM_ACTION_EAGENT;
3829 }
3830 else if (strcmp(value, "arunn") == 0) {
3831 action = ST_ADM_ACTION_ARUNN;
3832 }
3833 else if (strcmp(value, "adown") == 0) {
3834 action = ST_ADM_ACTION_ADOWN;
3835 }
3836 /* else these are the old supported methods */
3837 else if (strcmp(value, "disable") == 0) {
3838 action = ST_ADM_ACTION_DISABLE;
3839 }
3840 else if (strcmp(value, "enable") == 0) {
3841 action = ST_ADM_ACTION_ENABLE;
3842 }
3843 else if (strcmp(value, "stop") == 0) {
3844 action = ST_ADM_ACTION_STOP;
3845 }
3846 else if (strcmp(value, "start") == 0) {
3847 action = ST_ADM_ACTION_START;
3848 }
3849 else {
3850 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3851 goto out;
3852 }
3853 }
3854 else if (strcmp(key, "s") == 0) {
3855 if (!(px && action)) {
3856 /*
3857 * Indicates that we'll need to reprocess the parameters
3858 * as soon as backend and action are known
3859 */
3860 if (!reprocess) {
3861 st_cur_param = cur_param;
3862 st_next_param = next_param;
3863 }
3864 reprocess = 1;
3865 }
3866 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003867 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003868 switch (action) {
3869 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003870 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003871 altered_servers++;
3872 total_servers++;
3873 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3874 }
3875 break;
3876 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003877 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003878 altered_servers++;
3879 total_servers++;
3880 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3881 }
3882 break;
3883 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003884 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003885 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3886 altered_servers++;
3887 total_servers++;
3888 }
3889 break;
3890 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003891 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003892 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3893 altered_servers++;
3894 total_servers++;
3895 }
3896 break;
3897 case ST_ADM_ACTION_DHLTH:
3898 if (sv->check.state & CHK_ST_CONFIGURED) {
3899 sv->check.state &= ~CHK_ST_ENABLED;
3900 altered_servers++;
3901 total_servers++;
3902 }
3903 break;
3904 case ST_ADM_ACTION_EHLTH:
3905 if (sv->check.state & CHK_ST_CONFIGURED) {
3906 sv->check.state |= CHK_ST_ENABLED;
3907 altered_servers++;
3908 total_servers++;
3909 }
3910 break;
3911 case ST_ADM_ACTION_HRUNN:
3912 if (!(sv->track)) {
3913 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003914 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003915 altered_servers++;
3916 total_servers++;
3917 }
3918 break;
3919 case ST_ADM_ACTION_HNOLB:
3920 if (!(sv->track)) {
3921 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003922 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003923 altered_servers++;
3924 total_servers++;
3925 }
3926 break;
3927 case ST_ADM_ACTION_HDOWN:
3928 if (!(sv->track)) {
3929 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003930 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003931 altered_servers++;
3932 total_servers++;
3933 }
3934 break;
3935 case ST_ADM_ACTION_DAGENT:
3936 if (sv->agent.state & CHK_ST_CONFIGURED) {
3937 sv->agent.state &= ~CHK_ST_ENABLED;
3938 altered_servers++;
3939 total_servers++;
3940 }
3941 break;
3942 case ST_ADM_ACTION_EAGENT:
3943 if (sv->agent.state & CHK_ST_CONFIGURED) {
3944 sv->agent.state |= CHK_ST_ENABLED;
3945 altered_servers++;
3946 total_servers++;
3947 }
3948 break;
3949 case ST_ADM_ACTION_ARUNN:
3950 if (sv->agent.state & CHK_ST_ENABLED) {
3951 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003952 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003953 altered_servers++;
3954 total_servers++;
3955 }
3956 break;
3957 case ST_ADM_ACTION_ADOWN:
3958 if (sv->agent.state & CHK_ST_ENABLED) {
3959 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003960 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003961 altered_servers++;
3962 total_servers++;
3963 }
3964 break;
3965 case ST_ADM_ACTION_READY:
3966 srv_adm_set_ready(sv);
3967 altered_servers++;
3968 total_servers++;
3969 break;
3970 case ST_ADM_ACTION_DRAIN:
3971 srv_adm_set_drain(sv);
3972 altered_servers++;
3973 total_servers++;
3974 break;
3975 case ST_ADM_ACTION_MAINT:
3976 srv_adm_set_maint(sv);
3977 altered_servers++;
3978 total_servers++;
3979 break;
3980 case ST_ADM_ACTION_SHUTDOWN:
Willy Tarreauc3914d42020-09-24 08:39:22 +02003981 if (!px->disabled) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003982 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003983 altered_servers++;
3984 total_servers++;
3985 }
3986 break;
3987 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003988 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003989 } else {
3990 /* the server name is unknown or ambiguous (duplicate names) */
3991 total_servers++;
3992 }
3993 }
3994 if (reprocess && px && action) {
3995 /* Now, we know the backend and the action chosen by the user.
3996 * We can safely restart from the first server parameter
3997 * to reprocess them
3998 */
3999 cur_param = st_cur_param;
4000 next_param = st_next_param;
4001 reprocess = 0;
4002 goto reprocess_servers;
4003 }
4004
4005 next_param = cur_param;
4006 }
4007 }
4008
4009 if (total_servers == 0) {
4010 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4011 }
4012 else if (altered_servers == 0) {
4013 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
4014 }
4015 else if (altered_servers == total_servers) {
4016 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
4017 }
4018 else {
4019 appctx->ctx.stats.st_code = STAT_STATUS_PART;
4020 }
4021 out:
4022 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004023 wait:
4024 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4025 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004026}
4027
4028
Christopher Fauletb7f88902019-07-15 21:56:43 +02004029static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004030{
4031 struct stream *s = si_strm(si);
4032 struct uri_auth *uri = s->be->uri_auth;
4033 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004034 struct htx_sl *sl;
4035 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004036
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004037 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);
4038 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4039 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004040 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004041 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004042
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004043 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004044 goto full;
4045 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
4046 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4047 goto full;
4048 }
Christopher Faulet6338a082019-09-09 15:50:54 +02004049 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
4050 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4051 goto full;
4052 }
Christopher Fauletef779222018-10-31 08:47:01 +01004053 else {
4054 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4055 goto full;
4056 }
4057
4058 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
4059 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004060 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004061 goto full;
4062 }
4063
4064 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4065 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4066 goto full;
4067 }
4068
4069 if (!htx_add_endof(htx, HTX_BLK_EOH))
4070 goto full;
4071
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004072 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004073 return 1;
4074
4075 full:
4076 htx_reset(htx);
4077 si_rx_room_blk(si);
4078 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004079}
4080
Christopher Fauletef779222018-10-31 08:47:01 +01004081
Christopher Fauletb7f88902019-07-15 21:56:43 +02004082static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004083{
4084 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4085 struct stream *s = si_strm(si);
4086 struct uri_auth *uri = s->be->uri_auth;
4087 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004088 struct htx_sl *sl;
4089 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004090
4091 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4092 scope_txt[0] = 0;
4093 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01004094 const char *scope_ptr = stats_scope_ptr(appctx, si);
4095
Christopher Fauletef779222018-10-31 08:47:01 +01004096 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004097 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01004098 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
4099 }
4100
4101 /* We don't want to land on the posted stats page because a refresh will
4102 * repost the data. We don't want this to happen on accident so we redirect
4103 * the browse to the stats page with a GET.
4104 */
4105 chunk_printf(&trash, "%s;st=%s%s%s%s",
4106 uri->uri_prefix,
4107 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4108 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4109 stat_status_codes[appctx->ctx.stats.st_code]) ?
4110 stat_status_codes[appctx->ctx.stats.st_code] :
4111 stat_status_codes[STAT_STATUS_UNKN],
4112 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4113 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4114 scope_txt);
4115
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004116 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4117 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4118 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004119 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004120 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004121
4122 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004123 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4124 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4125 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4126 goto full;
4127
4128 if (!htx_add_endof(htx, HTX_BLK_EOH))
4129 goto full;
4130
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004131 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004132 return 1;
4133
4134full:
4135 htx_reset(htx);
4136 si_rx_room_blk(si);
4137 return 0;
4138}
William Lallemand74c24fb2016-11-21 17:18:36 +01004139
Simon Horman05ee2132017-01-04 09:37:25 +01004140
William Lallemand74c24fb2016-11-21 17:18:36 +01004141/* This I/O handler runs as an applet embedded in a stream interface. It is
4142 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4143 * appctx->st0 contains the operation in progress (dump, done). The handler
4144 * automatically unregisters itself once transfer is complete.
4145 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004146static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004147{
4148 struct stream_interface *si = appctx->owner;
4149 struct stream *s = si_strm(si);
4150 struct channel *req = si_oc(si);
4151 struct channel *res = si_ic(si);
4152 struct htx *req_htx, *res_htx;
4153
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004154 /* only proxy stats are available via http */
4155 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
4156
Christopher Fauletef779222018-10-31 08:47:01 +01004157 res_htx = htx_from_buf(&res->buf);
4158
4159 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4160 goto out;
4161
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004162 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004163 if (!b_size(&res->buf)) {
4164 si_rx_room_blk(si);
4165 goto out;
4166 }
4167
4168 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004169 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4170 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004171
4172 /* all states are processed in sequence */
4173 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02004174 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004175 if (s->txn->meth == HTTP_METH_HEAD)
4176 appctx->st0 = STAT_HTTP_DONE;
4177 else
4178 appctx->st0 = STAT_HTTP_DUMP;
4179 }
4180 }
4181
4182 if (appctx->st0 == STAT_HTTP_DUMP) {
4183 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
4184 appctx->st0 = STAT_HTTP_DONE;
4185 }
4186
4187 if (appctx->st0 == STAT_HTTP_POST) {
4188 if (stats_process_http_post(si))
4189 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004190 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004191 appctx->st0 = STAT_HTTP_DONE;
4192 }
4193
4194 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02004195 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004196 appctx->st0 = STAT_HTTP_DONE;
4197 }
4198
4199 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004200 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
4201 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004202 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004203 }
4204
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004205 if (appctx->st0 == STAT_HTTP_END) {
4206 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004207 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004208 si_shutr(si);
4209 }
4210
4211 /* eat the whole request */
4212 if (co_data(req)) {
4213 req_htx = htx_from_buf(&req->buf);
4214 co_htx_skip(req, req_htx, co_data(req));
4215 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004216 }
4217 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004218
Christopher Fauletef779222018-10-31 08:47:01 +01004219 out:
4220 /* we have left the request in the buffer for the case where we
4221 * process a POST, and this automatically re-enables activity on
4222 * read. It's better to indicate that we want to stop reading when
4223 * we're sending, so that we know there's at most one direction
4224 * deciding to wake the applet up. It saves it from looping when
4225 * emitting large blocks into small TCP windows.
4226 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004227 htx_to_buf(res_htx, &res->buf);
4228 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01004229 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01004230}
4231
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004232/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004233static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004234 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004235{
4236 int field;
4237
4238 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4239 if (!field_format(info, field))
4240 continue;
4241
Willy Tarreaueaa55372019-10-09 07:39:11 +02004242 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004243 return 0;
4244 if (!stats_emit_raw_data_field(out, &info[field]))
4245 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004246 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4247 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004248 if (!chunk_strcat(out, "\n"))
4249 return 0;
4250 }
4251 return 1;
4252}
4253
4254/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004255static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004256 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004257{
4258 int field;
4259
4260 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4261 if (!field_format(info, field))
4262 continue;
4263
Willy Tarreaueaa55372019-10-09 07:39:11 +02004264 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 +01004265 return 0;
4266 if (!stats_emit_field_tags(out, &info[field], ':'))
4267 return 0;
4268 if (!stats_emit_typed_data_field(out, &info[field]))
4269 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004270 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4271 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004272 if (!chunk_strcat(out, "\n"))
4273 return 0;
4274 }
4275 return 1;
4276}
4277
4278/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004279 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004280 * INF_TOTAL_FIELDS. If this length is less then this value, the
4281 * function returns 0, otherwise, it returns 1.
4282 */
4283int stats_fill_info(struct field *info, int len)
4284{
4285 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02004286 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004287
4288#ifdef USE_OPENSSL
4289 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
4290 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
4291 int ssl_reuse = 0;
4292
4293 if (ssl_key_rate < ssl_sess_rate) {
4294 /* count the ssl reuse ratio and avoid overflows in both directions */
4295 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
4296 }
4297#endif
4298
4299 if (len < INF_TOTAL_FIELDS)
4300 return 0;
4301
4302 chunk_reset(out);
4303 memset(info, 0, sizeof(*info) * len);
4304
4305 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004306 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004307 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004308 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004309
Yves Lafon95317282018-02-26 11:10:37 +01004310 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004311 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
4312 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
4313 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4314
4315 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
4316 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
4317
4318 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
William Dauchydefd1562021-01-15 22:41:38 +01004319 info[INF_START_TIME_SEC] = mkf_u32(FN_DURATION, start_date.tv_sec);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004320 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004321 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004322 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004323 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004324 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004325 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004326 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4327 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4328 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4329 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4330 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4331 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4332 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4333 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4334#ifdef USE_OPENSSL
4335 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
4336 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
4337 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
4338#endif
4339 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4340 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4341 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
4342 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
4343 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4344 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
4345 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
4346 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4347 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4348
4349#ifdef USE_OPENSSL
4350 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
4351 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4352 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
4353 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
4354 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
4355 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
4356 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
4357 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4358 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4359 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4360#endif
4361 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
4362 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
4363 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4364#ifdef USE_ZLIB
4365 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4366 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4367#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004368 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004369 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreau81036f22019-05-20 19:24:50 +02004370 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004371 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4372 if (global.desc)
4373 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004374 info[INF_STOPPING] = mkf_u32(0, stopping);
4375 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004376 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004377 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004378 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004379 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004380 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004381 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004382 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004383 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004384 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004385 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 +02004386 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004387 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004388 return 1;
4389}
4390
4391/* This function dumps information onto the stream interface's read buffer.
4392 * It returns 0 as long as it does not complete, non-zero upon completion.
4393 * No state is used.
4394 */
4395static int stats_dump_info_to_buffer(struct stream_interface *si)
4396{
4397 struct appctx *appctx = __objt_appctx(si->end);
4398
4399 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
4400 return 0;
4401
4402 chunk_reset(&trash);
4403
4404 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004405 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01004406 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004407 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004408 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02004409 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004410
Willy Tarreau06d80a92017-10-19 14:32:15 +02004411 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004412 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004413 return 0;
4414 }
4415
4416 return 1;
4417}
4418
Simon Horman6f6bb382017-01-04 09:37:26 +01004419/* This function dumps the schema onto the stream interface's read buffer.
4420 * It returns 0 as long as it does not complete, non-zero upon completion.
4421 * No state is used.
4422 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004423 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004424 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4425 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004426static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004427{
4428
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004429 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004430
4431 chunk_strcat(out,
4432 "{"
4433 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4434 "\"oneOf\":["
4435 "{"
4436 "\"title\":\"Info\","
4437 "\"type\":\"array\","
4438 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004439 "\"title\":\"InfoItem\","
4440 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004441 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004442 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4443 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4444 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4445 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4446 "},"
4447 "\"required\":[\"field\",\"processNum\",\"tags\","
4448 "\"value\"]"
4449 "}"
4450 "},"
4451 "{"
4452 "\"title\":\"Stat\","
4453 "\"type\":\"array\","
4454 "\"items\":{"
4455 "\"title\":\"InfoItem\","
4456 "\"type\":\"object\","
4457 "\"properties\":{"
4458 "\"objType\":{"
4459 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4460 "\"Server\",\"Unknown\"]"
4461 "},"
4462 "\"proxyId\":{"
4463 "\"type\":\"integer\","
4464 "\"minimum\":0"
4465 "},"
4466 "\"id\":{"
4467 "\"type\":\"integer\","
4468 "\"minimum\":0"
4469 "},"
4470 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4471 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4472 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4473 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4474 "},"
4475 "\"required\":[\"objType\",\"proxyId\",\"id\","
4476 "\"field\",\"processNum\",\"tags\","
4477 "\"value\"]"
4478 "}"
4479 "},"
4480 "{"
4481 "\"title\":\"Error\","
4482 "\"type\":\"object\","
4483 "\"properties\":{"
4484 "\"errorStr\":{"
4485 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004486 "}"
4487 "},"
4488 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004489 "}"
4490 "],"
4491 "\"definitions\":{"
4492 "\"field\":{"
4493 "\"type\":\"object\","
4494 "\"pos\":{"
4495 "\"type\":\"integer\","
4496 "\"minimum\":0"
4497 "},"
4498 "\"name\":{"
4499 "\"type\":\"string\""
4500 "},"
4501 "\"required\":[\"pos\",\"name\"]"
4502 "},"
4503 "\"processNum\":{"
4504 "\"type\":\"integer\","
4505 "\"minimum\":1"
4506 "},"
4507 "\"tags\":{"
4508 "\"type\":\"object\","
4509 "\"origin\":{"
4510 "\"type\":\"string\","
4511 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4512 "\"Config\",\"Product\",\"Unknown\"]"
4513 "},"
4514 "\"nature\":{"
4515 "\"type\":\"string\","
4516 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4517 "\"Rate\",\"Counter\",\"Duration\","
4518 "\"Age\",\"Time\",\"Name\",\"Output\","
4519 "\"Avg\", \"Unknown\"]"
4520 "},"
4521 "\"scope\":{"
4522 "\"type\":\"string\","
4523 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4524 "\"System\",\"Unknown\"]"
4525 "},"
4526 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4527 "},"
4528 "\"typedValue\":{"
4529 "\"type\":\"object\","
4530 "\"oneOf\":["
4531 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4532 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4533 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4534 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4535 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4536 "],"
4537 "\"definitions\":{"
4538 "\"s32Value\":{"
4539 "\"properties\":{"
4540 "\"type\":{"
4541 "\"type\":\"string\","
4542 "\"enum\":[\"s32\"]"
4543 "},"
4544 "\"value\":{"
4545 "\"type\":\"integer\","
4546 "\"minimum\":-2147483648,"
4547 "\"maximum\":2147483647"
4548 "}"
4549 "},"
4550 "\"required\":[\"type\",\"value\"]"
4551 "},"
4552 "\"s64Value\":{"
4553 "\"properties\":{"
4554 "\"type\":{"
4555 "\"type\":\"string\","
4556 "\"enum\":[\"s64\"]"
4557 "},"
4558 "\"value\":{"
4559 "\"type\":\"integer\","
4560 "\"minimum\":-9007199254740991,"
4561 "\"maximum\":9007199254740991"
4562 "}"
4563 "},"
4564 "\"required\":[\"type\",\"value\"]"
4565 "},"
4566 "\"u32Value\":{"
4567 "\"properties\":{"
4568 "\"type\":{"
4569 "\"type\":\"string\","
4570 "\"enum\":[\"u32\"]"
4571 "},"
4572 "\"value\":{"
4573 "\"type\":\"integer\","
4574 "\"minimum\":0,"
4575 "\"maximum\":4294967295"
4576 "}"
4577 "},"
4578 "\"required\":[\"type\",\"value\"]"
4579 "},"
4580 "\"u64Value\":{"
4581 "\"properties\":{"
4582 "\"type\":{"
4583 "\"type\":\"string\","
4584 "\"enum\":[\"u64\"]"
4585 "},"
4586 "\"value\":{"
4587 "\"type\":\"integer\","
4588 "\"minimum\":0,"
4589 "\"maximum\":9007199254740991"
4590 "}"
4591 "},"
4592 "\"required\":[\"type\",\"value\"]"
4593 "},"
4594 "\"strValue\":{"
4595 "\"properties\":{"
4596 "\"type\":{"
4597 "\"type\":\"string\","
4598 "\"enum\":[\"str\"]"
4599 "},"
4600 "\"value\":{\"type\":\"string\"}"
4601 "},"
4602 "\"required\":[\"type\",\"value\"]"
4603 "},"
4604 "\"unknownValue\":{"
4605 "\"properties\":{"
4606 "\"type\":{"
4607 "\"type\":\"integer\","
4608 "\"minimum\":0"
4609 "},"
4610 "\"value\":{"
4611 "\"type\":\"string\","
4612 "\"enum\":[\"unknown\"]"
4613 "}"
4614 "},"
4615 "\"required\":[\"type\",\"value\"]"
4616 "}"
4617 "}"
4618 "}"
4619 "}"
4620 "}");
4621
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004622 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004623 chunk_reset(out);
4624 chunk_appendf(out,
4625 "{\"errorStr\":\"output buffer too short\"}");
4626 }
4627}
4628
4629/* This function dumps the schema onto the stream interface's read buffer.
4630 * It returns 0 as long as it does not complete, non-zero upon completion.
4631 * No state is used.
4632 */
4633static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
4634{
4635 chunk_reset(&trash);
4636
4637 stats_dump_json_schema(&trash);
4638
Willy Tarreau06d80a92017-10-19 14:32:15 +02004639 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004640 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01004641 return 0;
4642 }
4643
4644 return 1;
4645}
4646
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004647static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004648{
4649 struct proxy *px;
4650 struct server *sv;
4651 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004652 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004653 int clrall = 0;
4654
4655 if (strcmp(args[2], "all") == 0)
4656 clrall = 1;
4657
4658 /* check permissions */
4659 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4660 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4661 return 1;
4662
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004663 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004664 if (clrall) {
4665 memset(&px->be_counters, 0, sizeof(px->be_counters));
4666 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4667 }
4668 else {
4669 px->be_counters.conn_max = 0;
4670 px->be_counters.p.http.rps_max = 0;
4671 px->be_counters.sps_max = 0;
4672 px->be_counters.cps_max = 0;
4673 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004674 px->be_counters.qtime_max = 0;
4675 px->be_counters.ctime_max = 0;
4676 px->be_counters.dtime_max = 0;
4677 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004678
4679 px->fe_counters.conn_max = 0;
4680 px->fe_counters.p.http.rps_max = 0;
4681 px->fe_counters.sps_max = 0;
4682 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004683 }
4684
4685 for (sv = px->srv; sv; sv = sv->next)
4686 if (clrall)
4687 memset(&sv->counters, 0, sizeof(sv->counters));
4688 else {
4689 sv->counters.cur_sess_max = 0;
4690 sv->counters.nbpend_max = 0;
4691 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004692 sv->counters.qtime_max = 0;
4693 sv->counters.ctime_max = 0;
4694 sv->counters.dtime_max = 0;
4695 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004696 }
4697
4698 list_for_each_entry(li, &px->conf.listeners, by_fe)
4699 if (li->counters) {
4700 if (clrall)
4701 memset(li->counters, 0, sizeof(*li->counters));
4702 else
4703 li->counters->conn_max = 0;
4704 }
4705 }
4706
4707 global.cps_max = 0;
4708 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004709 global.ssl_max = 0;
4710 global.ssl_fe_keys_max = 0;
4711 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004712
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004713 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4714 if (!mod->clearable && !clrall)
4715 continue;
4716
4717 for (px = proxies_list; px; px = px->next) {
4718 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4719
4720 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4721 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4722 mod,
4723 mod->counters,
4724 mod->counters_size);
4725 }
4726
4727 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4728 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4729 mod,
4730 mod->counters,
4731 mod->counters_size);
4732 }
4733
4734 if (mod_cap & STATS_PX_CAP_SRV) {
4735 for (sv = px->srv; sv; sv = sv->next) {
4736 EXTRA_COUNTERS_INIT(sv->extra_counters,
4737 mod,
4738 mod->counters,
4739 mod->counters_size);
4740 }
4741 }
4742
4743 if (mod_cap & STATS_PX_CAP_LI) {
4744 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4745 EXTRA_COUNTERS_INIT(li->extra_counters,
4746 mod,
4747 mod->counters,
4748 mod->counters_size);
4749 }
4750 }
4751 }
4752 }
4753
Emeric Brund3b44952021-01-06 16:01:02 +01004754 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_DNS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004755
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004756 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004757 return 1;
4758}
4759
4760
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004761static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004762{
Willy Tarreau2f397382019-10-09 11:43:59 +02004763 int arg = 2;
4764
Willy Tarreaud25fc792016-12-16 12:33:47 +01004765 appctx->ctx.stats.scope_str = 0;
4766 appctx->ctx.stats.scope_len = 0;
4767 appctx->ctx.stats.flags = 0;
4768
Willy Tarreau2f397382019-10-09 11:43:59 +02004769 while (*args[arg]) {
4770 if (strcmp(args[arg], "typed") == 0)
4771 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4772 else if (strcmp(args[arg], "json") == 0)
4773 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4774 else if (strcmp(args[arg], "desc") == 0)
4775 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4776 arg++;
4777 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004778 return 0;
4779}
4780
4781
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004782static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004783{
Willy Tarreau2f397382019-10-09 11:43:59 +02004784 int arg = 2;
4785
Willy Tarreaud25fc792016-12-16 12:33:47 +01004786 appctx->ctx.stats.scope_str = 0;
4787 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004788 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4789
4790 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
4791 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004792
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004793 /* proxy is the default domain */
4794 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004795 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004796 ++args;
4797
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004798 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004799 ++args;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004800 } else if (strcmp(args[arg], "dns") == 0) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004801 appctx->ctx.stats.domain = STATS_DOMAIN_DNS;
4802 ++args;
4803 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004804 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004805 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004806 }
4807
4808 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4809 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004810 struct proxy *px;
4811
Willy Tarreau2f397382019-10-09 11:43:59 +02004812 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004813 if (px)
4814 appctx->ctx.stats.iid = px->uuid;
4815 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004816 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004817
Willy Tarreau9d008692019-08-09 11:21:01 +02004818 if (!appctx->ctx.stats.iid)
4819 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004820
Willy Tarreau2b812e22016-11-22 16:18:05 +01004821 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004822 appctx->ctx.stats.type = atoi(args[arg+1]);
4823 appctx->ctx.stats.sid = atoi(args[arg+2]);
4824 arg += 3;
4825 }
4826
4827 while (*args[arg]) {
4828 if (strcmp(args[arg], "typed") == 0)
4829 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4830 else if (strcmp(args[arg], "json") == 0)
4831 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4832 else if (strcmp(args[arg], "desc") == 0)
4833 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02004834 else if (strcmp(args[arg], "no-maint") == 0)
4835 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02004836 else if (strcmp(args[arg], "up") == 0)
4837 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02004838 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004839 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004840
Willy Tarreau2b812e22016-11-22 16:18:05 +01004841 return 0;
4842}
4843
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004844static int cli_io_handler_dump_info(struct appctx *appctx)
4845{
4846 return stats_dump_info_to_buffer(appctx->owner);
4847}
4848
Willy Tarreau2b812e22016-11-22 16:18:05 +01004849/* This I/O handler runs as an applet embedded in a stream interface. It is
4850 * used to send raw stats over a socket.
4851 */
4852static int cli_io_handler_dump_stat(struct appctx *appctx)
4853{
Christopher Fauletef779222018-10-31 08:47:01 +01004854 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004855}
4856
Simon Horman6f6bb382017-01-04 09:37:26 +01004857static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4858{
4859 return stats_dump_json_schema_to_buffer(appctx->owner);
4860}
4861
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01004862int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
4863 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004864{
4865 struct stats_module *mod;
4866
4867 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
4868
4869 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4870 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4871 continue;
4872
4873 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
4874 }
4875
4876 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
4877
4878 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4879 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4880 continue;
4881
4882 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
4883 }
4884
4885 return 1;
4886
4887 alloc_failed:
4888 return 0;
4889}
4890
4891/* Initialize and allocate all extra counters for a proxy and its attached
4892 * servers/listeners with all already registered stats module
4893 */
4894int stats_allocate_proxy_counters(struct proxy *px)
4895{
4896 struct server *sv;
4897 struct listener *li;
4898
4899 if (px->cap & PR_CAP_FE) {
4900 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
4901 COUNTERS_FE,
4902 STATS_PX_CAP_FE)) {
4903 return 0;
4904 }
4905 }
4906
4907 if (px->cap & PR_CAP_BE) {
4908 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
4909 COUNTERS_BE,
4910 STATS_PX_CAP_BE)) {
4911 return 0;
4912 }
4913 }
4914
4915 for (sv = px->srv; sv; sv = sv->next) {
4916 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
4917 COUNTERS_SV,
4918 STATS_PX_CAP_SRV)) {
4919 return 0;
4920 }
4921 }
4922
4923 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4924 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
4925 COUNTERS_LI,
4926 STATS_PX_CAP_LI)) {
4927 return 0;
4928 }
4929 }
4930
4931 return 1;
4932}
4933
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004934void stats_register_module(struct stats_module *m)
4935{
4936 const uint8_t domain = stats_get_domain(m->domain_flags);
4937
4938 LIST_ADDQ(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004939 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004940}
4941
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004942static int allocate_stats_px_postcheck(void)
4943{
4944 struct stats_module *mod;
4945 size_t i = ST_F_TOTAL_FIELDS;
4946 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004947 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004948
4949 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
4950
4951 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
4952 if (!stat_f[STATS_DOMAIN_PROXY]) {
4953 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
4954 err_code |= ERR_ALERT | ERR_FATAL;
4955 return err_code;
4956 }
4957
4958 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
4959 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
4960
4961 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4962 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
4963 mod->stats,
4964 mod->stats_count * sizeof(struct name_desc));
4965 i += mod->stats_count;
4966 }
4967
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004968 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004969 if (!stats_allocate_proxy_counters(px)) {
4970 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
4971 err_code |= ERR_ALERT | ERR_FATAL;
4972 return err_code;
4973 }
4974 }
4975
Christopher Fauletde79cd22021-01-06 07:41:56 +01004976 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004977
4978 return err_code;
4979}
4980
4981REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
4982
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004983static int allocate_stats_dns_postcheck(void)
4984{
4985 struct stats_module *mod;
4986 size_t i = 0;
4987 int err_code = 0;
4988
4989 stat_f[STATS_DOMAIN_DNS] = malloc(stat_count[STATS_DOMAIN_DNS] * sizeof(struct name_desc));
4990 if (!stat_f[STATS_DOMAIN_DNS]) {
4991 ha_alert("stats: cannot allocate all fields for dns statistics\n");
4992 err_code |= ERR_ALERT | ERR_FATAL;
4993 return err_code;
4994 }
4995
4996 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_DNS], list) {
4997 memcpy(stat_f[STATS_DOMAIN_DNS] + i,
4998 mod->stats,
4999 mod->stats_count * sizeof(struct name_desc));
5000 i += mod->stats_count;
5001 }
5002
Emeric Brund3b44952021-01-06 16:01:02 +01005003 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_DNS])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005004 ha_alert("stats: cannot allocate all counters for dns statistics\n");
5005 err_code |= ERR_ALERT | ERR_FATAL;
5006 return err_code;
5007 }
5008
Christopher Fauletde79cd22021-01-06 07:41:56 +01005009 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005010
5011 return err_code;
5012}
5013
5014REGISTER_CONFIG_POSTPARSER("allocate-stats-dns", allocate_stats_dns_postcheck);
5015
Christopher Fauletde79cd22021-01-06 07:41:56 +01005016static int allocate_stat_lines_per_thread(void)
5017{
5018 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5019
5020 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5021 const int domain = domains[i];
5022
5023 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5024 if (!stat_l[domain])
5025 return 0;
5026 }
5027 return 1;
5028}
5029
5030REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5031
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005032static int allocate_trash_counters(void)
5033{
5034 struct stats_module *mod;
5035 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5036 size_t max_counters_size = 0;
5037
5038 /* calculate the greatest counters used by any stats modules */
5039 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5040 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5041 max_counters_size = mod->counters_size > max_counters_size ?
5042 mod->counters_size : max_counters_size;
5043 }
5044 }
5045
5046 /* allocate the trash with the size of the greatest counters */
5047 if (max_counters_size) {
5048 trash_counters = malloc(max_counters_size);
5049 if (!trash_counters) {
5050 ha_alert("stats: cannot allocate trash counters for statistics\n");
5051 return 0;
5052 }
5053 }
5054
5055 return 1;
5056}
5057
5058REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5059
Christopher Fauletde79cd22021-01-06 07:41:56 +01005060static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005061{
5062 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5063
5064 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5065 const int domain = domains[i];
5066
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005067 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005068 }
5069}
5070
5071
5072REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5073
5074static void deinit_stats(void)
5075{
5076 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5077
5078 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5079 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005080
5081 if (stat_f[domain])
5082 free(stat_f[domain]);
5083 }
5084}
5085
5086REGISTER_POST_DEINIT(deinit_stats);
5087
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005088static void free_trash_counters(void)
5089{
5090 if (trash_counters)
5091 free(trash_counters);
5092}
5093
5094REGISTER_PER_THREAD_FREE(free_trash_counters);
5095
Willy Tarreau2b812e22016-11-22 16:18:05 +01005096/* register cli keywords */
5097static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01005098 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreaueaa55372019-10-09 07:39:11 +02005099 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreau3e320362020-10-23 17:28:57 +02005100 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server [desc|json|no-maint|typed|up]*", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01005101 { { "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 +01005102 {{},}
5103}};
5104
Willy Tarreau0108d902018-11-25 19:14:37 +01005105INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5106
William Lallemand74c24fb2016-11-21 17:18:36 +01005107struct applet http_stats_applet = {
5108 .obj_type = OBJ_TYPE_APPLET,
5109 .name = "<STATS>", /* used for logging */
5110 .fct = http_stats_io_handler,
5111 .release = NULL,
5112};
5113
William Lallemand74c24fb2016-11-21 17:18:36 +01005114/*
5115 * Local variables:
5116 * c-indent-level: 8
5117 * c-basic-offset: 8
5118 * End:
5119 */