blob: b2bf8b2ab6ac88333d488836577648f90534aa10 [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" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200156 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100157};
158
Willy Tarreaueaa55372019-10-09 07:39:11 +0200159const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200160 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
161 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100162 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
163 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100164 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
165 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200166 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
167 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
168 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
169 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
170 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
171 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
172 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
173 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
174 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
175 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
176 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
177 [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 +0200178 [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 +0200179 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
180 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
181 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
182 [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" },
183 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
184 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
185 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
186 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
187 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
188 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
189 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
190 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
191 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
192 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
193 [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)" },
194 [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 +0100195 [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 +0200196 [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" },
197 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
198 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
199 [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" },
200 [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" },
201 [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" },
202 [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" },
203 [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" },
204 [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)" },
205 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
206 [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 +0100207 [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 +0200208 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
209 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
210 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
211 [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" },
212 [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" },
213 [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)" },
214 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
215 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
216 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
217 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
218 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
219 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
220 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
221 [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)" },
222 [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" },
223 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
224 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
225 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
226 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
227 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
228 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
229 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
230 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
231 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
232 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
233 [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" },
234 [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" },
235 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
236 [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" },
237 [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 +0100238 [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 +0200239 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
240 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
241 [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" },
242 [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" },
243 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
244 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
245 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
246 [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" },
247 [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" },
248 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
249 [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 +0100250 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
251 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
252 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
253 [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 +0100254 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200255 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
256 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
257 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200258 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200259 [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 +0100260};
261
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100262/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100263THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200264
265/* description of statistics (static and dynamic) */
266static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
267static size_t stat_count[STATS_DOMAIN_COUNT];
268
269/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100270THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100271
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200272/* list of all registered stats module */
273static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
274 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200275 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_DNS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200276};
277
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100278THREAD_LOCAL void *trash_counters;
279
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200280static inline uint8_t stats_get_domain(uint32_t domain)
281{
282 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
283}
284
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200285static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
286{
287 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
288}
289
Christopher Faulet6338a082019-09-09 15:50:54 +0200290static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100291
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200292int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100293{
294 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100295 if (chk->data >= channel_htx_recv_max(chn, htx))
296 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200297 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100298 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100299 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100300 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100301 }
302 else {
303 if (ci_putchk(chn, chk) == -1)
304 return 0;
305 }
306 return 1;
307}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100308
Christopher Fauleted7a0662019-01-14 11:07:34 +0100309static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
310{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200311 struct channel *req = si_oc(si);
312 struct htx *htx = htxbuf(&req->buf);
313 struct htx_blk *blk;
314 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100315
Christopher Fauletb7f88902019-07-15 21:56:43 +0200316 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100317 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200318 ALREADY_CHECKED(blk);
319 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
320 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100321}
322
William Lallemand74c24fb2016-11-21 17:18:36 +0100323/*
324 * http_stats_io_handler()
325 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
326 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100327 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100328 * -> stats_dump_html_head() // emits the HTML headers
329 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
330 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
331 * -> stats_dump_html_px_hdr()
332 * -> stats_dump_fe_stats()
333 * -> stats_dump_li_stats()
334 * -> stats_dump_sv_stats()
335 * -> stats_dump_be_stats()
336 * -> stats_dump_html_px_end()
337 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100338 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100339 */
340
341
William Lallemand74c24fb2016-11-21 17:18:36 +0100342/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
343 * for clearing it if needed.
344 * NOTE: Some tools happen to rely on the field position instead of its name,
345 * so please only append new fields at the end, never in the middle.
346 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200347static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100348{
349 int field;
350
351 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200352 if (stat_f[domain]) {
353 for (field = 0; field < stat_count[domain]; ++field) {
354 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100355
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200356 /* print special delimiter on proxy stats to mark end of
357 static fields */
358 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
359 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200360 }
361 }
362
William Lallemand74c24fb2016-11-21 17:18:36 +0100363 chunk_appendf(&trash, "\n");
364}
365
William Lallemand74c24fb2016-11-21 17:18:36 +0100366/* Emits a stats field without any surrounding element and properly encoded to
367 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
368 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200369int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100370{
371 switch (field_format(f, 0)) {
372 case FF_EMPTY: return 1;
373 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
374 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
375 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
376 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200377 case FF_FLT: {
378 size_t prev_data = out->data;
379 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
380 return out->data;
381 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100382 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
383 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
384 }
385}
386
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200387const char *field_to_html_str(const struct field *f)
388{
389 switch (field_format(f, 0)) {
390 case FF_S32: return U2H(f->u.s32);
391 case FF_S64: return U2H(f->u.s64);
392 case FF_U64: return U2H(f->u.u64);
393 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200394 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200395 case FF_STR: return field_str(f, 0);
396 case FF_EMPTY:
397 default:
398 return "";
399 }
400}
401
William Lallemand74c24fb2016-11-21 17:18:36 +0100402/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
403 * output is supposed to be used on its own line. Returns non-zero on success, 0
404 * if the buffer is full.
405 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200406int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100407{
408 switch (field_format(f, 0)) {
409 case FF_EMPTY: return 1;
410 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
411 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
412 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
413 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200414 case FF_FLT: {
415 size_t prev_data = out->data;
416 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
417 return out->data;
418 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100419 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
420 default: return chunk_appendf(out, "%08x:?", f->type);
421 }
422}
423
Simon Horman05ee2132017-01-04 09:37:25 +0100424/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
425 * the recommendation for interoperable integers in section 6 of RFC 7159.
426 */
427#define JSON_INT_MAX ((1ULL << 53) - 1)
428#define JSON_INT_MIN (0 - JSON_INT_MAX)
429
430/* Emits a stats field value and its type in JSON.
431 * Returns non-zero on success, 0 on error.
432 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200433int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100434{
435 int old_len;
436 char buf[20];
437 const char *type, *value = buf, *quote = "";
438
439 switch (field_format(f, 0)) {
440 case FF_EMPTY: return 1;
441 case FF_S32: type = "\"s32\"";
442 snprintf(buf, sizeof(buf), "%d", f->u.s32);
443 break;
444 case FF_U32: type = "\"u32\"";
445 snprintf(buf, sizeof(buf), "%u", f->u.u32);
446 break;
447 case FF_S64: type = "\"s64\"";
448 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
449 return 0;
450 type = "\"s64\"";
451 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
452 break;
453 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
454 return 0;
455 type = "\"u64\"";
456 snprintf(buf, sizeof(buf), "%llu",
457 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200458 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200459 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200460 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100461 break;
462 case FF_STR: type = "\"str\"";
463 value = field_str(f, 0);
464 quote = "\"";
465 break;
466 default: snprintf(buf, sizeof(buf), "%u", f->type);
467 type = buf;
468 value = "unknown";
469 quote = "\"";
470 break;
471 }
472
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200473 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100474 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
475 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200476 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100477}
478
William Lallemand74c24fb2016-11-21 17:18:36 +0100479/* Emits an encoding of the field type on 3 characters followed by a delimiter.
480 * Returns non-zero on success, 0 if the buffer is full.
481 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200482int stats_emit_field_tags(struct buffer *out, const struct field *f,
483 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100484{
485 char origin, nature, scope;
486
487 switch (field_origin(f, 0)) {
488 case FO_METRIC: origin = 'M'; break;
489 case FO_STATUS: origin = 'S'; break;
490 case FO_KEY: origin = 'K'; break;
491 case FO_CONFIG: origin = 'C'; break;
492 case FO_PRODUCT: origin = 'P'; break;
493 default: origin = '?'; break;
494 }
495
496 switch (field_nature(f, 0)) {
497 case FN_GAUGE: nature = 'G'; break;
498 case FN_LIMIT: nature = 'L'; break;
499 case FN_MIN: nature = 'm'; break;
500 case FN_MAX: nature = 'M'; break;
501 case FN_RATE: nature = 'R'; break;
502 case FN_COUNTER: nature = 'C'; break;
503 case FN_DURATION: nature = 'D'; break;
504 case FN_AGE: nature = 'A'; break;
505 case FN_TIME: nature = 'T'; break;
506 case FN_NAME: nature = 'N'; break;
507 case FN_OUTPUT: nature = 'O'; break;
508 case FN_AVG: nature = 'a'; break;
509 default: nature = '?'; break;
510 }
511
512 switch (field_scope(f, 0)) {
513 case FS_PROCESS: scope = 'P'; break;
514 case FS_SERVICE: scope = 'S'; break;
515 case FS_SYSTEM: scope = 's'; break;
516 case FS_CLUSTER: scope = 'C'; break;
517 default: scope = '?'; break;
518 }
519
520 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
521}
522
Simon Horman05ee2132017-01-04 09:37:25 +0100523/* Emits an encoding of the field type as JSON.
524 * Returns non-zero on success, 0 if the buffer is full.
525 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200526int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100527{
528 const char *origin, *nature, *scope;
529 int old_len;
530
531 switch (field_origin(f, 0)) {
532 case FO_METRIC: origin = "Metric"; break;
533 case FO_STATUS: origin = "Status"; break;
534 case FO_KEY: origin = "Key"; break;
535 case FO_CONFIG: origin = "Config"; break;
536 case FO_PRODUCT: origin = "Product"; break;
537 default: origin = "Unknown"; break;
538 }
539
540 switch (field_nature(f, 0)) {
541 case FN_GAUGE: nature = "Gauge"; break;
542 case FN_LIMIT: nature = "Limit"; break;
543 case FN_MIN: nature = "Min"; break;
544 case FN_MAX: nature = "Max"; break;
545 case FN_RATE: nature = "Rate"; break;
546 case FN_COUNTER: nature = "Counter"; break;
547 case FN_DURATION: nature = "Duration"; break;
548 case FN_AGE: nature = "Age"; break;
549 case FN_TIME: nature = "Time"; break;
550 case FN_NAME: nature = "Name"; break;
551 case FN_OUTPUT: nature = "Output"; break;
552 case FN_AVG: nature = "Avg"; break;
553 default: nature = "Unknown"; break;
554 }
555
556 switch (field_scope(f, 0)) {
557 case FS_PROCESS: scope = "Process"; break;
558 case FS_SERVICE: scope = "Service"; break;
559 case FS_SYSTEM: scope = "System"; break;
560 case FS_CLUSTER: scope = "Cluster"; break;
561 default: scope = "Unknown"; break;
562 }
563
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200564 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100565 chunk_appendf(out, "\"tags\":{"
566 "\"origin\":\"%s\","
567 "\"nature\":\"%s\","
568 "\"scope\":\"%s\""
569 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200570 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100571}
William Lallemand74c24fb2016-11-21 17:18:36 +0100572
573/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200574static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200575 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200576 unsigned int flags,
577 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100578{
579 int field;
580
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200581 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100582 if (!stats_emit_raw_data_field(out, &stats[field]))
583 return 0;
584 if (!chunk_strcat(out, ","))
585 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200586
587 /* print special delimiter on proxy stats to mark end of
588 static fields */
589 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
590 if (!chunk_strcat(out, "-,"))
591 return 0;
592 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100593 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200594
William Lallemand74c24fb2016-11-21 17:18:36 +0100595 chunk_strcat(out, "\n");
596 return 1;
597}
598
599/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200600static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200601 const struct field *stats,
602 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200603 unsigned int flags,
604 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100605{
606 int field;
607
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200608 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100609 if (!stats[field].type)
610 continue;
611
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200612 switch (domain) {
613 case STATS_DOMAIN_PROXY:
614 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
615 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
616 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
617 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
618 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
619 '?',
620 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
621 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200622 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200623 stats[ST_F_PID].u.u32);
624 break;
625
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200626 case STATS_DOMAIN_DNS:
627 chunk_appendf(out, "D.%d.%s:", field,
628 stat_f[domain][field].name);
629 break;
630
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200631 default:
632 break;
633 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100634
635 if (!stats_emit_field_tags(out, &stats[field], ':'))
636 return 0;
637 if (!stats_emit_typed_data_field(out, &stats[field]))
638 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200639
640 if (flags & STAT_SHOW_FDESC
641 && !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].name)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200642 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200643 }
644
William Lallemand74c24fb2016-11-21 17:18:36 +0100645 if (!chunk_strcat(out, "\n"))
646 return 0;
647 }
648 return 1;
649}
650
Simon Horman05ee2132017-01-04 09:37:25 +0100651/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200652static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200653 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100654{
655 int field;
656 int started = 0;
657
658 if (!chunk_strcat(out, "["))
659 return 0;
660
661 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
662 int old_len;
663
664 if (!field_format(info, field))
665 continue;
666
667 if (started && !chunk_strcat(out, ","))
668 goto err;
669 started = 1;
670
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200671 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100672 chunk_appendf(out,
673 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
674 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200675 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100676 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200677 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100678 goto err;
679
680 if (!stats_emit_json_field_tags(out, &info[field]))
681 goto err;
682
683 if (!stats_emit_json_data_field(out, &info[field]))
684 goto err;
685
686 if (!chunk_strcat(out, "}"))
687 goto err;
688 }
689
690 if (!chunk_strcat(out, "]"))
691 goto err;
692 return 1;
693
694err:
695 chunk_reset(out);
696 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
697 return 0;
698}
699
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200700static void stats_print_proxy_field_json(struct buffer *out,
701 const struct field *stat,
702 const char *name,
703 int pos,
704 uint32_t field_type,
705 uint32_t iid,
706 uint32_t sid,
707 uint32_t pid)
708{
709 const char *obj_type;
710 switch (field_type) {
711 case STATS_TYPE_FE: obj_type = "Frontend"; break;
712 case STATS_TYPE_BE: obj_type = "Backend"; break;
713 case STATS_TYPE_SO: obj_type = "Listener"; break;
714 case STATS_TYPE_SV: obj_type = "Server"; break;
715 default: obj_type = "Unknown"; break;
716 }
717
718 chunk_appendf(out,
719 "{"
720 "\"objType\":\"%s\","
721 "\"proxyId\":%u,"
722 "\"id\":%u,"
723 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
724 "\"processNum\":%u,",
725 obj_type, iid, sid, pos, name, pid);
726}
727
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200728static void stats_print_dns_field_json(struct buffer *out,
729 const struct field *stat,
730 const char *name,
731 int pos)
732{
733 chunk_appendf(out,
734 "{"
735 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
736 pos, name);
737}
738
739
Simon Horman05ee2132017-01-04 09:37:25 +0100740/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200741static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200742 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200743 unsigned int flags,
744 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100745{
746 int field;
747 int started = 0;
748
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200749 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100750 return 0;
751 if (!chunk_strcat(out, "["))
752 return 0;
753
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200754 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100755 int old_len;
756
757 if (!stats[field].type)
758 continue;
759
760 if (started && !chunk_strcat(out, ","))
761 goto err;
762 started = 1;
763
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200764 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200765 if (domain == STATS_DOMAIN_PROXY) {
766 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200767 stat_f[domain][field].name,
768 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200769 stats[ST_F_TYPE].u.u32,
770 stats[ST_F_IID].u.u32,
771 stats[ST_F_SID].u.u32,
772 stats[ST_F_PID].u.u32);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200773 } else if (domain == STATS_DOMAIN_DNS) {
774 stats_print_dns_field_json(out, &stats[field],
775 stat_f[domain][field].name,
776 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200777 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200778
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200779 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100780 goto err;
781
782 if (!stats_emit_json_field_tags(out, &stats[field]))
783 goto err;
784
785 if (!stats_emit_json_data_field(out, &stats[field]))
786 goto err;
787
788 if (!chunk_strcat(out, "}"))
789 goto err;
790 }
791
792 if (!chunk_strcat(out, "]"))
793 goto err;
794
795 return 1;
796
797err:
798 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200799 if (flags & STAT_STARTED)
800 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100801 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
802 return 0;
803}
804
William Lallemand74c24fb2016-11-21 17:18:36 +0100805/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200806 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200807 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
808 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
809 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100810 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200811static int stats_dump_fields_html(struct buffer *out,
812 const struct field *stats,
813 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100814{
Willy Tarreau83061a82018-07-13 11:56:34 +0200815 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200816 struct stats_module *mod;
817 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100818
819 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
820 chunk_appendf(out,
821 /* name, queue */
822 "<tr class=\"frontend\">");
823
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200824 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100825 /* Column sub-heading for Enable or Disable server */
826 chunk_appendf(out, "<td></td>");
827 }
828
829 chunk_appendf(out,
830 "<td class=ac>"
831 "<a name=\"%s/Frontend\"></a>"
832 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
833 "<td colspan=3></td>"
834 "",
835 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
836
837 chunk_appendf(out,
838 /* sessions rate : current */
839 "<td><u>%s<div class=tips><table class=det>"
840 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
841 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
842 "",
843 U2H(stats[ST_F_RATE].u.u32),
844 U2H(stats[ST_F_CONN_RATE].u.u32),
845 U2H(stats[ST_F_RATE].u.u32));
846
847 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
848 chunk_appendf(out,
849 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
850 U2H(stats[ST_F_REQ_RATE].u.u32));
851
852 chunk_appendf(out,
853 "</table></div></u></td>"
854 /* sessions rate : max */
855 "<td><u>%s<div class=tips><table class=det>"
856 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
857 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
858 "",
859 U2H(stats[ST_F_RATE_MAX].u.u32),
860 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
861 U2H(stats[ST_F_RATE_MAX].u.u32));
862
863 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
864 chunk_appendf(out,
865 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
866 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
867
868 chunk_appendf(out,
869 "</table></div></u></td>"
870 /* sessions rate : limit */
871 "<td>%s</td>",
872 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
873
874 chunk_appendf(out,
875 /* sessions: current, max, limit, total */
876 "<td>%s</td><td>%s</td><td>%s</td>"
877 "<td><u>%s<div class=tips><table class=det>"
878 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
879 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
880 "",
881 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
882 U2H(stats[ST_F_STOT].u.u64),
883 U2H(stats[ST_F_CONN_TOT].u.u64),
884 U2H(stats[ST_F_STOT].u.u64));
885
886 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
887 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
888 chunk_appendf(out,
889 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
890 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
891 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
892 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
893 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
894 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
895 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
896 "<tr><th>- other responses:</th><td>%s</td></tr>"
897 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100898 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
899 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200900 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100901 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100902 "",
903 U2H(stats[ST_F_REQ_TOT].u.u64),
904 U2H(stats[ST_F_HRSP_1XX].u.u64),
905 U2H(stats[ST_F_HRSP_2XX].u.u64),
906 U2H(stats[ST_F_COMP_RSP].u.u64),
907 stats[ST_F_HRSP_2XX].u.u64 ?
908 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
909 U2H(stats[ST_F_HRSP_3XX].u.u64),
910 U2H(stats[ST_F_HRSP_4XX].u.u64),
911 U2H(stats[ST_F_HRSP_5XX].u.u64),
912 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200913 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100914 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
915 U2H(stats[ST_F_CACHE_HITS].u.u64),
916 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
917 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100918 U2H(stats[ST_F_WREW].u.u64),
919 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100920 }
921
922 chunk_appendf(out,
923 "</table></div></u></td>"
924 /* sessions: lbtot, lastsess */
925 "<td></td><td></td>"
926 /* bytes : in */
927 "<td>%s</td>"
928 "",
929 U2H(stats[ST_F_BIN].u.u64));
930
931 chunk_appendf(out,
932 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
933 "<td>%s%s<div class=tips><table class=det>"
934 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
935 "<tr><th>Compression in:</th><td>%s</td></tr>"
936 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
937 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
938 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
939 "</table></div>%s</td>",
940 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
941 U2H(stats[ST_F_BOUT].u.u64),
942 U2H(stats[ST_F_BOUT].u.u64),
943 U2H(stats[ST_F_COMP_IN].u.u64),
944 U2H(stats[ST_F_COMP_OUT].u.u64),
945 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
946 U2H(stats[ST_F_COMP_BYP].u.u64),
947 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
948 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,
949 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
950
951 chunk_appendf(out,
952 /* denied: req, resp */
953 "<td>%s</td><td>%s</td>"
954 /* errors : request, connect, response */
955 "<td>%s</td><td></td><td></td>"
956 /* warnings: retries, redispatches */
957 "<td></td><td></td>"
958 /* server status : reflect frontend status */
959 "<td class=ac>%s</td>"
960 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200961 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100962 "",
963 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
964 U2H(stats[ST_F_EREQ].u.u64),
965 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200966
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200967 if (flags & STAT_SHMODULES) {
968 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100969 chunk_appendf(out, "<td>");
970
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200971 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100972 chunk_appendf(out,
973 "<u>%s<div class=tips><table class=det>",
974 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200975 for (j = 0; j < mod->stats_count; ++j) {
976 chunk_appendf(out,
977 "<tr><th>%s</th><td>%s</td></tr>",
978 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
979 ++i;
980 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100981 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200982 } else {
983 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200984 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200985
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100986 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200987 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200988 }
989
990 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100991 }
992 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
993 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200994 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100995 /* Column sub-heading for Enable or Disable server */
996 chunk_appendf(out, "<td></td>");
997 }
998
999 chunk_appendf(out,
1000 /* frontend name, listener name */
1001 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1002 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1003 "",
1004 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001005 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001006 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1007
Willy Tarreau708c4162019-10-09 10:19:16 +02001008 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001009 chunk_appendf(out, "<div class=tips>");
1010
Willy Tarreau90807112020-02-25 08:16:33 +01001011 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001012 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1013 else if (*field_str(stats, ST_F_ADDR) == '[')
1014 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1015 else if (*field_str(stats, ST_F_ADDR))
1016 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1017
1018 /* id */
1019 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1020 }
1021
1022 chunk_appendf(out,
1023 /* queue */
1024 "%s</td><td colspan=3></td>"
1025 /* sessions rate: current, max, limit */
1026 "<td colspan=3>&nbsp;</td>"
1027 /* sessions: current, max, limit, total, lbtot, lastsess */
1028 "<td>%s</td><td>%s</td><td>%s</td>"
1029 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1030 /* bytes: in, out */
1031 "<td>%s</td><td>%s</td>"
1032 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001033 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001034 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1035 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1036
1037 chunk_appendf(out,
1038 /* denied: req, resp */
1039 "<td>%s</td><td>%s</td>"
1040 /* errors: request, connect, response */
1041 "<td>%s</td><td></td><td></td>"
1042 /* warnings: retries, redispatches */
1043 "<td></td><td></td>"
1044 /* server status: reflect listener status */
1045 "<td class=ac>%s</td>"
1046 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001047 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001048 "",
1049 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1050 U2H(stats[ST_F_EREQ].u.u64),
1051 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001052
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001053 if (flags & STAT_SHMODULES) {
1054 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001055 chunk_appendf(out, "<td>");
1056
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001057 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001058 chunk_appendf(out,
1059 "<u>%s<div class=tips><table class=det>",
1060 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001061 for (j = 0; j < mod->stats_count; ++j) {
1062 chunk_appendf(out,
1063 "<tr><th>%s</th><td>%s</td></tr>",
1064 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1065 ++i;
1066 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001067 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001068 } else {
1069 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001070 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001071
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001072 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001073 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001074 }
1075
1076 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001077 }
1078 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1079 const char *style;
1080
1081 /* determine the style to use depending on the server's state,
1082 * its health and weight. There isn't a 1-to-1 mapping between
1083 * state and styles for the cases where the server is (still)
1084 * up. The reason is that we don't want to report nolb and
1085 * drain with the same color.
1086 */
1087
1088 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1089 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1090 style = "down";
1091 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001092 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001093 style = "going_up";
1094 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001095 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1096 style = "draining";
1097 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001098 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001099 style = "going_down";
1100 }
1101 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1102 style = "nolb";
1103 }
1104 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1105 style = "no_check";
1106 }
1107 else if (!stats[ST_F_CHKFAIL].type ||
1108 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1109 /* no check or max health = UP */
1110 if (stats[ST_F_WEIGHT].u.u32)
1111 style = "up";
1112 else
1113 style = "draining";
1114 }
1115 else {
1116 style = "going_down";
1117 }
1118
Willy Tarreau7b524852020-08-11 10:26:36 +02001119 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001120 chunk_appendf(out, "<tr class=\"maintain\">");
1121 else
1122 chunk_appendf(out,
1123 "<tr class=\"%s_%s\">",
1124 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1125
1126
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001127 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001128 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001129 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1130 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001131 field_str(stats, ST_F_SVNAME));
1132
1133 chunk_appendf(out,
1134 "<td class=ac><a name=\"%s/%s\"></a>%s"
1135 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1136 "",
1137 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001138 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001139 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1140
Willy Tarreau708c4162019-10-09 10:19:16 +02001141 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001142 chunk_appendf(out, "<div class=tips>");
1143
Willy Tarreau90807112020-02-25 08:16:33 +01001144 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001145 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1146 else if (*field_str(stats, ST_F_ADDR) == '[')
1147 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1148 else if (*field_str(stats, ST_F_ADDR))
1149 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1150
1151 /* id */
1152 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1153
1154 /* cookie */
1155 if (stats[ST_F_COOKIE].type) {
1156 chunk_appendf(out, ", cookie: '");
1157 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1158 chunk_htmlencode(out, &src);
1159 chunk_appendf(out, "'");
1160 }
1161
1162 chunk_appendf(out, "</div>");
1163 }
1164
1165 chunk_appendf(out,
1166 /* queue : current, max, limit */
1167 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1168 /* sessions rate : current, max, limit */
1169 "<td>%s</td><td>%s</td><td></td>"
1170 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001171 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001172 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1173 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1174
1175 chunk_appendf(out,
1176 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001177 "<td><u>%s<div class=tips>"
1178 "<table class=det>"
1179 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001180 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001181 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001182 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1183 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001184 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001185 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1186 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1187 "</table></div></u>"
1188 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001189 "<td><u>%s<div class=tips><table class=det>"
1190 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1191 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001192 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001193 U2H(stats[ST_F_SCUR].u.u32),
1194 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1195 U2H(stats[ST_F_SRV_ICUR].u.u32),
1196 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1197 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001198 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001199
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001200 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1201 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1202 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001203 U2H(stats[ST_F_STOT].u.u64),
1204 U2H(stats[ST_F_STOT].u.u64));
1205
1206 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1207 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001208 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001209 "<tr><th>New connections:</th><td>%s</td></tr>"
1210 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001211 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001212 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1213 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1214 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1215 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1216 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1217 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001218 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001219 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001220 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001221 U2H(stats[ST_F_CONNECT].u.u64),
1222 U2H(stats[ST_F_REUSE].u.u64),
1223 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1224 (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 +02001225 U2H(stats[ST_F_REQ_TOT].u.u64),
1226 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1227 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1228 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1229 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1230 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1231 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1232 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1233 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1234 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1235 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1236 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1237 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001238 U2H(stats[ST_F_WREW].u.u64),
1239 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001240 }
1241
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001242 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1243 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1244 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1245 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1246 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001247 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001248 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1249 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1250 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1251 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001252
1253 chunk_appendf(out,
1254 "</table></div></u></td>"
1255 /* sessions: lbtot, last */
1256 "<td>%s</td><td>%s</td>",
1257 U2H(stats[ST_F_LBTOT].u.u64),
1258 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1259
1260 chunk_appendf(out,
1261 /* bytes : in, out */
1262 "<td>%s</td><td>%s</td>"
1263 /* denied: req, resp */
1264 "<td></td><td>%s</td>"
1265 /* errors : request, connect */
1266 "<td></td><td>%s</td>"
1267 /* errors : response */
1268 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1269 /* warnings: retries, redispatches */
1270 "<td>%lld</td><td>%lld</td>"
1271 "",
1272 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1273 U2H(stats[ST_F_DRESP].u.u64),
1274 U2H(stats[ST_F_ECON].u.u64),
1275 U2H(stats[ST_F_ERESP].u.u64),
1276 (long long)stats[ST_F_CLI_ABRT].u.u64,
1277 (long long)stats[ST_F_SRV_ABRT].u.u64,
1278 (long long)stats[ST_F_WRETR].u.u64,
1279 (long long)stats[ST_F_WREDIS].u.u64);
1280
1281 /* status, last change */
1282 chunk_appendf(out, "<td class=ac>");
1283
1284 /* FIXME!!!!
1285 * LASTCHG should contain the last change for *this* server and must be computed
1286 * properly above, as was done below, ie: this server if maint, otherwise ref server
1287 * if tracking. Note that ref is either local or remote depending on tracking.
1288 */
1289
1290
Willy Tarreau7b524852020-08-11 10:26:36 +02001291 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001292 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1293 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001294 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001295 chunk_strcat(out, "<i>no check</i>");
1296 }
1297 else {
1298 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 +02001299 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001300 if (stats[ST_F_CHECK_HEALTH].u.u32)
1301 chunk_strcat(out, " &uarr;");
1302 }
1303 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1304 chunk_strcat(out, " &darr;");
1305 }
1306
Willy Tarreau7b524852020-08-11 10:26:36 +02001307 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001308 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1309 chunk_appendf(out,
1310 "</td><td class=ac><u> %s",
1311 field_str(stats, ST_F_AGENT_STATUS));
1312
1313 if (stats[ST_F_AGENT_CODE].type)
1314 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1315
1316 if (stats[ST_F_AGENT_DURATION].type)
1317 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1318
1319 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1320
1321 if (*field_str(stats, ST_F_LAST_AGT)) {
1322 chunk_appendf(out, ": ");
1323 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1324 chunk_htmlencode(out, &src);
1325 }
1326 chunk_appendf(out, "</div></u>");
1327 }
1328 else if (stats[ST_F_CHECK_STATUS].type) {
1329 chunk_appendf(out,
1330 "</td><td class=ac><u> %s",
1331 field_str(stats, ST_F_CHECK_STATUS));
1332
1333 if (stats[ST_F_CHECK_CODE].type)
1334 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1335
1336 if (stats[ST_F_CHECK_DURATION].type)
1337 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1338
1339 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1340
1341 if (*field_str(stats, ST_F_LAST_CHK)) {
1342 chunk_appendf(out, ": ");
1343 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1344 chunk_htmlencode(out, &src);
1345 }
1346 chunk_appendf(out, "</div></u>");
1347 }
1348 else
1349 chunk_appendf(out, "</td><td>");
1350
1351 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001352 /* weight / uweight */
1353 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001354 /* act, bck */
1355 "<td class=ac>%s</td><td class=ac>%s</td>"
1356 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001357 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001358 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1359 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1360
1361 /* check failures: unique, fatal, down time */
1362 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1363 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1364 }
1365 else if (stats[ST_F_CHKFAIL].type) {
1366 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1367
1368 if (stats[ST_F_HANAFAIL].type)
1369 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1370
1371 chunk_appendf(out,
1372 "<div class=tips>Failed Health Checks%s</div></u></td>"
1373 "<td>%lld</td><td>%s</td>"
1374 "",
1375 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1376 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1377 }
1378 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1379 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1380 chunk_appendf(out,
1381 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1382 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1383 }
1384 else
1385 chunk_appendf(out, "<td colspan=3></td>");
1386
1387 /* throttle */
1388 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001389 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001390 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001391 chunk_appendf(out, "<td class=ac>-</td>");
1392
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001393 if (flags & STAT_SHMODULES) {
1394 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001395 chunk_appendf(out, "<td>");
1396
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001397 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001398 chunk_appendf(out,
1399 "<u>%s<div class=tips><table class=det>",
1400 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001401 for (j = 0; j < mod->stats_count; ++j) {
1402 chunk_appendf(out,
1403 "<tr><th>%s</th><td>%s</td></tr>",
1404 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1405 ++i;
1406 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001407 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001408 } else {
1409 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001410 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001411
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001412 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001413 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001414 }
1415
1416 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001417 }
1418 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1419 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001420 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001421 /* Column sub-heading for Enable or Disable server */
1422 chunk_appendf(out, "<td></td>");
1423 }
1424 chunk_appendf(out,
1425 "<td class=ac>"
1426 /* name */
1427 "%s<a name=\"%s/Backend\"></a>"
1428 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1429 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001430 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001431 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1432
Willy Tarreau708c4162019-10-09 10:19:16 +02001433 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001434 /* balancing */
1435 chunk_appendf(out, "<div class=tips>balancing: %s",
1436 field_str(stats, ST_F_ALGO));
1437
1438 /* cookie */
1439 if (stats[ST_F_COOKIE].type) {
1440 chunk_appendf(out, ", cookie: '");
1441 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1442 chunk_htmlencode(out, &src);
1443 chunk_appendf(out, "'");
1444 }
1445 chunk_appendf(out, "</div>");
1446 }
1447
1448 chunk_appendf(out,
1449 "%s</td>"
1450 /* queue : current, max */
1451 "<td>%s</td><td>%s</td><td></td>"
1452 /* sessions rate : current, max, limit */
1453 "<td>%s</td><td>%s</td><td></td>"
1454 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001455 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001456 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1457 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1458
1459 chunk_appendf(out,
1460 /* sessions: current, max, limit, total */
1461 "<td>%s</td><td>%s</td><td>%s</td>"
1462 "<td><u>%s<div class=tips><table class=det>"
1463 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1464 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001465 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 +01001466 U2H(stats[ST_F_STOT].u.u64),
1467 U2H(stats[ST_F_STOT].u.u64));
1468
1469 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1470 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1471 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001472 "<tr><th>New connections:</th><td>%s</td></tr>"
1473 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001474 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1475 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1476 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1477 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1478 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1479 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1480 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1481 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001482 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1483 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001484 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001485 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001486 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001487 U2H(stats[ST_F_CONNECT].u.u64),
1488 U2H(stats[ST_F_REUSE].u.u64),
1489 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1490 (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 +01001491 U2H(stats[ST_F_REQ_TOT].u.u64),
1492 U2H(stats[ST_F_HRSP_1XX].u.u64),
1493 U2H(stats[ST_F_HRSP_2XX].u.u64),
1494 U2H(stats[ST_F_COMP_RSP].u.u64),
1495 stats[ST_F_HRSP_2XX].u.u64 ?
1496 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1497 U2H(stats[ST_F_HRSP_3XX].u.u64),
1498 U2H(stats[ST_F_HRSP_4XX].u.u64),
1499 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001500 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001501 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1502 U2H(stats[ST_F_CACHE_HITS].u.u64),
1503 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1504 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001505 U2H(stats[ST_F_WREW].u.u64),
1506 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001507 }
1508
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001509 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1510 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1511 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1512 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1513 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001514 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001515 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1516 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1517 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1518 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001519
1520 chunk_appendf(out,
1521 "</table></div></u></td>"
1522 /* sessions: lbtot, last */
1523 "<td>%s</td><td>%s</td>"
1524 /* bytes: in */
1525 "<td>%s</td>"
1526 "",
1527 U2H(stats[ST_F_LBTOT].u.u64),
1528 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1529 U2H(stats[ST_F_BIN].u.u64));
1530
1531 chunk_appendf(out,
1532 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1533 "<td>%s%s<div class=tips><table class=det>"
1534 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1535 "<tr><th>Compression in:</th><td>%s</td></tr>"
1536 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1537 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1538 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1539 "</table></div>%s</td>",
1540 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1541 U2H(stats[ST_F_BOUT].u.u64),
1542 U2H(stats[ST_F_BOUT].u.u64),
1543 U2H(stats[ST_F_COMP_IN].u.u64),
1544 U2H(stats[ST_F_COMP_OUT].u.u64),
1545 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1546 U2H(stats[ST_F_COMP_BYP].u.u64),
1547 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1548 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,
1549 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1550
1551 chunk_appendf(out,
1552 /* denied: req, resp */
1553 "<td>%s</td><td>%s</td>"
1554 /* errors : request, connect */
1555 "<td></td><td>%s</td>"
1556 /* errors : response */
1557 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1558 /* warnings: retries, redispatches */
1559 "<td>%lld</td><td>%lld</td>"
1560 /* backend status: reflect backend status (up/down): we display UP
1561 * if the backend has known working servers or if it has no server at
1562 * all (eg: for stats). Then we display the total weight, number of
1563 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001564 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001565 "<td class=ac>%d</td><td class=ac>%d</td>"
1566 "",
1567 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1568 U2H(stats[ST_F_ECON].u.u64),
1569 U2H(stats[ST_F_ERESP].u.u64),
1570 (long long)stats[ST_F_CLI_ABRT].u.u64,
1571 (long long)stats[ST_F_SRV_ABRT].u.u64,
1572 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1573 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1574 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 +02001575 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001576 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1577
1578 chunk_appendf(out,
1579 /* rest of backend: nothing, down transitions, total downtime, throttle */
1580 "<td class=ac>&nbsp;</td><td>%d</td>"
1581 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001582 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001583 stats[ST_F_CHKDOWN].u.u32,
1584 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001585
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001586 if (flags & STAT_SHMODULES) {
1587 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001588 chunk_appendf(out, "<td>");
1589
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001590 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001591 chunk_appendf(out,
1592 "<u>%s<div class=tips><table class=det>",
1593 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001594 for (j = 0; j < mod->stats_count; ++j) {
1595 chunk_appendf(out,
1596 "<tr><th>%s</th><td>%s</td></tr>",
1597 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1598 ++i;
1599 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001600 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001601 } else {
1602 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001603 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001604
1605 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001606 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001607 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001608
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001609 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001610 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001611
William Lallemand74c24fb2016-11-21 17:18:36 +01001612 return 1;
1613}
1614
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001615int stats_dump_one_line(const struct field *stats, size_t stats_count,
1616 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001617{
Simon Horman05ee2132017-01-04 09:37:25 +01001618 int ret;
1619
William Lallemand74c24fb2016-11-21 17:18:36 +01001620 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001621 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001622 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001623 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001624 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001625 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001626 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001627 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001628
1629 if (ret)
1630 appctx->ctx.stats.flags |= STAT_STARTED;
1631
1632 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001633}
1634
William Dauchy0ef54392021-01-17 18:27:45 +01001635/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001636 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1637 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1638 * this value, or if the selected field is not implemented for frontends, the
1639 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001640 */
William Dauchy0ef54392021-01-17 18:27:45 +01001641int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1642 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001643{
William Dauchy0ef54392021-01-17 18:27:45 +01001644 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001645
William Lallemand74c24fb2016-11-21 17:18:36 +01001646 if (len < ST_F_TOTAL_FIELDS)
1647 return 0;
1648
William Dauchy0ef54392021-01-17 18:27:45 +01001649 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001650 struct field metric = { 0 };
1651
William Dauchy0ef54392021-01-17 18:27:45 +01001652 switch (current_field) {
1653 case ST_F_PXNAME:
1654 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1655 break;
1656 case ST_F_SVNAME:
1657 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1658 break;
1659 case ST_F_MODE:
1660 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1661 break;
1662 case ST_F_SCUR:
1663 metric = mkf_u32(0, px->feconn);
1664 break;
1665 case ST_F_SMAX:
1666 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1667 break;
1668 case ST_F_SLIM:
1669 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1670 break;
1671 case ST_F_STOT:
1672 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1673 break;
1674 case ST_F_BIN:
1675 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1676 break;
1677 case ST_F_BOUT:
1678 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1679 break;
1680 case ST_F_DREQ:
1681 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1682 break;
1683 case ST_F_DRESP:
1684 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1685 break;
1686 case ST_F_EREQ:
1687 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1688 break;
1689 case ST_F_DCON:
1690 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1691 break;
1692 case ST_F_DSES:
1693 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1694 break;
1695 case ST_F_STATUS:
1696 metric = mkf_str(FO_STATUS, px->disabled ? "STOP" : "OPEN");
1697 break;
1698 case ST_F_PID:
1699 metric = mkf_u32(FO_KEY, relative_pid);
1700 break;
1701 case ST_F_IID:
1702 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1703 break;
1704 case ST_F_SID:
1705 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1706 break;
1707 case ST_F_TYPE:
1708 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1709 break;
1710 case ST_F_RATE:
1711 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1712 break;
1713 case ST_F_RATE_LIM:
1714 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1715 break;
1716 case ST_F_RATE_MAX:
1717 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1718 break;
1719 case ST_F_WREW:
1720 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1721 break;
1722 case ST_F_EINT:
1723 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1724 break;
1725 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001726 if (px->mode == PR_MODE_HTTP)
1727 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001728 break;
1729 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001730 if (px->mode == PR_MODE_HTTP)
1731 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001732 break;
1733 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001734 if (px->mode == PR_MODE_HTTP)
1735 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001736 break;
1737 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001738 if (px->mode == PR_MODE_HTTP)
1739 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001740 break;
1741 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001742 if (px->mode == PR_MODE_HTTP)
1743 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001744 break;
1745 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001746 if (px->mode == PR_MODE_HTTP)
1747 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001748 break;
1749 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001750 if (px->mode == PR_MODE_HTTP)
1751 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001752 break;
1753 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001754 if (px->mode == PR_MODE_HTTP)
1755 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001756 break;
1757 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001758 if (px->mode == PR_MODE_HTTP)
1759 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001760 break;
1761 case ST_F_REQ_RATE:
1762 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1763 break;
1764 case ST_F_REQ_RATE_MAX:
1765 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1766 break;
1767 case ST_F_REQ_TOT:
1768 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1769 break;
1770 case ST_F_COMP_IN:
1771 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1772 break;
1773 case ST_F_COMP_OUT:
1774 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1775 break;
1776 case ST_F_COMP_BYP:
1777 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1778 break;
1779 case ST_F_COMP_RSP:
1780 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1781 break;
1782 case ST_F_CONN_RATE:
1783 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1784 break;
1785 case ST_F_CONN_RATE_MAX:
1786 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1787 break;
1788 case ST_F_CONN_TOT:
1789 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1790 break;
1791 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001792 /* not used for frontends. If a specific metric
1793 * is requested, return an error. Otherwise continue.
1794 */
1795 if (selected_field != NULL)
1796 return 0;
1797 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001798 }
1799 stats[current_field] = metric;
1800 if (selected_field != NULL)
1801 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001802 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001803 return 1;
1804}
1805
1806/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1807 * the state from stream interface <si>. The caller is responsible for clearing
1808 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1809 */
1810static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1811{
1812 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001813 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1814 struct stats_module *mod;
1815 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001816
1817 if (!(px->cap & PR_CAP_FE))
1818 return 0;
1819
1820 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1821 return 0;
1822
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001823 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1824
William Dauchy0ef54392021-01-17 18:27:45 +01001825 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001826 return 0;
1827
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001828 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1829 void *counters;
1830
1831 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1832 stats_count += mod->stats_count;
1833 continue;
1834 }
1835
1836 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1837 mod->fill_stats(counters, stats + stats_count);
1838 stats_count += mod->stats_count;
1839 }
1840
1841 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001842}
1843
William Dauchy655e14e2021-02-14 23:22:54 +01001844/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1845 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1846 * this length is less then this value, the function returns 0, otherwise, it
1847 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1848 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001849 */
1850int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001851 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001852{
William Dauchy655e14e2021-02-14 23:22:54 +01001853 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001854 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001855
1856 if (len < ST_F_TOTAL_FIELDS)
1857 return 0;
1858
1859 if (!l->counters)
1860 return 0;
1861
1862 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001863
William Dauchy655e14e2021-02-14 23:22:54 +01001864 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1865 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001866
William Dauchy655e14e2021-02-14 23:22:54 +01001867 switch (current_field) {
1868 case ST_F_PXNAME:
1869 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1870 break;
1871 case ST_F_SVNAME:
1872 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1873 break;
1874 case ST_F_MODE:
1875 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1876 break;
1877 case ST_F_SCUR:
1878 metric = mkf_u32(0, l->nbconn);
1879 break;
1880 case ST_F_SMAX:
1881 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1882 break;
1883 case ST_F_SLIM:
1884 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1885 break;
1886 case ST_F_STOT:
1887 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1888 break;
1889 case ST_F_BIN:
1890 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1891 break;
1892 case ST_F_BOUT:
1893 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1894 break;
1895 case ST_F_DREQ:
1896 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1897 break;
1898 case ST_F_DRESP:
1899 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1900 break;
1901 case ST_F_EREQ:
1902 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1903 break;
1904 case ST_F_DCON:
1905 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1906 break;
1907 case ST_F_DSES:
1908 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1909 break;
1910 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001911 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001912 break;
1913 case ST_F_PID:
1914 metric = mkf_u32(FO_KEY, relative_pid);
1915 break;
1916 case ST_F_IID:
1917 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1918 break;
1919 case ST_F_SID:
1920 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1921 break;
1922 case ST_F_TYPE:
1923 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1924 break;
1925 case ST_F_WREW:
1926 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1927 break;
1928 case ST_F_EINT:
1929 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1930 break;
1931 case ST_F_ADDR:
1932 if (flags & STAT_SHLGNDS) {
1933 char str[INET6_ADDRSTRLEN];
1934 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001935
William Dauchy655e14e2021-02-14 23:22:54 +01001936 port = get_host_port(&l->rx.addr);
1937 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1938 case AF_INET:
1939 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1940 chunk_appendf(out, "%s:%d", str, port);
1941 break;
1942 case AF_INET6:
1943 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1944 chunk_appendf(out, "[%s]:%d", str, port);
1945 break;
1946 case AF_UNIX:
1947 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1948 break;
1949 case -1:
1950 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1951 chunk_strcat(out, strerror(errno));
1952 break;
1953 default: /* address family not supported */
1954 break;
1955 }
1956 }
1957 break;
1958 default:
1959 /* not used for listen. If a specific metric
1960 * is requested, return an error. Otherwise continue.
1961 */
1962 if (selected_field != NULL)
1963 return 0;
1964 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001965 }
William Dauchy655e14e2021-02-14 23:22:54 +01001966 stats[current_field] = metric;
1967 if (selected_field != NULL)
1968 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001969 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001970 return 1;
1971}
1972
1973/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001974 * from stream interface <si>. The caller is responsible for clearing the trash
1975 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001976 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001977static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001978{
1979 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001980 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1981 struct stats_module *mod;
1982 size_t stats_count = ST_F_TOTAL_FIELDS;
1983
1984 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001985
William Dauchy655e14e2021-02-14 23:22:54 +01001986 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats,
1987 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001988 return 0;
1989
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001990 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1991 void *counters;
1992
1993 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1994 stats_count += mod->stats_count;
1995 continue;
1996 }
1997
1998 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
1999 mod->fill_stats(counters, stats + stats_count);
2000 stats_count += mod->stats_count;
2001 }
2002
2003 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002004}
2005
2006enum srv_stats_state {
2007 SRV_STATS_STATE_DOWN = 0,
2008 SRV_STATS_STATE_DOWN_AGENT,
2009 SRV_STATS_STATE_GOING_UP,
2010 SRV_STATS_STATE_UP_GOING_DOWN,
2011 SRV_STATS_STATE_UP,
2012 SRV_STATS_STATE_NOLB_GOING_DOWN,
2013 SRV_STATS_STATE_NOLB,
2014 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2015 SRV_STATS_STATE_DRAIN,
2016 SRV_STATS_STATE_DRAIN_AGENT,
2017 SRV_STATS_STATE_NO_CHECK,
2018
2019 SRV_STATS_STATE_COUNT, /* Must be last */
2020};
2021
2022static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2023 [SRV_STATS_STATE_DOWN] = "DOWN",
2024 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2025 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2026 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2027 [SRV_STATS_STATE_UP] = "UP",
2028 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2029 [SRV_STATS_STATE_NOLB] = "NOLB",
2030 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2031 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2032 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2033 [SRV_STATS_STATE_NO_CHECK] = "no check"
2034};
2035
William Dauchyd3a9a492021-01-25 17:29:03 +01002036/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002037 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002038static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2039 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002040{
Emeric Brun52a91d32017-08-31 14:41:55 +02002041 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002042 if ((ref->check.state & CHK_ST_ENABLED) &&
2043 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002044 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002045 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002046 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002047 }
2048
Emeric Brun52a91d32017-08-31 14:41:55 +02002049 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002050 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002051 *state = SRV_STATS_STATE_DRAIN_AGENT;
2052 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2053 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002054 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002055 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002056 }
2057
William Dauchyd3a9a492021-01-25 17:29:03 +01002058 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2059 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002060 }
2061 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002062 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002063 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2064 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002065 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002066 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002067 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002068 }
2069 }
2070 else { /* stopped */
2071 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002072 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002073 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002074 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002075 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002076 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002077 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002078 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002079 }
2080 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002081}
William Lallemand74c24fb2016-11-21 17:18:36 +01002082
William Dauchyd3a9a492021-01-25 17:29:03 +01002083/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2084 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2085 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2086 * this value, or if the selected field is not implemented for servers, the
2087 * function returns 0, otherwise, it returns 1. <flags> can take the value
2088 * STAT_SHLGNDS.
2089 */
2090int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2091 struct field *stats, int len,
2092 enum stat_field *selected_field)
2093{
2094 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2095 struct server *via = sv->track ? sv->track : sv;
2096 struct server *ref = via;
2097 enum srv_stats_state state = 0;
2098 char str[INET6_ADDRSTRLEN];
2099 struct buffer *out = get_trash_chunk();
2100 char *fld_status;
2101 long long srv_samples_counter;
2102 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002103
William Dauchyd3a9a492021-01-25 17:29:03 +01002104 if (len < ST_F_TOTAL_FIELDS)
2105 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002106
William Dauchyd3a9a492021-01-25 17:29:03 +01002107 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002108
William Dauchyd3a9a492021-01-25 17:29:03 +01002109 /* compute state for later use */
2110 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2111 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2112 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2113 /* we have "via" which is the tracked server as described in the configuration,
2114 * and "ref" which is the checked server and the end of the chain.
2115 */
2116 while (ref->track)
2117 ref = ref->track;
2118 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002119 }
2120
William Dauchyd3a9a492021-01-25 17:29:03 +01002121 /* compue time values for later use */
2122 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2123 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2124 *selected_field == ST_F_TTIME) {
2125 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2126 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2127 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002128 }
2129
William Dauchyd3a9a492021-01-25 17:29:03 +01002130 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2131 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002132
William Dauchyd3a9a492021-01-25 17:29:03 +01002133 switch (current_field) {
2134 case ST_F_PXNAME:
2135 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2136 break;
2137 case ST_F_SVNAME:
2138 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2139 break;
2140 case ST_F_MODE:
2141 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002142 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002143 case ST_F_QCUR:
William Dauchyd3a9a492021-01-25 17:29:03 +01002144 metric = mkf_u32(0, sv->nbpend);
2145 break;
2146 case ST_F_QMAX:
2147 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2148 break;
2149 case ST_F_SCUR:
2150 metric = mkf_u32(0, sv->cur_sess);
2151 break;
2152 case ST_F_SMAX:
2153 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2154 break;
2155 case ST_F_SLIM:
2156 if (sv->maxconn)
2157 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2158 break;
2159 case ST_F_SRV_ICUR:
2160 metric = mkf_u32(0, sv->curr_idle_conns);
2161 break;
2162 case ST_F_SRV_ILIM:
2163 if (sv->max_idle_conns != -1)
2164 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2165 break;
2166 case ST_F_STOT:
2167 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2168 break;
2169 case ST_F_BIN:
2170 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2171 break;
2172 case ST_F_BOUT:
2173 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2174 break;
2175 case ST_F_DRESP:
2176 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2177 break;
2178 case ST_F_ECON:
2179 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2180 break;
2181 case ST_F_ERESP:
2182 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2183 break;
2184 case ST_F_WRETR:
2185 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2186 break;
2187 case ST_F_WREDIS:
2188 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2189 break;
2190 case ST_F_WREW:
2191 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2192 break;
2193 case ST_F_EINT:
2194 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2195 break;
2196 case ST_F_CONNECT:
2197 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2198 break;
2199 case ST_F_REUSE:
2200 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2201 break;
2202 case ST_F_IDLE_CONN_CUR:
2203 metric = mkf_u32(0, sv->curr_idle_nb);
2204 break;
2205 case ST_F_SAFE_CONN_CUR:
2206 metric = mkf_u32(0, sv->curr_safe_nb);
2207 break;
2208 case ST_F_USED_CONN_CUR:
2209 metric = mkf_u32(0, sv->curr_used_conns);
2210 break;
2211 case ST_F_NEED_CONN_EST:
2212 metric = mkf_u32(0, sv->est_need_conns);
2213 break;
2214 case ST_F_STATUS:
2215 fld_status = chunk_newstr(out);
2216 if (sv->cur_admin & SRV_ADMF_RMAINT)
2217 chunk_appendf(out, "MAINT (resolution)");
2218 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2219 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2220 else if (sv->cur_admin & SRV_ADMF_MAINT)
2221 chunk_appendf(out, "MAINT");
2222 else
2223 chunk_appendf(out,
2224 srv_hlt_st[state],
2225 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2226 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002227
William Dauchyd3a9a492021-01-25 17:29:03 +01002228 metric = mkf_str(FO_STATUS, fld_status);
2229 break;
2230 case ST_F_LASTCHG:
2231 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2232 break;
2233 case ST_F_WEIGHT:
2234 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2235 break;
2236 case ST_F_UWEIGHT:
2237 metric = mkf_u32(FN_AVG, sv->uweight);
2238 break;
2239 case ST_F_ACT:
2240 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2241 break;
2242 case ST_F_BCK:
2243 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2244 break;
2245 case ST_F_CHKFAIL:
2246 if (sv->check.state & CHK_ST_ENABLED)
2247 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2248 break;
2249 case ST_F_CHKDOWN:
2250 if (sv->check.state & CHK_ST_ENABLED)
2251 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2252 break;
2253 case ST_F_DOWNTIME:
2254 if (sv->check.state & CHK_ST_ENABLED)
2255 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2256 break;
2257 case ST_F_QLIMIT:
2258 if (sv->maxqueue)
2259 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2260 break;
2261 case ST_F_PID:
2262 metric = mkf_u32(FO_KEY, relative_pid);
2263 break;
2264 case ST_F_IID:
2265 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2266 break;
2267 case ST_F_SID:
2268 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2269 break;
2270 case ST_F_THROTTLE:
2271 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2272 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2273 break;
2274 case ST_F_LBTOT:
2275 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2276 break;
2277 case ST_F_TRACKED:
2278 if (sv->track) {
2279 char *fld_track = chunk_newstr(out);
2280 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2281 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2282 }
2283 break;
2284 case ST_F_TYPE:
2285 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2286 break;
2287 case ST_F_RATE:
2288 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2289 break;
2290 case ST_F_RATE_MAX:
2291 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2292 break;
2293 case ST_F_CHECK_STATUS:
2294 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2295 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002296
William Dauchyd3a9a492021-01-25 17:29:03 +01002297 fld_chksts = chunk_newstr(out);
2298 chunk_strcat(out, "* "); // for check in progress
2299 chunk_strcat(out, get_check_status_info(sv->check.status));
2300 if (!(sv->check.state & CHK_ST_INPROGRESS))
2301 fld_chksts += 2; // skip "* "
2302 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
2303 }
2304 break;
2305 case ST_F_CHECK_CODE:
2306 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2307 sv->check.status >= HCHK_STATUS_L57DATA)
2308 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2309 break;
2310 case ST_F_CHECK_DURATION:
2311 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2312 sv->check.status >= HCHK_STATUS_CHECKED)
2313 metric = mkf_u64(FN_DURATION, sv->check.duration);
2314 break;
2315 case ST_F_CHECK_DESC:
2316 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2317 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2318 break;
2319 case ST_F_LAST_CHK:
2320 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2321 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2322 break;
2323 case ST_F_CHECK_RISE:
2324 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2325 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2326 break;
2327 case ST_F_CHECK_FALL:
2328 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2329 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2330 break;
2331 case ST_F_CHECK_HEALTH:
2332 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2333 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2334 break;
2335 case ST_F_REQ_TOT:
2336 if (px->mode == PR_MODE_HTTP)
2337 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2338 break;
2339 case ST_F_HRSP_1XX:
2340 if (px->mode == PR_MODE_HTTP)
2341 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2342 break;
2343 case ST_F_HRSP_2XX:
2344 if (px->mode == PR_MODE_HTTP)
2345 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2346 break;
2347 case ST_F_HRSP_3XX:
2348 if (px->mode == PR_MODE_HTTP)
2349 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2350 break;
2351 case ST_F_HRSP_4XX:
2352 if (px->mode == PR_MODE_HTTP)
2353 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2354 break;
2355 case ST_F_HRSP_5XX:
2356 if (px->mode == PR_MODE_HTTP)
2357 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2358 break;
2359 case ST_F_HRSP_OTHER:
2360 if (px->mode == PR_MODE_HTTP)
2361 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2362 break;
2363 case ST_F_HANAFAIL:
2364 if (ref->observe)
2365 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2366 break;
2367 case ST_F_CLI_ABRT:
2368 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2369 break;
2370 case ST_F_SRV_ABRT:
2371 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2372 break;
2373 case ST_F_LASTSESS:
2374 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2375 break;
2376 case ST_F_QTIME:
2377 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2378 break;
2379 case ST_F_CTIME:
2380 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2381 break;
2382 case ST_F_RTIME:
2383 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2384 break;
2385 case ST_F_TTIME:
2386 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2387 break;
2388 case ST_F_QT_MAX:
2389 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2390 break;
2391 case ST_F_CT_MAX:
2392 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2393 break;
2394 case ST_F_RT_MAX:
2395 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2396 break;
2397 case ST_F_TT_MAX:
2398 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2399 break;
2400 case ST_F_ADDR:
2401 if (flags & STAT_SHLGNDS) {
2402 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2403 case AF_INET:
2404 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2405 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2406 break;
2407 case AF_INET6:
2408 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2409 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2410 break;
2411 case AF_UNIX:
2412 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2413 break;
2414 case -1:
2415 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2416 chunk_strcat(out, strerror(errno));
2417 break;
2418 default: /* address family not supported */
2419 break;
2420 }
2421 }
2422 break;
2423 case ST_F_COOKIE:
2424 if (flags & STAT_SHLGNDS && sv->cookie)
2425 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2426 break;
2427 default:
2428 /* not used for servers. If a specific metric
2429 * is requested, return an error. Otherwise continue.
2430 */
2431 if (selected_field != NULL)
2432 return 0;
2433 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002434 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002435 stats[current_field] = metric;
2436 if (selected_field != NULL)
2437 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002438 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002439 return 1;
2440}
2441
2442/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02002443 * from stream interface <si>, and server state <state>. The caller is
2444 * responsible for clearing the trash if needed. Returns non-zero if it emits
2445 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002446 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002447static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002448{
2449 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002450 struct stats_module *mod;
2451 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2452 size_t stats_count = ST_F_TOTAL_FIELDS;
2453
2454 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002455
William Dauchyd3a9a492021-01-25 17:29:03 +01002456 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats,
2457 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002458 return 0;
2459
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002460 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2461 void *counters;
2462
2463 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2464 continue;
2465
2466 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2467 stats_count += mod->stats_count;
2468 continue;
2469 }
2470
2471 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2472 mod->fill_stats(counters, stats + stats_count);
2473 stats_count += mod->stats_count;
2474 }
2475
2476 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002477}
2478
William Dauchyda3b4662021-01-25 17:29:01 +01002479/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002480 */
William Dauchyda3b4662021-01-25 17:29:01 +01002481static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002482{
William Dauchyda3b4662021-01-25 17:29:01 +01002483 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002484 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002485
William Dauchyda3b4662021-01-25 17:29:01 +01002486 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002487 for (srv = px->srv; srv; srv = srv->next) {
2488 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002489 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002490 if (srv_currently_usable(srv) &&
2491 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002492 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002493 }
William Dauchyda3b4662021-01-25 17:29:01 +01002494 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002495 }
2496
Willy Tarreaubd715102020-10-23 22:44:30 +02002497 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2498 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002499 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002500 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2501
William Dauchyda3b4662021-01-25 17:29:01 +01002502 /* use tmp variable then assign result to make gcc happy */
2503 *nbup = nbup_tmp;
2504 *nbsrv = nbsrv_tmp;
2505 *totuw = totuw_tmp;
2506}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002507
William Dauchyda3b4662021-01-25 17:29:01 +01002508/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2509 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2510 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2511 * this value, or if the selected field is not implemented for backends, the
2512 * function returns 0, otherwise, it returns 1. <flags> can take the value
2513 * STAT_SHLGNDS.
2514 */
2515int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2516 enum stat_field *selected_field)
2517{
2518 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2519 long long be_samples_counter;
2520 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2521 struct buffer *out = get_trash_chunk();
2522 int nbup, nbsrv, totuw;
2523 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002524
William Dauchyda3b4662021-01-25 17:29:01 +01002525 if (len < ST_F_TOTAL_FIELDS)
2526 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002527
William Dauchyda3b4662021-01-25 17:29:01 +01002528 nbup = nbsrv = totuw = 0;
2529 /* some srv values compute for later if we either select all fields or
2530 * need them for one of the mentioned ones */
2531 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2532 *selected_field == ST_F_UWEIGHT)
2533 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002534
William Dauchyda3b4662021-01-25 17:29:01 +01002535 /* same here but specific to time fields */
2536 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2537 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2538 *selected_field == ST_F_TTIME) {
2539 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2540 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2541 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002542 }
2543
William Dauchyda3b4662021-01-25 17:29:01 +01002544 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2545 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002546
William Dauchyda3b4662021-01-25 17:29:01 +01002547 switch (current_field) {
2548 case ST_F_PXNAME:
2549 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2550 break;
2551 case ST_F_SVNAME:
2552 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2553 break;
2554 case ST_F_MODE:
2555 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2556 break;
2557 case ST_F_QCUR:
2558 metric = mkf_u32(0, px->nbpend);
2559 break;
2560 case ST_F_QMAX:
2561 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2562 break;
2563 case ST_F_SCUR:
2564 metric = mkf_u32(0, px->beconn);
2565 break;
2566 case ST_F_SMAX:
2567 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2568 break;
2569 case ST_F_SLIM:
2570 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2571 break;
2572 case ST_F_STOT:
2573 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2574 break;
2575 case ST_F_BIN:
2576 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2577 break;
2578 case ST_F_BOUT:
2579 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2580 break;
2581 case ST_F_DREQ:
2582 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2583 break;
2584 case ST_F_DRESP:
2585 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2586 break;
2587 case ST_F_ECON:
2588 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2589 break;
2590 case ST_F_ERESP:
2591 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2592 break;
2593 case ST_F_WRETR:
2594 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2595 break;
2596 case ST_F_WREDIS:
2597 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2598 break;
2599 case ST_F_WREW:
2600 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2601 break;
2602 case ST_F_EINT:
2603 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2604 break;
2605 case ST_F_CONNECT:
2606 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2607 break;
2608 case ST_F_REUSE:
2609 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2610 break;
2611 case ST_F_STATUS:
2612 fld = chunk_newstr(out);
2613 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2614 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2615 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2616 metric = mkf_str(FO_STATUS, fld);
2617 break;
2618 case ST_F_WEIGHT:
2619 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2620 break;
2621 case ST_F_UWEIGHT:
2622 metric = mkf_u32(FN_AVG, totuw);
2623 break;
2624 case ST_F_ACT:
2625 metric = mkf_u32(0, px->srv_act);
2626 break;
2627 case ST_F_BCK:
2628 metric = mkf_u32(0, px->srv_bck);
2629 break;
2630 case ST_F_CHKDOWN:
2631 metric = mkf_u64(FN_COUNTER, px->down_trans);
2632 break;
2633 case ST_F_LASTCHG:
2634 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2635 break;
2636 case ST_F_DOWNTIME:
2637 if (px->srv)
2638 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2639 break;
2640 case ST_F_PID:
2641 metric = mkf_u32(FO_KEY, relative_pid);
2642 break;
2643 case ST_F_IID:
2644 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2645 break;
2646 case ST_F_SID:
2647 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2648 break;
2649 case ST_F_LBTOT:
2650 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2651 break;
2652 case ST_F_TYPE:
2653 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2654 break;
2655 case ST_F_RATE:
2656 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2657 break;
2658 case ST_F_RATE_MAX:
2659 metric = mkf_u32(0, px->be_counters.sps_max);
2660 break;
2661 case ST_F_COOKIE:
2662 if (flags & STAT_SHLGNDS && px->cookie_name)
2663 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2664 break;
2665 case ST_F_ALGO:
2666 if (flags & STAT_SHLGNDS)
2667 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2668 break;
2669 case ST_F_REQ_TOT:
2670 if (px->mode == PR_MODE_HTTP)
2671 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2672 break;
2673 case ST_F_HRSP_1XX:
2674 if (px->mode == PR_MODE_HTTP)
2675 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2676 break;
2677 case ST_F_HRSP_2XX:
2678 if (px->mode == PR_MODE_HTTP)
2679 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2680 break;
2681 case ST_F_HRSP_3XX:
2682 if (px->mode == PR_MODE_HTTP)
2683 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2684 break;
2685 case ST_F_HRSP_4XX:
2686 if (px->mode == PR_MODE_HTTP)
2687 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2688 break;
2689 case ST_F_HRSP_5XX:
2690 if (px->mode == PR_MODE_HTTP)
2691 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2692 break;
2693 case ST_F_HRSP_OTHER:
2694 if (px->mode == PR_MODE_HTTP)
2695 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2696 break;
2697 case ST_F_CACHE_LOOKUPS:
2698 if (px->mode == PR_MODE_HTTP)
2699 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2700 break;
2701 case ST_F_CACHE_HITS:
2702 if (px->mode == PR_MODE_HTTP)
2703 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2704 break;
2705 case ST_F_CLI_ABRT:
2706 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2707 break;
2708 case ST_F_SRV_ABRT:
2709 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2710 break;
2711 case ST_F_COMP_IN:
2712 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2713 break;
2714 case ST_F_COMP_OUT:
2715 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2716 break;
2717 case ST_F_COMP_BYP:
2718 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2719 break;
2720 case ST_F_COMP_RSP:
2721 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2722 break;
2723 case ST_F_LASTSESS:
2724 metric = mkf_s32(FN_AGE, be_lastsession(px));
2725 break;
2726 case ST_F_QTIME:
2727 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2728 break;
2729 case ST_F_CTIME:
2730 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2731 break;
2732 case ST_F_RTIME:
2733 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2734 break;
2735 case ST_F_TTIME:
2736 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2737 break;
2738 case ST_F_QT_MAX:
2739 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2740 break;
2741 case ST_F_CT_MAX:
2742 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2743 break;
2744 case ST_F_RT_MAX:
2745 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2746 break;
2747 case ST_F_TT_MAX:
2748 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2749 break;
2750 default:
2751 /* not used for backends. If a specific metric
2752 * is requested, return an error. Otherwise continue.
2753 */
2754 if (selected_field != NULL)
2755 return 0;
2756 continue;
2757 }
2758 stats[current_field] = metric;
2759 if (selected_field != NULL)
2760 break;
2761 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002762 return 1;
2763}
2764
2765/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002766 * interface <si>. The caller is responsible for clearing the trash if needed.
2767 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002768 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002769static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002770{
2771 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002772 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2773 struct stats_module *mod;
2774 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002775
2776 if (!(px->cap & PR_CAP_BE))
2777 return 0;
2778
2779 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2780 return 0;
2781
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002782 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2783
William Dauchyda3b4662021-01-25 17:29:01 +01002784 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002785 return 0;
2786
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002787 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2788 struct extra_counters *counters;
2789
2790 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2791 continue;
2792
2793 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2794 stats_count += mod->stats_count;
2795 continue;
2796 }
2797
2798 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2799 mod->fill_stats(counters, stats + stats_count);
2800 stats_count += mod->stats_count;
2801 }
2802
2803 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002804}
2805
2806/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2807 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2808 * for clearing the trash if needed.
2809 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002810static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002811{
2812 struct appctx *appctx = __objt_appctx(si->end);
2813 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002814 struct stats_module *mod;
2815 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002816
2817 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2818 /* A form to enable/disable this proxy servers */
2819
2820 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2821 scope_txt[0] = 0;
2822 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002823 const char *scope_ptr = stats_scope_ptr(appctx, si);
2824
William Lallemand74c24fb2016-11-21 17:18:36 +01002825 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002826 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002827 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2828 }
2829
2830 chunk_appendf(&trash,
2831 "<form method=\"post\">");
2832 }
2833
2834 /* print a new table */
2835 chunk_appendf(&trash,
2836 "<table class=\"tbl\" width=\"100%%\">\n"
2837 "<tr class=\"titre\">"
2838 "<th class=\"pxname\" width=\"10%%\">");
2839
2840 chunk_appendf(&trash,
2841 "<a name=\"%s\"></a>%s"
2842 "<a class=px href=\"#%s\">%s</a>",
2843 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002844 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002845 px->id, px->id);
2846
Willy Tarreau676c29e2019-10-09 10:50:01 +02002847 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002848 /* cap, mode, id */
2849 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2850 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2851 px->uuid);
2852 chunk_appendf(&trash, "</div>");
2853 }
2854
2855 chunk_appendf(&trash,
2856 "%s</th>"
2857 "<th class=\"%s\" width=\"90%%\">%s</th>"
2858 "</tr>\n"
2859 "</table>\n"
2860 "<table class=\"tbl\" width=\"100%%\">\n"
2861 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002862 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002863 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2864
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002865 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002866 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002867 if ((px->cap & PR_CAP_BE) && px->srv)
2868 chunk_appendf(&trash,
2869 "<th rowspan=2 width=1><input type=\"checkbox\" "
2870 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2871 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2872 px->id,
2873 px->id);
2874 else
2875 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002876 }
2877
2878 chunk_appendf(&trash,
2879 "<th rowspan=2></th>"
2880 "<th colspan=3>Queue</th>"
2881 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2882 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2883 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002884 "<th colspan=9>Server</th>");
2885
2886 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2887 // calculate the count of module for colspan attribute
2888 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2889 ++stats_module_len;
2890 }
2891 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2892 stats_module_len);
2893 }
2894
2895 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002896 "</tr>\n"
2897 "<tr class=\"titre\">"
2898 "<th>Cur</th><th>Max</th><th>Limit</th>"
2899 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2900 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2901 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2902 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2903 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2904 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002905 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002906
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002907 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2908 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2909 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2910 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002911 }
2912
2913 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002914}
2915
2916/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2917 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2918 */
2919static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2920{
2921 struct appctx *appctx = __objt_appctx(si->end);
2922 chunk_appendf(&trash, "</table>");
2923
2924 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2925 /* close the form used to enable/disable this proxy servers */
2926 chunk_appendf(&trash,
2927 "Choose the action to perform on the checked servers : "
2928 "<select name=action>"
2929 "<option value=\"\"></option>"
2930 "<option value=\"ready\">Set state to READY</option>"
2931 "<option value=\"drain\">Set state to DRAIN</option>"
2932 "<option value=\"maint\">Set state to MAINT</option>"
2933 "<option value=\"dhlth\">Health: disable checks</option>"
2934 "<option value=\"ehlth\">Health: enable checks</option>"
2935 "<option value=\"hrunn\">Health: force UP</option>"
2936 "<option value=\"hnolb\">Health: force NOLB</option>"
2937 "<option value=\"hdown\">Health: force DOWN</option>"
2938 "<option value=\"dagent\">Agent: disable checks</option>"
2939 "<option value=\"eagent\">Agent: enable checks</option>"
2940 "<option value=\"arunn\">Agent: force UP</option>"
2941 "<option value=\"adown\">Agent: force DOWN</option>"
2942 "<option value=\"shutdown\">Kill Sessions</option>"
2943 "</select>"
2944 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2945 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2946 "</form>",
2947 px->uuid);
2948 }
2949
2950 chunk_appendf(&trash, "<p>\n");
2951}
2952
2953/*
2954 * Dumps statistics for a proxy. The output is sent to the stream interface's
2955 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2956 * buffer space, or non-zero if everything completed. This function is used
2957 * both by the CLI and the HTTP entry points, and is able to dump the output
2958 * in HTML or CSV formats. If the later, <uri> must be NULL.
2959 */
Christopher Fauletef779222018-10-31 08:47:01 +01002960int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2961 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002962{
2963 struct appctx *appctx = __objt_appctx(si->end);
2964 struct stream *s = si_strm(si);
2965 struct channel *rep = si_ic(si);
2966 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2967 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002968
2969 chunk_reset(&trash);
2970
2971 switch (appctx->ctx.stats.px_st) {
2972 case STAT_PX_ST_INIT:
2973 /* we are on a new proxy */
2974 if (uri && uri->scope) {
2975 /* we have a limited scope, we have to check the proxy name */
2976 struct stat_scope *scope;
2977 int len;
2978
2979 len = strlen(px->id);
2980 scope = uri->scope;
2981
2982 while (scope) {
2983 /* match exact proxy name */
2984 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2985 break;
2986
2987 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002988 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01002989 break;
2990 scope = scope->next;
2991 }
2992
2993 /* proxy name not found : don't dump anything */
2994 if (scope == NULL)
2995 return 1;
2996 }
2997
2998 /* if the user has requested a limited output and the proxy
2999 * name does not match, skip it.
3000 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003001 if (appctx->ctx.stats.scope_len) {
3002 const char *scope_ptr = stats_scope_ptr(appctx, si);
3003
3004 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
3005 return 1;
3006 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003007
3008 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3009 (appctx->ctx.stats.iid != -1) &&
3010 (px->uuid != appctx->ctx.stats.iid))
3011 return 1;
3012
3013 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
3014 /* fall through */
3015
3016 case STAT_PX_ST_TH:
3017 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02003018 stats_dump_html_px_hdr(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
3023 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
3024 /* fall through */
3025
3026 case STAT_PX_ST_FE:
3027 /* print the frontend */
3028 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003029 if (!stats_putchk(rep, htx, &trash))
3030 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003031 }
3032
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003033 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01003034 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
3035 /* fall through */
3036
3037 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003038 /* obj2 points to listeners list as initialized above */
3039 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003040 if (htx) {
3041 if (htx_almost_full(htx))
3042 goto full;
3043 }
3044 else {
3045 if (buffer_almost_full(&rep->buf))
3046 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003047 }
3048
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003049 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003050 if (!l->counters)
3051 continue;
3052
3053 if (appctx->ctx.stats.flags & STAT_BOUND) {
3054 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
3055 break;
3056
3057 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
3058 continue;
3059 }
3060
3061 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02003062 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003063 if (!stats_putchk(rep, htx, &trash))
3064 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003065 }
3066 }
3067
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003068 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01003069 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
3070 /* fall through */
3071
3072 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003073 /* obj2 points to servers list as initialized above */
3074 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01003075 if (htx) {
3076 if (htx_almost_full(htx))
3077 goto full;
3078 }
3079 else {
3080 if (buffer_almost_full(&rep->buf))
3081 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003082 }
3083
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003084 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01003085
3086 if (appctx->ctx.stats.flags & STAT_BOUND) {
3087 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
3088 break;
3089
3090 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
3091 continue;
3092 }
3093
Willy Tarreau3e320362020-10-23 17:28:57 +02003094 /* do not report disabled servers */
3095 if (appctx->ctx.stats.flags & STAT_HIDE_MAINT &&
3096 sv->cur_admin & SRV_ADMF_MAINT) {
3097 continue;
3098 }
3099
William Lallemand74c24fb2016-11-21 17:18:36 +01003100 svs = sv;
3101 while (svs->track)
3102 svs = svs->track;
3103
3104 /* do not report servers which are DOWN and not changing state */
3105 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003106 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3107 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003108 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3109 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3110 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3111 continue;
3112 }
3113
Willy Tarreau43241ff2019-10-09 11:27:51 +02003114 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003115 if (!stats_putchk(rep, htx, &trash))
3116 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003117 }
3118 } /* for sv */
3119
3120 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
3121 /* fall through */
3122
3123 case STAT_PX_ST_BE:
3124 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02003125 if (stats_dump_be_stats(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_END;
3131 /* fall through */
3132
3133 case STAT_PX_ST_END:
3134 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3135 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003136 if (!stats_putchk(rep, htx, &trash))
3137 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003138 }
3139
3140 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
3141 /* fall through */
3142
3143 case STAT_PX_ST_FIN:
3144 return 1;
3145
3146 default:
3147 /* unknown state, we should put an abort() here ! */
3148 return 1;
3149 }
Christopher Fauletef779222018-10-31 08:47:01 +01003150
3151 full:
3152 si_rx_room_blk(si);
3153 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003154}
3155
3156/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3157 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3158 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003159static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003160{
3161 /* WARNING! This must fit in the first buffer !!! */
3162 chunk_appendf(&trash,
3163 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3164 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3165 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03003166 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003167 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3168 "<style type=\"text/css\"><!--\n"
3169 "body {"
3170 " font-family: arial, helvetica, sans-serif;"
3171 " font-size: 12px;"
3172 " font-weight: normal;"
3173 " color: black;"
3174 " background: white;"
3175 "}\n"
3176 "th,td {"
3177 " font-size: 10px;"
3178 "}\n"
3179 "h1 {"
3180 " font-size: x-large;"
3181 " margin-bottom: 0.5em;"
3182 "}\n"
3183 "h2 {"
3184 " font-family: helvetica, arial;"
3185 " font-size: x-large;"
3186 " font-weight: bold;"
3187 " font-style: italic;"
3188 " color: #6020a0;"
3189 " margin-top: 0em;"
3190 " margin-bottom: 0em;"
3191 "}\n"
3192 "h3 {"
3193 " font-family: helvetica, arial;"
3194 " font-size: 16px;"
3195 " font-weight: bold;"
3196 " color: #b00040;"
3197 " background: #e8e8d0;"
3198 " margin-top: 0em;"
3199 " margin-bottom: 0em;"
3200 "}\n"
3201 "li {"
3202 " margin-top: 0.25em;"
3203 " margin-right: 2em;"
3204 "}\n"
3205 ".hr {margin-top: 0.25em;"
3206 " border-color: black;"
3207 " border-bottom-style: solid;"
3208 "}\n"
3209 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3210 ".total {background: #20D0D0;color: #ffff80;}\n"
3211 ".frontend {background: #e8e8d0;}\n"
3212 ".socket {background: #d0d0d0;}\n"
3213 ".backend {background: #e8e8d0;}\n"
3214 ".active_down {background: #ff9090;}\n"
3215 ".active_going_up {background: #ffd020;}\n"
3216 ".active_going_down {background: #ffffa0;}\n"
3217 ".active_up {background: #c0ffc0;}\n"
3218 ".active_nolb {background: #20a0ff;}\n"
3219 ".active_draining {background: #20a0FF;}\n"
3220 ".active_no_check {background: #e0e0e0;}\n"
3221 ".backup_down {background: #ff9090;}\n"
3222 ".backup_going_up {background: #ff80ff;}\n"
3223 ".backup_going_down {background: #c060ff;}\n"
3224 ".backup_up {background: #b0d0ff;}\n"
3225 ".backup_nolb {background: #90b0e0;}\n"
3226 ".backup_draining {background: #cc9900;}\n"
3227 ".backup_no_check {background: #e0e0e0;}\n"
3228 ".maintain {background: #c07820;}\n"
3229 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3230 "\n"
3231 "a.px:link {color: #ffff40; text-decoration: none;}"
3232 "a.px:visited {color: #ffff40; text-decoration: none;}"
3233 "a.px:hover {color: #ffffff; text-decoration: none;}"
3234 "a.lfsb:link {color: #000000; text-decoration: none;}"
3235 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3236 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3237 "\n"
3238 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3239 "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"
3240 "table.tbl td.ac { text-align: center;}\n"
3241 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3242 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3243 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3244 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3245 "\n"
3246 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3247 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3248 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3249 "table.det { border-collapse: collapse; border-style: none; }\n"
3250 "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"
3251 "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"
3252 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
3253 "div.tips {\n"
3254 " display:block;\n"
3255 " visibility:hidden;\n"
3256 " z-index:2147483647;\n"
3257 " position:absolute;\n"
3258 " padding:2px 4px 3px;\n"
3259 " background:#f0f060; color:#000000;\n"
3260 " border:1px solid #7040c0;\n"
3261 " white-space:nowrap;\n"
3262 " font-style:normal;font-size:11px;font-weight:normal;\n"
3263 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3264 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3265 "}\n"
3266 "u:hover div.tips {visibility:visible;}\n"
3267 "-->\n"
3268 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003269 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01003270 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003271 );
3272}
3273
3274/* Dumps the HTML stats information block to the trash for and uses the state from
3275 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
3276 * for clearing the trash if needed.
3277 */
3278static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
3279{
3280 struct appctx *appctx = __objt_appctx(si->end);
3281 unsigned int up = (now.tv_sec - start_date.tv_sec);
3282 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01003283 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02003284 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3285
3286 /* Turn the bytes per second to bits per second and take care of the
3287 * usual ethernet overhead in order to help figure how far we are from
3288 * interface saturation since it's the only case which usually matters.
3289 * For this we count the total size of an Ethernet frame on the wire
3290 * including preamble and IFG (1538) for the largest TCP segment it
3291 * transports (1448 with TCP timestamps). This is not valid for smaller
3292 * packets (under-estimated), but it gives a reasonably accurate
3293 * estimation of how far we are from uplink saturation.
3294 */
3295 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003296
3297 /* WARNING! this has to fit the first packet too.
3298 * We are around 3.5 kB, add adding entries will
3299 * become tricky if we want to support 4kB buffers !
3300 */
3301 chunk_appendf(&trash,
3302 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3303 PRODUCT_NAME "%s</a></h1>\n"
3304 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3305 "<hr width=\"100%%\" class=\"hr\">\n"
3306 "<h3>&gt; General process information</h3>\n"
3307 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003308 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003309 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3310 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3311 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003312 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003313 "Running tasks: %d/%d; idle = %d %%<br>\n"
3314 "</td><td align=\"center\" nowrap>\n"
3315 "<table class=\"lgd\"><tr>\n"
3316 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3317 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3318 "</tr><tr>\n"
3319 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3320 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3321 "</tr><tr>\n"
3322 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3323 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3324 "</tr><tr>\n"
3325 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3326 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3327 "</tr><tr>\n"
3328 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3329 "</tr><tr>\n"
3330 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3331 "</tr></table>\n"
3332 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3333 "</td>"
3334 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3335 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3336 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003337 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
3338 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
3339 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3340 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
3341 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01003342 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003343 up / 86400, (up % 86400) / 3600,
3344 (up % 3600) / 60, (up % 60),
3345 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3346 global.rlimit_memmax ? " MB" : "",
3347 global.rlimit_nofile,
3348 global.maxsock, global.maxconn, global.maxpipes,
3349 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003350 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3351 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau955a11e2021-02-24 17:03:30 +01003352 total_run_queues(), total_allocated_tasks(), ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01003353 );
3354
3355 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003356 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003357 scope_txt[appctx->ctx.stats.scope_len] = '\0';
3358
3359 chunk_appendf(&trash,
3360 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
3361 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
3362 STAT_SCOPE_TXT_MAXLEN);
3363
3364 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3365 scope_txt[0] = 0;
3366 if (appctx->ctx.stats.scope_len) {
3367 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003368 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003369 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3370 }
3371
3372 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
3373 chunk_appendf(&trash,
3374 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3375 uri->uri_prefix,
3376 "",
3377 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3378 scope_txt);
3379 else
3380 chunk_appendf(&trash,
3381 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3382 uri->uri_prefix,
3383 ";up",
3384 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3385 scope_txt);
3386
3387 if (uri->refresh > 0) {
3388 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
3389 chunk_appendf(&trash,
3390 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3391 uri->uri_prefix,
3392 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3393 "",
3394 scope_txt);
3395 else
3396 chunk_appendf(&trash,
3397 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3398 uri->uri_prefix,
3399 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3400 ";norefresh",
3401 scope_txt);
3402 }
3403
3404 chunk_appendf(&trash,
3405 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3406 uri->uri_prefix,
3407 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3408 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3409 scope_txt);
3410
3411 chunk_appendf(&trash,
3412 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3413 uri->uri_prefix,
3414 (uri->refresh > 0) ? ";norefresh" : "",
3415 scope_txt);
3416
3417 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003418 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3419 uri->uri_prefix,
3420 (uri->refresh > 0) ? ";norefresh" : "",
3421 scope_txt, uri->uri_prefix);
3422
3423 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003424 "</ul></td>"
3425 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3426 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3427 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3428 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3429 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3430 "</ul>"
3431 "</td>"
3432 "</tr></table>\n"
3433 ""
3434 );
3435
3436 if (appctx->ctx.stats.st_code) {
3437 switch (appctx->ctx.stats.st_code) {
3438 case STAT_STATUS_DONE:
3439 chunk_appendf(&trash,
3440 "<p><div class=active_up>"
3441 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3442 "Action processed successfully."
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_NONE:
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 "Nothing has changed."
3453 "</div>\n", uri->uri_prefix,
3454 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3455 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3456 scope_txt);
3457 break;
3458 case STAT_STATUS_PART:
3459 chunk_appendf(&trash,
3460 "<p><div class=active_going_down>"
3461 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3462 "Action partially processed.<br>"
3463 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3464 "</div>\n", uri->uri_prefix,
3465 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3466 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3467 scope_txt);
3468 break;
3469 case STAT_STATUS_ERRP:
3470 chunk_appendf(&trash,
3471 "<p><div class=active_down>"
3472 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3473 "Action not processed because of invalid parameters."
3474 "<ul>"
3475 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003476 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003477 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3478 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3479 "</ul>"
3480 "</div>\n", uri->uri_prefix,
3481 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3482 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3483 scope_txt);
3484 break;
3485 case STAT_STATUS_EXCD:
3486 chunk_appendf(&trash,
3487 "<p><div class=active_down>"
3488 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3489 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3490 "You should retry with less servers at a time.</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;
3496 case STAT_STATUS_DENY:
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>Action denied.</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;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003506 case STAT_STATUS_IVAL:
3507 chunk_appendf(&trash,
3508 "<p><div class=active_down>"
3509 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3510 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
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 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003516 default:
3517 chunk_appendf(&trash,
3518 "<p><div class=active_no_check>"
3519 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3520 "Unexpected result."
3521 "</div>\n", uri->uri_prefix,
3522 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3523 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3524 scope_txt);
3525 }
3526 chunk_appendf(&trash, "<p>\n");
3527 }
3528}
3529
3530/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3531 * for clearing the trash if needed.
3532 */
3533static void stats_dump_html_end()
3534{
3535 chunk_appendf(&trash, "</body></html>\n");
3536}
3537
Simon Horman05ee2132017-01-04 09:37:25 +01003538/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3539 * for clearing it if needed.
3540 */
3541static void stats_dump_json_header()
3542{
3543 chunk_strcat(&trash, "[");
3544}
3545
3546
3547/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3548 * for clearing the trash if needed.
3549 */
3550static void stats_dump_json_end()
3551{
3552 chunk_strcat(&trash, "]");
3553}
3554
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003555/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3556 * a pointer to the current server/listener.
3557 */
3558static int stats_dump_proxies(struct stream_interface *si,
3559 struct htx *htx,
3560 struct uri_auth *uri)
3561{
3562 struct appctx *appctx = __objt_appctx(si->end);
3563 struct channel *rep = si_ic(si);
3564 struct proxy *px;
3565
3566 /* dump proxies */
3567 while (appctx->ctx.stats.obj1) {
3568 if (htx) {
3569 if (htx_almost_full(htx))
3570 goto full;
3571 }
3572 else {
3573 if (buffer_almost_full(&rep->buf))
3574 goto full;
3575 }
3576
3577 px = appctx->ctx.stats.obj1;
3578 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02003579 if (!px->disabled && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003580 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
3581 return 0;
3582 }
3583
3584 appctx->ctx.stats.obj1 = px->next;
3585 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3586 }
3587
3588 return 1;
3589
3590 full:
3591 si_rx_room_blk(si);
3592 return 0;
3593}
3594
William Lallemand74c24fb2016-11-21 17:18:36 +01003595/* This function dumps statistics onto the stream interface's read buffer in
3596 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3597 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3598 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3599 * and the stream must be closed, or -1 in case of any error. This function is
3600 * used by both the CLI and the HTTP handlers.
3601 */
Christopher Fauletef779222018-10-31 08:47:01 +01003602static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
3603 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003604{
3605 struct appctx *appctx = __objt_appctx(si->end);
3606 struct channel *rep = si_ic(si);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003607 enum stats_domain domain = appctx->ctx.stats.domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003608
3609 chunk_reset(&trash);
3610
3611 switch (appctx->st2) {
3612 case STAT_ST_INIT:
3613 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3614 /* fall through */
3615
3616 case STAT_ST_HEAD:
3617 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003618 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003619 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3620 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003621 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003622 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003623 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003624 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003625
Christopher Fauletef779222018-10-31 08:47:01 +01003626 if (!stats_putchk(rep, htx, &trash))
3627 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003628
Christopher Faulet6338a082019-09-09 15:50:54 +02003629 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3630 appctx->st2 = STAT_ST_FIN;
3631 return 1;
3632 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003633 appctx->st2 = STAT_ST_INFO;
3634 /* fall through */
3635
3636 case STAT_ST_INFO:
3637 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3638 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003639 if (!stats_putchk(rep, htx, &trash))
3640 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003641 }
3642
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003643 if (domain == STATS_DOMAIN_PROXY)
3644 appctx->ctx.stats.obj1 = proxies_list;
3645
William Lallemand74c24fb2016-11-21 17:18:36 +01003646 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3647 appctx->st2 = STAT_ST_LIST;
3648 /* fall through */
3649
3650 case STAT_ST_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003651 switch (domain) {
3652 case STATS_DOMAIN_DNS:
Emeric Brund3b44952021-01-06 16:01:02 +01003653 if (!stats_dump_resolvers(si, stat_l[domain],
3654 stat_count[domain],
3655 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003656 return 0;
3657 }
3658 break;
3659
3660 case STATS_DOMAIN_PROXY:
3661 default:
3662 /* dump proxies */
3663 if (!stats_dump_proxies(si, htx, uri))
3664 return 0;
3665 break;
3666 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003667
3668 appctx->st2 = STAT_ST_END;
3669 /* fall through */
3670
3671 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003672 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3673 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3674 stats_dump_html_end();
3675 else
3676 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003677 if (!stats_putchk(rep, htx, &trash))
3678 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003679 }
3680
3681 appctx->st2 = STAT_ST_FIN;
3682 /* fall through */
3683
3684 case STAT_ST_FIN:
3685 return 1;
3686
3687 default:
3688 /* unknown state ! */
3689 appctx->st2 = STAT_ST_FIN;
3690 return -1;
3691 }
Christopher Fauletef779222018-10-31 08:47:01 +01003692
3693 full:
3694 si_rx_room_blk(si);
3695 return 0;
3696
William Lallemand74c24fb2016-11-21 17:18:36 +01003697}
3698
3699/* We reached the stats page through a POST request. The appctx is
3700 * expected to have already been allocated by the caller.
3701 * Parse the posted data and enable/disable servers if necessary.
3702 * Returns 1 if request was parsed or zero if it needs more data.
3703 */
3704static int stats_process_http_post(struct stream_interface *si)
3705{
3706 struct stream *s = si_strm(si);
3707 struct appctx *appctx = objt_appctx(si->end);
3708
3709 struct proxy *px = NULL;
3710 struct server *sv = NULL;
3711
3712 char key[LINESIZE];
3713 int action = ST_ADM_ACTION_NONE;
3714 int reprocess = 0;
3715
3716 int total_servers = 0;
3717 int altered_servers = 0;
3718
3719 char *first_param, *cur_param, *next_param, *end_params;
3720 char *st_cur_param = NULL;
3721 char *st_next_param = NULL;
3722
Christopher Fauleta3618372018-12-13 21:59:56 +01003723 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003724
Christopher Fauletb7f88902019-07-15 21:56:43 +02003725 struct htx *htx = htxbuf(&s->req.buf);
3726 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003727
Christopher Fauletb7f88902019-07-15 21:56:43 +02003728 /* we need more data */
3729 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3730 /* check if we can receive more */
3731 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3732 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3733 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003734 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003735 goto wait;
3736 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003737
Christopher Fauletb7f88902019-07-15 21:56:43 +02003738 /* The request was fully received. Copy data */
3739 blk = htx_get_head_blk(htx);
3740 while (blk) {
3741 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003742
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003743 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003744 break;
3745 if (type == HTX_BLK_DATA) {
3746 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003747
Christopher Fauletb7f88902019-07-15 21:56:43 +02003748 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003749 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3750 goto out;
3751 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003752 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003753 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003754 }
3755
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003756 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003757 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003758 cur_param = next_param = end_params;
3759 *end_params = '\0';
3760
3761 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3762
3763 /*
3764 * Parse the parameters in reverse order to only store the last value.
3765 * From the html form, the backend and the action are at the end.
3766 */
3767 while (cur_param > first_param) {
3768 char *value;
3769 int poffset, plen;
3770
3771 cur_param--;
3772
3773 if ((*cur_param == '&') || (cur_param == first_param)) {
3774 reprocess_servers:
3775 /* Parse the key */
3776 poffset = (cur_param != first_param ? 1 : 0);
3777 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3778 if ((plen > 0) && (plen <= sizeof(key))) {
3779 strncpy(key, cur_param + poffset, plen);
3780 key[plen - 1] = '\0';
3781 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003782 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003783 goto out;
3784 }
3785
3786 /* Parse the value */
3787 value = key;
3788 while (*value != '\0' && *value != '=') {
3789 value++;
3790 }
3791 if (*value == '=') {
3792 /* Ok, a value is found, we can mark the end of the key */
3793 *value++ = '\0';
3794 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003795 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003796 break;
3797
3798 /* Now we can check the key to see what to do */
3799 if (!px && (strcmp(key, "b") == 0)) {
3800 if ((px = proxy_be_by_name(value)) == NULL) {
3801 /* the backend name is unknown or ambiguous (duplicate names) */
3802 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3803 goto out;
3804 }
3805 }
3806 else if (!action && (strcmp(key, "action") == 0)) {
3807 if (strcmp(value, "ready") == 0) {
3808 action = ST_ADM_ACTION_READY;
3809 }
3810 else if (strcmp(value, "drain") == 0) {
3811 action = ST_ADM_ACTION_DRAIN;
3812 }
3813 else if (strcmp(value, "maint") == 0) {
3814 action = ST_ADM_ACTION_MAINT;
3815 }
3816 else if (strcmp(value, "shutdown") == 0) {
3817 action = ST_ADM_ACTION_SHUTDOWN;
3818 }
3819 else if (strcmp(value, "dhlth") == 0) {
3820 action = ST_ADM_ACTION_DHLTH;
3821 }
3822 else if (strcmp(value, "ehlth") == 0) {
3823 action = ST_ADM_ACTION_EHLTH;
3824 }
3825 else if (strcmp(value, "hrunn") == 0) {
3826 action = ST_ADM_ACTION_HRUNN;
3827 }
3828 else if (strcmp(value, "hnolb") == 0) {
3829 action = ST_ADM_ACTION_HNOLB;
3830 }
3831 else if (strcmp(value, "hdown") == 0) {
3832 action = ST_ADM_ACTION_HDOWN;
3833 }
3834 else if (strcmp(value, "dagent") == 0) {
3835 action = ST_ADM_ACTION_DAGENT;
3836 }
3837 else if (strcmp(value, "eagent") == 0) {
3838 action = ST_ADM_ACTION_EAGENT;
3839 }
3840 else if (strcmp(value, "arunn") == 0) {
3841 action = ST_ADM_ACTION_ARUNN;
3842 }
3843 else if (strcmp(value, "adown") == 0) {
3844 action = ST_ADM_ACTION_ADOWN;
3845 }
3846 /* else these are the old supported methods */
3847 else if (strcmp(value, "disable") == 0) {
3848 action = ST_ADM_ACTION_DISABLE;
3849 }
3850 else if (strcmp(value, "enable") == 0) {
3851 action = ST_ADM_ACTION_ENABLE;
3852 }
3853 else if (strcmp(value, "stop") == 0) {
3854 action = ST_ADM_ACTION_STOP;
3855 }
3856 else if (strcmp(value, "start") == 0) {
3857 action = ST_ADM_ACTION_START;
3858 }
3859 else {
3860 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3861 goto out;
3862 }
3863 }
3864 else if (strcmp(key, "s") == 0) {
3865 if (!(px && action)) {
3866 /*
3867 * Indicates that we'll need to reprocess the parameters
3868 * as soon as backend and action are known
3869 */
3870 if (!reprocess) {
3871 st_cur_param = cur_param;
3872 st_next_param = next_param;
3873 }
3874 reprocess = 1;
3875 }
3876 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003877 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003878 switch (action) {
3879 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003880 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003881 altered_servers++;
3882 total_servers++;
3883 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3884 }
3885 break;
3886 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003887 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003888 altered_servers++;
3889 total_servers++;
3890 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3891 }
3892 break;
3893 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003894 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003895 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3896 altered_servers++;
3897 total_servers++;
3898 }
3899 break;
3900 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003901 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003902 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3903 altered_servers++;
3904 total_servers++;
3905 }
3906 break;
3907 case ST_ADM_ACTION_DHLTH:
3908 if (sv->check.state & CHK_ST_CONFIGURED) {
3909 sv->check.state &= ~CHK_ST_ENABLED;
3910 altered_servers++;
3911 total_servers++;
3912 }
3913 break;
3914 case ST_ADM_ACTION_EHLTH:
3915 if (sv->check.state & CHK_ST_CONFIGURED) {
3916 sv->check.state |= CHK_ST_ENABLED;
3917 altered_servers++;
3918 total_servers++;
3919 }
3920 break;
3921 case ST_ADM_ACTION_HRUNN:
3922 if (!(sv->track)) {
3923 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003924 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003925 altered_servers++;
3926 total_servers++;
3927 }
3928 break;
3929 case ST_ADM_ACTION_HNOLB:
3930 if (!(sv->track)) {
3931 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003932 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003933 altered_servers++;
3934 total_servers++;
3935 }
3936 break;
3937 case ST_ADM_ACTION_HDOWN:
3938 if (!(sv->track)) {
3939 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003940 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003941 altered_servers++;
3942 total_servers++;
3943 }
3944 break;
3945 case ST_ADM_ACTION_DAGENT:
3946 if (sv->agent.state & CHK_ST_CONFIGURED) {
3947 sv->agent.state &= ~CHK_ST_ENABLED;
3948 altered_servers++;
3949 total_servers++;
3950 }
3951 break;
3952 case ST_ADM_ACTION_EAGENT:
3953 if (sv->agent.state & CHK_ST_CONFIGURED) {
3954 sv->agent.state |= CHK_ST_ENABLED;
3955 altered_servers++;
3956 total_servers++;
3957 }
3958 break;
3959 case ST_ADM_ACTION_ARUNN:
3960 if (sv->agent.state & CHK_ST_ENABLED) {
3961 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003962 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003963 altered_servers++;
3964 total_servers++;
3965 }
3966 break;
3967 case ST_ADM_ACTION_ADOWN:
3968 if (sv->agent.state & CHK_ST_ENABLED) {
3969 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003970 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003971 altered_servers++;
3972 total_servers++;
3973 }
3974 break;
3975 case ST_ADM_ACTION_READY:
3976 srv_adm_set_ready(sv);
3977 altered_servers++;
3978 total_servers++;
3979 break;
3980 case ST_ADM_ACTION_DRAIN:
3981 srv_adm_set_drain(sv);
3982 altered_servers++;
3983 total_servers++;
3984 break;
3985 case ST_ADM_ACTION_MAINT:
3986 srv_adm_set_maint(sv);
3987 altered_servers++;
3988 total_servers++;
3989 break;
3990 case ST_ADM_ACTION_SHUTDOWN:
Willy Tarreauc3914d42020-09-24 08:39:22 +02003991 if (!px->disabled) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003992 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003993 altered_servers++;
3994 total_servers++;
3995 }
3996 break;
3997 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003998 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003999 } else {
4000 /* the server name is unknown or ambiguous (duplicate names) */
4001 total_servers++;
4002 }
4003 }
4004 if (reprocess && px && action) {
4005 /* Now, we know the backend and the action chosen by the user.
4006 * We can safely restart from the first server parameter
4007 * to reprocess them
4008 */
4009 cur_param = st_cur_param;
4010 next_param = st_next_param;
4011 reprocess = 0;
4012 goto reprocess_servers;
4013 }
4014
4015 next_param = cur_param;
4016 }
4017 }
4018
4019 if (total_servers == 0) {
4020 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4021 }
4022 else if (altered_servers == 0) {
4023 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
4024 }
4025 else if (altered_servers == total_servers) {
4026 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
4027 }
4028 else {
4029 appctx->ctx.stats.st_code = STAT_STATUS_PART;
4030 }
4031 out:
4032 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004033 wait:
4034 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4035 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004036}
4037
4038
Christopher Fauletb7f88902019-07-15 21:56:43 +02004039static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004040{
4041 struct stream *s = si_strm(si);
4042 struct uri_auth *uri = s->be->uri_auth;
4043 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004044 struct htx_sl *sl;
4045 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004046
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004047 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);
4048 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4049 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004050 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004051 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004052
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004053 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004054 goto full;
4055 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
4056 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4057 goto full;
4058 }
Christopher Faulet6338a082019-09-09 15:50:54 +02004059 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
4060 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4061 goto full;
4062 }
Christopher Fauletef779222018-10-31 08:47:01 +01004063 else {
4064 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4065 goto full;
4066 }
4067
4068 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
4069 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004070 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004071 goto full;
4072 }
4073
4074 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4075 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4076 goto full;
4077 }
4078
4079 if (!htx_add_endof(htx, HTX_BLK_EOH))
4080 goto full;
4081
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004082 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004083 return 1;
4084
4085 full:
4086 htx_reset(htx);
4087 si_rx_room_blk(si);
4088 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004089}
4090
Christopher Fauletef779222018-10-31 08:47:01 +01004091
Christopher Fauletb7f88902019-07-15 21:56:43 +02004092static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004093{
4094 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4095 struct stream *s = si_strm(si);
4096 struct uri_auth *uri = s->be->uri_auth;
4097 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004098 struct htx_sl *sl;
4099 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004100
4101 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4102 scope_txt[0] = 0;
4103 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01004104 const char *scope_ptr = stats_scope_ptr(appctx, si);
4105
Christopher Fauletef779222018-10-31 08:47:01 +01004106 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004107 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01004108 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
4109 }
4110
4111 /* We don't want to land on the posted stats page because a refresh will
4112 * repost the data. We don't want this to happen on accident so we redirect
4113 * the browse to the stats page with a GET.
4114 */
4115 chunk_printf(&trash, "%s;st=%s%s%s%s",
4116 uri->uri_prefix,
4117 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4118 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4119 stat_status_codes[appctx->ctx.stats.st_code]) ?
4120 stat_status_codes[appctx->ctx.stats.st_code] :
4121 stat_status_codes[STAT_STATUS_UNKN],
4122 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4123 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4124 scope_txt);
4125
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004126 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4127 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4128 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004129 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004130 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004131
4132 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004133 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4134 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4135 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4136 goto full;
4137
4138 if (!htx_add_endof(htx, HTX_BLK_EOH))
4139 goto full;
4140
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004141 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004142 return 1;
4143
4144full:
4145 htx_reset(htx);
4146 si_rx_room_blk(si);
4147 return 0;
4148}
William Lallemand74c24fb2016-11-21 17:18:36 +01004149
Simon Horman05ee2132017-01-04 09:37:25 +01004150
William Lallemand74c24fb2016-11-21 17:18:36 +01004151/* This I/O handler runs as an applet embedded in a stream interface. It is
4152 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4153 * appctx->st0 contains the operation in progress (dump, done). The handler
4154 * automatically unregisters itself once transfer is complete.
4155 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004156static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004157{
4158 struct stream_interface *si = appctx->owner;
4159 struct stream *s = si_strm(si);
4160 struct channel *req = si_oc(si);
4161 struct channel *res = si_ic(si);
4162 struct htx *req_htx, *res_htx;
4163
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004164 /* only proxy stats are available via http */
4165 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
4166
Christopher Fauletef779222018-10-31 08:47:01 +01004167 res_htx = htx_from_buf(&res->buf);
4168
4169 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4170 goto out;
4171
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004172 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004173 if (!b_size(&res->buf)) {
4174 si_rx_room_blk(si);
4175 goto out;
4176 }
4177
4178 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004179 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4180 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004181
4182 /* all states are processed in sequence */
4183 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02004184 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004185 if (s->txn->meth == HTTP_METH_HEAD)
4186 appctx->st0 = STAT_HTTP_DONE;
4187 else
4188 appctx->st0 = STAT_HTTP_DUMP;
4189 }
4190 }
4191
4192 if (appctx->st0 == STAT_HTTP_DUMP) {
4193 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
4194 appctx->st0 = STAT_HTTP_DONE;
4195 }
4196
4197 if (appctx->st0 == STAT_HTTP_POST) {
4198 if (stats_process_http_post(si))
4199 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004200 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004201 appctx->st0 = STAT_HTTP_DONE;
4202 }
4203
4204 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02004205 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004206 appctx->st0 = STAT_HTTP_DONE;
4207 }
4208
4209 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004210 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
4211 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004212 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004213 }
4214
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004215 if (appctx->st0 == STAT_HTTP_END) {
4216 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004217 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004218 si_shutr(si);
4219 }
4220
4221 /* eat the whole request */
4222 if (co_data(req)) {
4223 req_htx = htx_from_buf(&req->buf);
4224 co_htx_skip(req, req_htx, co_data(req));
4225 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004226 }
4227 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004228
Christopher Fauletef779222018-10-31 08:47:01 +01004229 out:
4230 /* we have left the request in the buffer for the case where we
4231 * process a POST, and this automatically re-enables activity on
4232 * read. It's better to indicate that we want to stop reading when
4233 * we're sending, so that we know there's at most one direction
4234 * deciding to wake the applet up. It saves it from looping when
4235 * emitting large blocks into small TCP windows.
4236 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004237 htx_to_buf(res_htx, &res->buf);
4238 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01004239 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01004240}
4241
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004242/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004243static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004244 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004245{
4246 int field;
4247
4248 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4249 if (!field_format(info, field))
4250 continue;
4251
Willy Tarreaueaa55372019-10-09 07:39:11 +02004252 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004253 return 0;
4254 if (!stats_emit_raw_data_field(out, &info[field]))
4255 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004256 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4257 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004258 if (!chunk_strcat(out, "\n"))
4259 return 0;
4260 }
4261 return 1;
4262}
4263
4264/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004265static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004266 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004267{
4268 int field;
4269
4270 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4271 if (!field_format(info, field))
4272 continue;
4273
Willy Tarreaueaa55372019-10-09 07:39:11 +02004274 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 +01004275 return 0;
4276 if (!stats_emit_field_tags(out, &info[field], ':'))
4277 return 0;
4278 if (!stats_emit_typed_data_field(out, &info[field]))
4279 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004280 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4281 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004282 if (!chunk_strcat(out, "\n"))
4283 return 0;
4284 }
4285 return 1;
4286}
4287
4288/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004289 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004290 * INF_TOTAL_FIELDS. If this length is less then this value, the
4291 * function returns 0, otherwise, it returns 1.
4292 */
4293int stats_fill_info(struct field *info, int len)
4294{
4295 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02004296 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004297
4298#ifdef USE_OPENSSL
4299 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
4300 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
4301 int ssl_reuse = 0;
4302
4303 if (ssl_key_rate < ssl_sess_rate) {
4304 /* count the ssl reuse ratio and avoid overflows in both directions */
4305 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
4306 }
4307#endif
4308
4309 if (len < INF_TOTAL_FIELDS)
4310 return 0;
4311
4312 chunk_reset(out);
4313 memset(info, 0, sizeof(*info) * len);
4314
4315 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004316 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004317 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004318 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004319
Yves Lafon95317282018-02-26 11:10:37 +01004320 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004321 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
4322 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
4323 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4324
4325 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
4326 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
4327
4328 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
William Dauchydefd1562021-01-15 22:41:38 +01004329 info[INF_START_TIME_SEC] = mkf_u32(FN_DURATION, start_date.tv_sec);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004330 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004331 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004332 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004333 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004334 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004335 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004336 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4337 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4338 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4339 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4340 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4341 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4342 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4343 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4344#ifdef USE_OPENSSL
4345 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
4346 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
4347 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
4348#endif
4349 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4350 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4351 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
4352 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
4353 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4354 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
4355 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
4356 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4357 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4358
4359#ifdef USE_OPENSSL
4360 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
4361 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4362 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
4363 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
4364 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
4365 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
4366 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
4367 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4368 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4369 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4370#endif
4371 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
4372 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
4373 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4374#ifdef USE_ZLIB
4375 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4376 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4377#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004378 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004379 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreau81036f22019-05-20 19:24:50 +02004380 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004381 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4382 if (global.desc)
4383 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004384 info[INF_STOPPING] = mkf_u32(0, stopping);
4385 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004386 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004387 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004388 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004389 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004390 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004391 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004392 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004393 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004394 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004395 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 +02004396 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004397 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004398
4399 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4400 chunk_appendf(out, "%#x", get_tainted());
4401
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004402 return 1;
4403}
4404
4405/* This function dumps information onto the stream interface's read buffer.
4406 * It returns 0 as long as it does not complete, non-zero upon completion.
4407 * No state is used.
4408 */
4409static int stats_dump_info_to_buffer(struct stream_interface *si)
4410{
4411 struct appctx *appctx = __objt_appctx(si->end);
4412
4413 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
4414 return 0;
4415
4416 chunk_reset(&trash);
4417
4418 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004419 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01004420 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004421 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004422 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02004423 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004424
Willy Tarreau06d80a92017-10-19 14:32:15 +02004425 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004426 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004427 return 0;
4428 }
4429
4430 return 1;
4431}
4432
Simon Horman6f6bb382017-01-04 09:37:26 +01004433/* This function dumps the schema onto the stream interface's read buffer.
4434 * It returns 0 as long as it does not complete, non-zero upon completion.
4435 * No state is used.
4436 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004437 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004438 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4439 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004440static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004441{
4442
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004443 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004444
4445 chunk_strcat(out,
4446 "{"
4447 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4448 "\"oneOf\":["
4449 "{"
4450 "\"title\":\"Info\","
4451 "\"type\":\"array\","
4452 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004453 "\"title\":\"InfoItem\","
4454 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004455 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004456 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4457 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4458 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4459 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4460 "},"
4461 "\"required\":[\"field\",\"processNum\",\"tags\","
4462 "\"value\"]"
4463 "}"
4464 "},"
4465 "{"
4466 "\"title\":\"Stat\","
4467 "\"type\":\"array\","
4468 "\"items\":{"
4469 "\"title\":\"InfoItem\","
4470 "\"type\":\"object\","
4471 "\"properties\":{"
4472 "\"objType\":{"
4473 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4474 "\"Server\",\"Unknown\"]"
4475 "},"
4476 "\"proxyId\":{"
4477 "\"type\":\"integer\","
4478 "\"minimum\":0"
4479 "},"
4480 "\"id\":{"
4481 "\"type\":\"integer\","
4482 "\"minimum\":0"
4483 "},"
4484 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4485 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4486 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4487 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4488 "},"
4489 "\"required\":[\"objType\",\"proxyId\",\"id\","
4490 "\"field\",\"processNum\",\"tags\","
4491 "\"value\"]"
4492 "}"
4493 "},"
4494 "{"
4495 "\"title\":\"Error\","
4496 "\"type\":\"object\","
4497 "\"properties\":{"
4498 "\"errorStr\":{"
4499 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004500 "}"
4501 "},"
4502 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004503 "}"
4504 "],"
4505 "\"definitions\":{"
4506 "\"field\":{"
4507 "\"type\":\"object\","
4508 "\"pos\":{"
4509 "\"type\":\"integer\","
4510 "\"minimum\":0"
4511 "},"
4512 "\"name\":{"
4513 "\"type\":\"string\""
4514 "},"
4515 "\"required\":[\"pos\",\"name\"]"
4516 "},"
4517 "\"processNum\":{"
4518 "\"type\":\"integer\","
4519 "\"minimum\":1"
4520 "},"
4521 "\"tags\":{"
4522 "\"type\":\"object\","
4523 "\"origin\":{"
4524 "\"type\":\"string\","
4525 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4526 "\"Config\",\"Product\",\"Unknown\"]"
4527 "},"
4528 "\"nature\":{"
4529 "\"type\":\"string\","
4530 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4531 "\"Rate\",\"Counter\",\"Duration\","
4532 "\"Age\",\"Time\",\"Name\",\"Output\","
4533 "\"Avg\", \"Unknown\"]"
4534 "},"
4535 "\"scope\":{"
4536 "\"type\":\"string\","
4537 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4538 "\"System\",\"Unknown\"]"
4539 "},"
4540 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4541 "},"
4542 "\"typedValue\":{"
4543 "\"type\":\"object\","
4544 "\"oneOf\":["
4545 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4546 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4547 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4548 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4549 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4550 "],"
4551 "\"definitions\":{"
4552 "\"s32Value\":{"
4553 "\"properties\":{"
4554 "\"type\":{"
4555 "\"type\":\"string\","
4556 "\"enum\":[\"s32\"]"
4557 "},"
4558 "\"value\":{"
4559 "\"type\":\"integer\","
4560 "\"minimum\":-2147483648,"
4561 "\"maximum\":2147483647"
4562 "}"
4563 "},"
4564 "\"required\":[\"type\",\"value\"]"
4565 "},"
4566 "\"s64Value\":{"
4567 "\"properties\":{"
4568 "\"type\":{"
4569 "\"type\":\"string\","
4570 "\"enum\":[\"s64\"]"
4571 "},"
4572 "\"value\":{"
4573 "\"type\":\"integer\","
4574 "\"minimum\":-9007199254740991,"
4575 "\"maximum\":9007199254740991"
4576 "}"
4577 "},"
4578 "\"required\":[\"type\",\"value\"]"
4579 "},"
4580 "\"u32Value\":{"
4581 "\"properties\":{"
4582 "\"type\":{"
4583 "\"type\":\"string\","
4584 "\"enum\":[\"u32\"]"
4585 "},"
4586 "\"value\":{"
4587 "\"type\":\"integer\","
4588 "\"minimum\":0,"
4589 "\"maximum\":4294967295"
4590 "}"
4591 "},"
4592 "\"required\":[\"type\",\"value\"]"
4593 "},"
4594 "\"u64Value\":{"
4595 "\"properties\":{"
4596 "\"type\":{"
4597 "\"type\":\"string\","
4598 "\"enum\":[\"u64\"]"
4599 "},"
4600 "\"value\":{"
4601 "\"type\":\"integer\","
4602 "\"minimum\":0,"
4603 "\"maximum\":9007199254740991"
4604 "}"
4605 "},"
4606 "\"required\":[\"type\",\"value\"]"
4607 "},"
4608 "\"strValue\":{"
4609 "\"properties\":{"
4610 "\"type\":{"
4611 "\"type\":\"string\","
4612 "\"enum\":[\"str\"]"
4613 "},"
4614 "\"value\":{\"type\":\"string\"}"
4615 "},"
4616 "\"required\":[\"type\",\"value\"]"
4617 "},"
4618 "\"unknownValue\":{"
4619 "\"properties\":{"
4620 "\"type\":{"
4621 "\"type\":\"integer\","
4622 "\"minimum\":0"
4623 "},"
4624 "\"value\":{"
4625 "\"type\":\"string\","
4626 "\"enum\":[\"unknown\"]"
4627 "}"
4628 "},"
4629 "\"required\":[\"type\",\"value\"]"
4630 "}"
4631 "}"
4632 "}"
4633 "}"
4634 "}");
4635
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004636 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004637 chunk_reset(out);
4638 chunk_appendf(out,
4639 "{\"errorStr\":\"output buffer too short\"}");
4640 }
4641}
4642
4643/* This function dumps the schema onto the stream interface's read buffer.
4644 * It returns 0 as long as it does not complete, non-zero upon completion.
4645 * No state is used.
4646 */
4647static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
4648{
4649 chunk_reset(&trash);
4650
4651 stats_dump_json_schema(&trash);
4652
Willy Tarreau06d80a92017-10-19 14:32:15 +02004653 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004654 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01004655 return 0;
4656 }
4657
4658 return 1;
4659}
4660
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004661static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004662{
4663 struct proxy *px;
4664 struct server *sv;
4665 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004666 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004667 int clrall = 0;
4668
4669 if (strcmp(args[2], "all") == 0)
4670 clrall = 1;
4671
4672 /* check permissions */
4673 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4674 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4675 return 1;
4676
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004677 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004678 if (clrall) {
4679 memset(&px->be_counters, 0, sizeof(px->be_counters));
4680 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4681 }
4682 else {
4683 px->be_counters.conn_max = 0;
4684 px->be_counters.p.http.rps_max = 0;
4685 px->be_counters.sps_max = 0;
4686 px->be_counters.cps_max = 0;
4687 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004688 px->be_counters.qtime_max = 0;
4689 px->be_counters.ctime_max = 0;
4690 px->be_counters.dtime_max = 0;
4691 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004692
4693 px->fe_counters.conn_max = 0;
4694 px->fe_counters.p.http.rps_max = 0;
4695 px->fe_counters.sps_max = 0;
4696 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004697 }
4698
4699 for (sv = px->srv; sv; sv = sv->next)
4700 if (clrall)
4701 memset(&sv->counters, 0, sizeof(sv->counters));
4702 else {
4703 sv->counters.cur_sess_max = 0;
4704 sv->counters.nbpend_max = 0;
4705 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004706 sv->counters.qtime_max = 0;
4707 sv->counters.ctime_max = 0;
4708 sv->counters.dtime_max = 0;
4709 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004710 }
4711
4712 list_for_each_entry(li, &px->conf.listeners, by_fe)
4713 if (li->counters) {
4714 if (clrall)
4715 memset(li->counters, 0, sizeof(*li->counters));
4716 else
4717 li->counters->conn_max = 0;
4718 }
4719 }
4720
4721 global.cps_max = 0;
4722 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004723 global.ssl_max = 0;
4724 global.ssl_fe_keys_max = 0;
4725 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004726
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004727 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4728 if (!mod->clearable && !clrall)
4729 continue;
4730
4731 for (px = proxies_list; px; px = px->next) {
4732 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4733
4734 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4735 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4736 mod,
4737 mod->counters,
4738 mod->counters_size);
4739 }
4740
4741 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4742 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4743 mod,
4744 mod->counters,
4745 mod->counters_size);
4746 }
4747
4748 if (mod_cap & STATS_PX_CAP_SRV) {
4749 for (sv = px->srv; sv; sv = sv->next) {
4750 EXTRA_COUNTERS_INIT(sv->extra_counters,
4751 mod,
4752 mod->counters,
4753 mod->counters_size);
4754 }
4755 }
4756
4757 if (mod_cap & STATS_PX_CAP_LI) {
4758 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4759 EXTRA_COUNTERS_INIT(li->extra_counters,
4760 mod,
4761 mod->counters,
4762 mod->counters_size);
4763 }
4764 }
4765 }
4766 }
4767
Emeric Brund3b44952021-01-06 16:01:02 +01004768 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_DNS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004769
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004770 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004771 return 1;
4772}
4773
4774
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004775static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004776{
Willy Tarreau2f397382019-10-09 11:43:59 +02004777 int arg = 2;
4778
Willy Tarreaud25fc792016-12-16 12:33:47 +01004779 appctx->ctx.stats.scope_str = 0;
4780 appctx->ctx.stats.scope_len = 0;
4781 appctx->ctx.stats.flags = 0;
4782
Willy Tarreau2f397382019-10-09 11:43:59 +02004783 while (*args[arg]) {
4784 if (strcmp(args[arg], "typed") == 0)
4785 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4786 else if (strcmp(args[arg], "json") == 0)
4787 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4788 else if (strcmp(args[arg], "desc") == 0)
4789 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4790 arg++;
4791 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004792 return 0;
4793}
4794
4795
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004796static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004797{
Willy Tarreau2f397382019-10-09 11:43:59 +02004798 int arg = 2;
4799
Willy Tarreaud25fc792016-12-16 12:33:47 +01004800 appctx->ctx.stats.scope_str = 0;
4801 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004802 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4803
4804 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
4805 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004806
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004807 /* proxy is the default domain */
4808 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004809 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004810 ++args;
4811
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004812 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004813 ++args;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004814 } else if (strcmp(args[arg], "dns") == 0) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004815 appctx->ctx.stats.domain = STATS_DOMAIN_DNS;
4816 ++args;
4817 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004818 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004819 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004820 }
4821
4822 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4823 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004824 struct proxy *px;
4825
Willy Tarreau2f397382019-10-09 11:43:59 +02004826 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004827 if (px)
4828 appctx->ctx.stats.iid = px->uuid;
4829 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004830 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004831
Willy Tarreau9d008692019-08-09 11:21:01 +02004832 if (!appctx->ctx.stats.iid)
4833 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004834
Willy Tarreau2b812e22016-11-22 16:18:05 +01004835 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004836 appctx->ctx.stats.type = atoi(args[arg+1]);
4837 appctx->ctx.stats.sid = atoi(args[arg+2]);
4838 arg += 3;
4839 }
4840
4841 while (*args[arg]) {
4842 if (strcmp(args[arg], "typed") == 0)
4843 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4844 else if (strcmp(args[arg], "json") == 0)
4845 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4846 else if (strcmp(args[arg], "desc") == 0)
4847 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02004848 else if (strcmp(args[arg], "no-maint") == 0)
4849 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02004850 else if (strcmp(args[arg], "up") == 0)
4851 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02004852 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004853 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004854
Willy Tarreau2b812e22016-11-22 16:18:05 +01004855 return 0;
4856}
4857
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004858static int cli_io_handler_dump_info(struct appctx *appctx)
4859{
4860 return stats_dump_info_to_buffer(appctx->owner);
4861}
4862
Willy Tarreau2b812e22016-11-22 16:18:05 +01004863/* This I/O handler runs as an applet embedded in a stream interface. It is
4864 * used to send raw stats over a socket.
4865 */
4866static int cli_io_handler_dump_stat(struct appctx *appctx)
4867{
Christopher Fauletef779222018-10-31 08:47:01 +01004868 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004869}
4870
Simon Horman6f6bb382017-01-04 09:37:26 +01004871static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4872{
4873 return stats_dump_json_schema_to_buffer(appctx->owner);
4874}
4875
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01004876int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
4877 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004878{
4879 struct stats_module *mod;
4880
4881 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
4882
4883 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4884 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4885 continue;
4886
4887 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
4888 }
4889
4890 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
4891
4892 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4893 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4894 continue;
4895
4896 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
4897 }
4898
4899 return 1;
4900
4901 alloc_failed:
4902 return 0;
4903}
4904
4905/* Initialize and allocate all extra counters for a proxy and its attached
4906 * servers/listeners with all already registered stats module
4907 */
4908int stats_allocate_proxy_counters(struct proxy *px)
4909{
4910 struct server *sv;
4911 struct listener *li;
4912
4913 if (px->cap & PR_CAP_FE) {
4914 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
4915 COUNTERS_FE,
4916 STATS_PX_CAP_FE)) {
4917 return 0;
4918 }
4919 }
4920
4921 if (px->cap & PR_CAP_BE) {
4922 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
4923 COUNTERS_BE,
4924 STATS_PX_CAP_BE)) {
4925 return 0;
4926 }
4927 }
4928
4929 for (sv = px->srv; sv; sv = sv->next) {
4930 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
4931 COUNTERS_SV,
4932 STATS_PX_CAP_SRV)) {
4933 return 0;
4934 }
4935 }
4936
4937 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4938 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
4939 COUNTERS_LI,
4940 STATS_PX_CAP_LI)) {
4941 return 0;
4942 }
4943 }
4944
4945 return 1;
4946}
4947
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004948void stats_register_module(struct stats_module *m)
4949{
4950 const uint8_t domain = stats_get_domain(m->domain_flags);
4951
Willy Tarreau2b718102021-04-21 07:32:39 +02004952 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004953 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004954}
4955
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004956static int allocate_stats_px_postcheck(void)
4957{
4958 struct stats_module *mod;
4959 size_t i = ST_F_TOTAL_FIELDS;
4960 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004961 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004962
4963 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
4964
4965 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
4966 if (!stat_f[STATS_DOMAIN_PROXY]) {
4967 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
4968 err_code |= ERR_ALERT | ERR_FATAL;
4969 return err_code;
4970 }
4971
4972 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
4973 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
4974
4975 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4976 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
4977 mod->stats,
4978 mod->stats_count * sizeof(struct name_desc));
4979 i += mod->stats_count;
4980 }
4981
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004982 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004983 if (!stats_allocate_proxy_counters(px)) {
4984 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
4985 err_code |= ERR_ALERT | ERR_FATAL;
4986 return err_code;
4987 }
4988 }
4989
Christopher Fauletde79cd22021-01-06 07:41:56 +01004990 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004991
4992 return err_code;
4993}
4994
4995REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
4996
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004997static int allocate_stats_dns_postcheck(void)
4998{
4999 struct stats_module *mod;
5000 size_t i = 0;
5001 int err_code = 0;
5002
5003 stat_f[STATS_DOMAIN_DNS] = malloc(stat_count[STATS_DOMAIN_DNS] * sizeof(struct name_desc));
5004 if (!stat_f[STATS_DOMAIN_DNS]) {
5005 ha_alert("stats: cannot allocate all fields for dns statistics\n");
5006 err_code |= ERR_ALERT | ERR_FATAL;
5007 return err_code;
5008 }
5009
5010 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_DNS], list) {
5011 memcpy(stat_f[STATS_DOMAIN_DNS] + i,
5012 mod->stats,
5013 mod->stats_count * sizeof(struct name_desc));
5014 i += mod->stats_count;
5015 }
5016
Emeric Brund3b44952021-01-06 16:01:02 +01005017 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_DNS])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005018 ha_alert("stats: cannot allocate all counters for dns statistics\n");
5019 err_code |= ERR_ALERT | ERR_FATAL;
5020 return err_code;
5021 }
5022
Christopher Fauletde79cd22021-01-06 07:41:56 +01005023 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005024
5025 return err_code;
5026}
5027
5028REGISTER_CONFIG_POSTPARSER("allocate-stats-dns", allocate_stats_dns_postcheck);
5029
Christopher Fauletde79cd22021-01-06 07:41:56 +01005030static int allocate_stat_lines_per_thread(void)
5031{
5032 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5033
5034 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5035 const int domain = domains[i];
5036
5037 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5038 if (!stat_l[domain])
5039 return 0;
5040 }
5041 return 1;
5042}
5043
5044REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5045
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005046static int allocate_trash_counters(void)
5047{
5048 struct stats_module *mod;
5049 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5050 size_t max_counters_size = 0;
5051
5052 /* calculate the greatest counters used by any stats modules */
5053 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5054 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5055 max_counters_size = mod->counters_size > max_counters_size ?
5056 mod->counters_size : max_counters_size;
5057 }
5058 }
5059
5060 /* allocate the trash with the size of the greatest counters */
5061 if (max_counters_size) {
5062 trash_counters = malloc(max_counters_size);
5063 if (!trash_counters) {
5064 ha_alert("stats: cannot allocate trash counters for statistics\n");
5065 return 0;
5066 }
5067 }
5068
5069 return 1;
5070}
5071
5072REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5073
Christopher Fauletde79cd22021-01-06 07:41:56 +01005074static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005075{
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];
5080
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005081 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005082 }
5083}
5084
5085
5086REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5087
5088static void deinit_stats(void)
5089{
5090 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5091
5092 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5093 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005094
5095 if (stat_f[domain])
5096 free(stat_f[domain]);
5097 }
5098}
5099
5100REGISTER_POST_DEINIT(deinit_stats);
5101
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005102static void free_trash_counters(void)
5103{
5104 if (trash_counters)
5105 free(trash_counters);
5106}
5107
5108REGISTER_PER_THREAD_FREE(free_trash_counters);
5109
Willy Tarreau2b812e22016-11-22 16:18:05 +01005110/* register cli keywords */
5111static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005112 { { "clear", "counters", NULL }, "clear counters [all] : clear max statistics counters (or all counters)", cli_parse_clear_counters, NULL, NULL },
5113 { { "show", "info", NULL }, "show info [desc|json|typed]* : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
5114 { { "show", "stat", NULL }, "show stat [desc|json|no-maint|typed|up]*: report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
5115 { { "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 +01005116 {{},}
5117}};
5118
Willy Tarreau0108d902018-11-25 19:14:37 +01005119INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5120
William Lallemand74c24fb2016-11-21 17:18:36 +01005121struct applet http_stats_applet = {
5122 .obj_type = OBJ_TYPE_APPLET,
5123 .name = "<STATS>", /* used for logging */
5124 .fct = http_stats_io_handler,
5125 .release = NULL,
5126};
5127
William Lallemand74c24fb2016-11-21 17:18:36 +01005128/*
5129 * Local variables:
5130 * c-indent-level: 8
5131 * c-basic-offset: 8
5132 * End:
5133 */