blob: 9862fdd35aca83d3e73af3e8116fe1522c6e3607 [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);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200377 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100378 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
379 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
380 }
381}
382
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200383const char *field_to_html_str(const struct field *f)
384{
385 switch (field_format(f, 0)) {
386 case FF_S32: return U2H(f->u.s32);
387 case FF_S64: return U2H(f->u.s64);
388 case FF_U64: return U2H(f->u.u64);
389 case FF_U32: return U2H(f->u.u32);
390 case FF_STR: return field_str(f, 0);
391 case FF_EMPTY:
392 default:
393 return "";
394 }
395}
396
William Lallemand74c24fb2016-11-21 17:18:36 +0100397/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
398 * output is supposed to be used on its own line. Returns non-zero on success, 0
399 * if the buffer is full.
400 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200401int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100402{
403 switch (field_format(f, 0)) {
404 case FF_EMPTY: return 1;
405 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
406 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
407 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
408 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200409 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100410 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
411 default: return chunk_appendf(out, "%08x:?", f->type);
412 }
413}
414
Simon Horman05ee2132017-01-04 09:37:25 +0100415/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
416 * the recommendation for interoperable integers in section 6 of RFC 7159.
417 */
418#define JSON_INT_MAX ((1ULL << 53) - 1)
419#define JSON_INT_MIN (0 - JSON_INT_MAX)
420
421/* Emits a stats field value and its type in JSON.
422 * Returns non-zero on success, 0 on error.
423 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200424int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100425{
426 int old_len;
427 char buf[20];
428 const char *type, *value = buf, *quote = "";
429
430 switch (field_format(f, 0)) {
431 case FF_EMPTY: return 1;
432 case FF_S32: type = "\"s32\"";
433 snprintf(buf, sizeof(buf), "%d", f->u.s32);
434 break;
435 case FF_U32: type = "\"u32\"";
436 snprintf(buf, sizeof(buf), "%u", f->u.u32);
437 break;
438 case FF_S64: type = "\"s64\"";
439 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
440 return 0;
441 type = "\"s64\"";
442 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
443 break;
444 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
445 return 0;
446 type = "\"u64\"";
447 snprintf(buf, sizeof(buf), "%llu",
448 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200449 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200450 case FF_FLT: type = "\"flt\"";
451 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100452 break;
453 case FF_STR: type = "\"str\"";
454 value = field_str(f, 0);
455 quote = "\"";
456 break;
457 default: snprintf(buf, sizeof(buf), "%u", f->type);
458 type = buf;
459 value = "unknown";
460 quote = "\"";
461 break;
462 }
463
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200464 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100465 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
466 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100468}
469
William Lallemand74c24fb2016-11-21 17:18:36 +0100470/* Emits an encoding of the field type on 3 characters followed by a delimiter.
471 * Returns non-zero on success, 0 if the buffer is full.
472 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200473int stats_emit_field_tags(struct buffer *out, const struct field *f,
474 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100475{
476 char origin, nature, scope;
477
478 switch (field_origin(f, 0)) {
479 case FO_METRIC: origin = 'M'; break;
480 case FO_STATUS: origin = 'S'; break;
481 case FO_KEY: origin = 'K'; break;
482 case FO_CONFIG: origin = 'C'; break;
483 case FO_PRODUCT: origin = 'P'; break;
484 default: origin = '?'; break;
485 }
486
487 switch (field_nature(f, 0)) {
488 case FN_GAUGE: nature = 'G'; break;
489 case FN_LIMIT: nature = 'L'; break;
490 case FN_MIN: nature = 'm'; break;
491 case FN_MAX: nature = 'M'; break;
492 case FN_RATE: nature = 'R'; break;
493 case FN_COUNTER: nature = 'C'; break;
494 case FN_DURATION: nature = 'D'; break;
495 case FN_AGE: nature = 'A'; break;
496 case FN_TIME: nature = 'T'; break;
497 case FN_NAME: nature = 'N'; break;
498 case FN_OUTPUT: nature = 'O'; break;
499 case FN_AVG: nature = 'a'; break;
500 default: nature = '?'; break;
501 }
502
503 switch (field_scope(f, 0)) {
504 case FS_PROCESS: scope = 'P'; break;
505 case FS_SERVICE: scope = 'S'; break;
506 case FS_SYSTEM: scope = 's'; break;
507 case FS_CLUSTER: scope = 'C'; break;
508 default: scope = '?'; break;
509 }
510
511 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
512}
513
Simon Horman05ee2132017-01-04 09:37:25 +0100514/* Emits an encoding of the field type as JSON.
515 * Returns non-zero on success, 0 if the buffer is full.
516 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200517int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100518{
519 const char *origin, *nature, *scope;
520 int old_len;
521
522 switch (field_origin(f, 0)) {
523 case FO_METRIC: origin = "Metric"; break;
524 case FO_STATUS: origin = "Status"; break;
525 case FO_KEY: origin = "Key"; break;
526 case FO_CONFIG: origin = "Config"; break;
527 case FO_PRODUCT: origin = "Product"; break;
528 default: origin = "Unknown"; break;
529 }
530
531 switch (field_nature(f, 0)) {
532 case FN_GAUGE: nature = "Gauge"; break;
533 case FN_LIMIT: nature = "Limit"; break;
534 case FN_MIN: nature = "Min"; break;
535 case FN_MAX: nature = "Max"; break;
536 case FN_RATE: nature = "Rate"; break;
537 case FN_COUNTER: nature = "Counter"; break;
538 case FN_DURATION: nature = "Duration"; break;
539 case FN_AGE: nature = "Age"; break;
540 case FN_TIME: nature = "Time"; break;
541 case FN_NAME: nature = "Name"; break;
542 case FN_OUTPUT: nature = "Output"; break;
543 case FN_AVG: nature = "Avg"; break;
544 default: nature = "Unknown"; break;
545 }
546
547 switch (field_scope(f, 0)) {
548 case FS_PROCESS: scope = "Process"; break;
549 case FS_SERVICE: scope = "Service"; break;
550 case FS_SYSTEM: scope = "System"; break;
551 case FS_CLUSTER: scope = "Cluster"; break;
552 default: scope = "Unknown"; break;
553 }
554
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200555 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100556 chunk_appendf(out, "\"tags\":{"
557 "\"origin\":\"%s\","
558 "\"nature\":\"%s\","
559 "\"scope\":\"%s\""
560 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200561 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100562}
William Lallemand74c24fb2016-11-21 17:18:36 +0100563
564/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200565static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200566 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200567 unsigned int flags,
568 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100569{
570 int field;
571
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200572 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100573 if (!stats_emit_raw_data_field(out, &stats[field]))
574 return 0;
575 if (!chunk_strcat(out, ","))
576 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200577
578 /* print special delimiter on proxy stats to mark end of
579 static fields */
580 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
581 if (!chunk_strcat(out, "-,"))
582 return 0;
583 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100584 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200585
William Lallemand74c24fb2016-11-21 17:18:36 +0100586 chunk_strcat(out, "\n");
587 return 1;
588}
589
590/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200591static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200592 const struct field *stats,
593 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200594 unsigned int flags,
595 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100596{
597 int field;
598
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200599 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100600 if (!stats[field].type)
601 continue;
602
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200603 switch (domain) {
604 case STATS_DOMAIN_PROXY:
605 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
606 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
607 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
608 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
609 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
610 '?',
611 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
612 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200613 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200614 stats[ST_F_PID].u.u32);
615 break;
616
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200617 case STATS_DOMAIN_DNS:
618 chunk_appendf(out, "D.%d.%s:", field,
619 stat_f[domain][field].name);
620 break;
621
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200622 default:
623 break;
624 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100625
626 if (!stats_emit_field_tags(out, &stats[field], ':'))
627 return 0;
628 if (!stats_emit_typed_data_field(out, &stats[field]))
629 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200630
631 if (flags & STAT_SHOW_FDESC
632 && !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].name)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200633 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200634 }
635
William Lallemand74c24fb2016-11-21 17:18:36 +0100636 if (!chunk_strcat(out, "\n"))
637 return 0;
638 }
639 return 1;
640}
641
Simon Horman05ee2132017-01-04 09:37:25 +0100642/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200643static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200644 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100645{
646 int field;
647 int started = 0;
648
649 if (!chunk_strcat(out, "["))
650 return 0;
651
652 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
653 int old_len;
654
655 if (!field_format(info, field))
656 continue;
657
658 if (started && !chunk_strcat(out, ","))
659 goto err;
660 started = 1;
661
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200662 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100663 chunk_appendf(out,
664 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
665 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200666 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100667 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200668 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100669 goto err;
670
671 if (!stats_emit_json_field_tags(out, &info[field]))
672 goto err;
673
674 if (!stats_emit_json_data_field(out, &info[field]))
675 goto err;
676
677 if (!chunk_strcat(out, "}"))
678 goto err;
679 }
680
681 if (!chunk_strcat(out, "]"))
682 goto err;
683 return 1;
684
685err:
686 chunk_reset(out);
687 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
688 return 0;
689}
690
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200691static void stats_print_proxy_field_json(struct buffer *out,
692 const struct field *stat,
693 const char *name,
694 int pos,
695 uint32_t field_type,
696 uint32_t iid,
697 uint32_t sid,
698 uint32_t pid)
699{
700 const char *obj_type;
701 switch (field_type) {
702 case STATS_TYPE_FE: obj_type = "Frontend"; break;
703 case STATS_TYPE_BE: obj_type = "Backend"; break;
704 case STATS_TYPE_SO: obj_type = "Listener"; break;
705 case STATS_TYPE_SV: obj_type = "Server"; break;
706 default: obj_type = "Unknown"; break;
707 }
708
709 chunk_appendf(out,
710 "{"
711 "\"objType\":\"%s\","
712 "\"proxyId\":%u,"
713 "\"id\":%u,"
714 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
715 "\"processNum\":%u,",
716 obj_type, iid, sid, pos, name, pid);
717}
718
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200719static void stats_print_dns_field_json(struct buffer *out,
720 const struct field *stat,
721 const char *name,
722 int pos)
723{
724 chunk_appendf(out,
725 "{"
726 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
727 pos, name);
728}
729
730
Simon Horman05ee2132017-01-04 09:37:25 +0100731/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200732static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200733 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200734 unsigned int flags,
735 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100736{
737 int field;
738 int started = 0;
739
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200740 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100741 return 0;
742 if (!chunk_strcat(out, "["))
743 return 0;
744
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200745 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100746 int old_len;
747
748 if (!stats[field].type)
749 continue;
750
751 if (started && !chunk_strcat(out, ","))
752 goto err;
753 started = 1;
754
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200755 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200756 if (domain == STATS_DOMAIN_PROXY) {
757 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200758 stat_f[domain][field].name,
759 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200760 stats[ST_F_TYPE].u.u32,
761 stats[ST_F_IID].u.u32,
762 stats[ST_F_SID].u.u32,
763 stats[ST_F_PID].u.u32);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200764 } else if (domain == STATS_DOMAIN_DNS) {
765 stats_print_dns_field_json(out, &stats[field],
766 stat_f[domain][field].name,
767 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200768 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200769
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200770 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100771 goto err;
772
773 if (!stats_emit_json_field_tags(out, &stats[field]))
774 goto err;
775
776 if (!stats_emit_json_data_field(out, &stats[field]))
777 goto err;
778
779 if (!chunk_strcat(out, "}"))
780 goto err;
781 }
782
783 if (!chunk_strcat(out, "]"))
784 goto err;
785
786 return 1;
787
788err:
789 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200790 if (flags & STAT_STARTED)
791 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100792 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
793 return 0;
794}
795
William Lallemand74c24fb2016-11-21 17:18:36 +0100796/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200797 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200798 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
799 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
800 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100801 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200802static int stats_dump_fields_html(struct buffer *out,
803 const struct field *stats,
804 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100805{
Willy Tarreau83061a82018-07-13 11:56:34 +0200806 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200807 struct stats_module *mod;
808 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100809
810 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
811 chunk_appendf(out,
812 /* name, queue */
813 "<tr class=\"frontend\">");
814
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200815 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100816 /* Column sub-heading for Enable or Disable server */
817 chunk_appendf(out, "<td></td>");
818 }
819
820 chunk_appendf(out,
821 "<td class=ac>"
822 "<a name=\"%s/Frontend\"></a>"
823 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
824 "<td colspan=3></td>"
825 "",
826 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
827
828 chunk_appendf(out,
829 /* sessions rate : current */
830 "<td><u>%s<div class=tips><table class=det>"
831 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
832 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
833 "",
834 U2H(stats[ST_F_RATE].u.u32),
835 U2H(stats[ST_F_CONN_RATE].u.u32),
836 U2H(stats[ST_F_RATE].u.u32));
837
838 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
839 chunk_appendf(out,
840 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
841 U2H(stats[ST_F_REQ_RATE].u.u32));
842
843 chunk_appendf(out,
844 "</table></div></u></td>"
845 /* sessions rate : max */
846 "<td><u>%s<div class=tips><table class=det>"
847 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
848 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
849 "",
850 U2H(stats[ST_F_RATE_MAX].u.u32),
851 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
852 U2H(stats[ST_F_RATE_MAX].u.u32));
853
854 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
855 chunk_appendf(out,
856 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
857 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
858
859 chunk_appendf(out,
860 "</table></div></u></td>"
861 /* sessions rate : limit */
862 "<td>%s</td>",
863 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
864
865 chunk_appendf(out,
866 /* sessions: current, max, limit, total */
867 "<td>%s</td><td>%s</td><td>%s</td>"
868 "<td><u>%s<div class=tips><table class=det>"
869 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
870 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
871 "",
872 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
873 U2H(stats[ST_F_STOT].u.u64),
874 U2H(stats[ST_F_CONN_TOT].u.u64),
875 U2H(stats[ST_F_STOT].u.u64));
876
877 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
878 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
879 chunk_appendf(out,
880 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
881 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
882 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
883 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
884 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
885 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
886 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
887 "<tr><th>- other responses:</th><td>%s</td></tr>"
888 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100889 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
890 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200891 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100892 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100893 "",
894 U2H(stats[ST_F_REQ_TOT].u.u64),
895 U2H(stats[ST_F_HRSP_1XX].u.u64),
896 U2H(stats[ST_F_HRSP_2XX].u.u64),
897 U2H(stats[ST_F_COMP_RSP].u.u64),
898 stats[ST_F_HRSP_2XX].u.u64 ?
899 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
900 U2H(stats[ST_F_HRSP_3XX].u.u64),
901 U2H(stats[ST_F_HRSP_4XX].u.u64),
902 U2H(stats[ST_F_HRSP_5XX].u.u64),
903 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200904 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100905 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
906 U2H(stats[ST_F_CACHE_HITS].u.u64),
907 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
908 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100909 U2H(stats[ST_F_WREW].u.u64),
910 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100911 }
912
913 chunk_appendf(out,
914 "</table></div></u></td>"
915 /* sessions: lbtot, lastsess */
916 "<td></td><td></td>"
917 /* bytes : in */
918 "<td>%s</td>"
919 "",
920 U2H(stats[ST_F_BIN].u.u64));
921
922 chunk_appendf(out,
923 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
924 "<td>%s%s<div class=tips><table class=det>"
925 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
926 "<tr><th>Compression in:</th><td>%s</td></tr>"
927 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
928 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
929 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
930 "</table></div>%s</td>",
931 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
932 U2H(stats[ST_F_BOUT].u.u64),
933 U2H(stats[ST_F_BOUT].u.u64),
934 U2H(stats[ST_F_COMP_IN].u.u64),
935 U2H(stats[ST_F_COMP_OUT].u.u64),
936 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
937 U2H(stats[ST_F_COMP_BYP].u.u64),
938 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
939 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,
940 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
941
942 chunk_appendf(out,
943 /* denied: req, resp */
944 "<td>%s</td><td>%s</td>"
945 /* errors : request, connect, response */
946 "<td>%s</td><td></td><td></td>"
947 /* warnings: retries, redispatches */
948 "<td></td><td></td>"
949 /* server status : reflect frontend status */
950 "<td class=ac>%s</td>"
951 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200952 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100953 "",
954 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
955 U2H(stats[ST_F_EREQ].u.u64),
956 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200957
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200958 if (flags & STAT_SHMODULES) {
959 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100960 chunk_appendf(out, "<td>");
961
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200962 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100963 chunk_appendf(out,
964 "<u>%s<div class=tips><table class=det>",
965 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200966 for (j = 0; j < mod->stats_count; ++j) {
967 chunk_appendf(out,
968 "<tr><th>%s</th><td>%s</td></tr>",
969 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
970 ++i;
971 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100972 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200973 } else {
974 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200975 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200976
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100977 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200978 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200979 }
980
981 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100982 }
983 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
984 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200985 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100986 /* Column sub-heading for Enable or Disable server */
987 chunk_appendf(out, "<td></td>");
988 }
989
990 chunk_appendf(out,
991 /* frontend name, listener name */
992 "<td class=ac><a name=\"%s/+%s\"></a>%s"
993 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
994 "",
995 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200996 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100997 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
998
Willy Tarreau708c4162019-10-09 10:19:16 +0200999 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001000 chunk_appendf(out, "<div class=tips>");
1001
Willy Tarreau90807112020-02-25 08:16:33 +01001002 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001003 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1004 else if (*field_str(stats, ST_F_ADDR) == '[')
1005 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1006 else if (*field_str(stats, ST_F_ADDR))
1007 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1008
1009 /* id */
1010 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1011 }
1012
1013 chunk_appendf(out,
1014 /* queue */
1015 "%s</td><td colspan=3></td>"
1016 /* sessions rate: current, max, limit */
1017 "<td colspan=3>&nbsp;</td>"
1018 /* sessions: current, max, limit, total, lbtot, lastsess */
1019 "<td>%s</td><td>%s</td><td>%s</td>"
1020 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1021 /* bytes: in, out */
1022 "<td>%s</td><td>%s</td>"
1023 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001024 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001025 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1026 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1027
1028 chunk_appendf(out,
1029 /* denied: req, resp */
1030 "<td>%s</td><td>%s</td>"
1031 /* errors: request, connect, response */
1032 "<td>%s</td><td></td><td></td>"
1033 /* warnings: retries, redispatches */
1034 "<td></td><td></td>"
1035 /* server status: reflect listener status */
1036 "<td class=ac>%s</td>"
1037 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001038 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001039 "",
1040 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1041 U2H(stats[ST_F_EREQ].u.u64),
1042 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001043
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001044 if (flags & STAT_SHMODULES) {
1045 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001046 chunk_appendf(out, "<td>");
1047
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001048 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001049 chunk_appendf(out,
1050 "<u>%s<div class=tips><table class=det>",
1051 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001052 for (j = 0; j < mod->stats_count; ++j) {
1053 chunk_appendf(out,
1054 "<tr><th>%s</th><td>%s</td></tr>",
1055 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1056 ++i;
1057 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001058 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001059 } else {
1060 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001061 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001062
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001063 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001064 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001065 }
1066
1067 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001068 }
1069 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1070 const char *style;
1071
1072 /* determine the style to use depending on the server's state,
1073 * its health and weight. There isn't a 1-to-1 mapping between
1074 * state and styles for the cases where the server is (still)
1075 * up. The reason is that we don't want to report nolb and
1076 * drain with the same color.
1077 */
1078
1079 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1080 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1081 style = "down";
1082 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001083 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001084 style = "going_up";
1085 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001086 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1087 style = "draining";
1088 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001089 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001090 style = "going_down";
1091 }
1092 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1093 style = "nolb";
1094 }
1095 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1096 style = "no_check";
1097 }
1098 else if (!stats[ST_F_CHKFAIL].type ||
1099 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1100 /* no check or max health = UP */
1101 if (stats[ST_F_WEIGHT].u.u32)
1102 style = "up";
1103 else
1104 style = "draining";
1105 }
1106 else {
1107 style = "going_down";
1108 }
1109
Willy Tarreau7b524852020-08-11 10:26:36 +02001110 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001111 chunk_appendf(out, "<tr class=\"maintain\">");
1112 else
1113 chunk_appendf(out,
1114 "<tr class=\"%s_%s\">",
1115 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1116
1117
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001118 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001119 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001120 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1121 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001122 field_str(stats, ST_F_SVNAME));
1123
1124 chunk_appendf(out,
1125 "<td class=ac><a name=\"%s/%s\"></a>%s"
1126 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1127 "",
1128 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001129 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001130 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1131
Willy Tarreau708c4162019-10-09 10:19:16 +02001132 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001133 chunk_appendf(out, "<div class=tips>");
1134
Willy Tarreau90807112020-02-25 08:16:33 +01001135 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001136 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1137 else if (*field_str(stats, ST_F_ADDR) == '[')
1138 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1139 else if (*field_str(stats, ST_F_ADDR))
1140 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1141
1142 /* id */
1143 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1144
1145 /* cookie */
1146 if (stats[ST_F_COOKIE].type) {
1147 chunk_appendf(out, ", cookie: '");
1148 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1149 chunk_htmlencode(out, &src);
1150 chunk_appendf(out, "'");
1151 }
1152
1153 chunk_appendf(out, "</div>");
1154 }
1155
1156 chunk_appendf(out,
1157 /* queue : current, max, limit */
1158 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1159 /* sessions rate : current, max, limit */
1160 "<td>%s</td><td>%s</td><td></td>"
1161 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001162 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001163 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1164 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1165
1166 chunk_appendf(out,
1167 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001168 "<td><u>%s<div class=tips>"
1169 "<table class=det>"
1170 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001171 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001172 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001173 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1174 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001175 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001176 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1177 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1178 "</table></div></u>"
1179 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001180 "<td><u>%s<div class=tips><table class=det>"
1181 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1182 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001183 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001184 U2H(stats[ST_F_SCUR].u.u32),
1185 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1186 U2H(stats[ST_F_SRV_ICUR].u.u32),
1187 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1188 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001189 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001190
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001191 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1192 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1193 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001194 U2H(stats[ST_F_STOT].u.u64),
1195 U2H(stats[ST_F_STOT].u.u64));
1196
1197 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1198 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001199 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001200 "<tr><th>New connections:</th><td>%s</td></tr>"
1201 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001202 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001203 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1204 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1205 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1206 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1207 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1208 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001209 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001210 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001211 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001212 U2H(stats[ST_F_CONNECT].u.u64),
1213 U2H(stats[ST_F_REUSE].u.u64),
1214 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1215 (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 +02001216 U2H(stats[ST_F_REQ_TOT].u.u64),
1217 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1218 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1219 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1220 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1221 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1222 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1223 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1224 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1225 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1226 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1227 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1228 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001229 U2H(stats[ST_F_WREW].u.u64),
1230 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001231 }
1232
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001233 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1234 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1235 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1236 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1237 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001238 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001239 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1240 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1241 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1242 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001243
1244 chunk_appendf(out,
1245 "</table></div></u></td>"
1246 /* sessions: lbtot, last */
1247 "<td>%s</td><td>%s</td>",
1248 U2H(stats[ST_F_LBTOT].u.u64),
1249 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1250
1251 chunk_appendf(out,
1252 /* bytes : in, out */
1253 "<td>%s</td><td>%s</td>"
1254 /* denied: req, resp */
1255 "<td></td><td>%s</td>"
1256 /* errors : request, connect */
1257 "<td></td><td>%s</td>"
1258 /* errors : response */
1259 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1260 /* warnings: retries, redispatches */
1261 "<td>%lld</td><td>%lld</td>"
1262 "",
1263 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1264 U2H(stats[ST_F_DRESP].u.u64),
1265 U2H(stats[ST_F_ECON].u.u64),
1266 U2H(stats[ST_F_ERESP].u.u64),
1267 (long long)stats[ST_F_CLI_ABRT].u.u64,
1268 (long long)stats[ST_F_SRV_ABRT].u.u64,
1269 (long long)stats[ST_F_WRETR].u.u64,
1270 (long long)stats[ST_F_WREDIS].u.u64);
1271
1272 /* status, last change */
1273 chunk_appendf(out, "<td class=ac>");
1274
1275 /* FIXME!!!!
1276 * LASTCHG should contain the last change for *this* server and must be computed
1277 * properly above, as was done below, ie: this server if maint, otherwise ref server
1278 * if tracking. Note that ref is either local or remote depending on tracking.
1279 */
1280
1281
Willy Tarreau7b524852020-08-11 10:26:36 +02001282 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001283 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1284 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001285 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001286 chunk_strcat(out, "<i>no check</i>");
1287 }
1288 else {
1289 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 +02001290 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001291 if (stats[ST_F_CHECK_HEALTH].u.u32)
1292 chunk_strcat(out, " &uarr;");
1293 }
1294 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1295 chunk_strcat(out, " &darr;");
1296 }
1297
Willy Tarreau7b524852020-08-11 10:26:36 +02001298 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001299 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1300 chunk_appendf(out,
1301 "</td><td class=ac><u> %s",
1302 field_str(stats, ST_F_AGENT_STATUS));
1303
1304 if (stats[ST_F_AGENT_CODE].type)
1305 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1306
1307 if (stats[ST_F_AGENT_DURATION].type)
1308 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1309
1310 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1311
1312 if (*field_str(stats, ST_F_LAST_AGT)) {
1313 chunk_appendf(out, ": ");
1314 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1315 chunk_htmlencode(out, &src);
1316 }
1317 chunk_appendf(out, "</div></u>");
1318 }
1319 else if (stats[ST_F_CHECK_STATUS].type) {
1320 chunk_appendf(out,
1321 "</td><td class=ac><u> %s",
1322 field_str(stats, ST_F_CHECK_STATUS));
1323
1324 if (stats[ST_F_CHECK_CODE].type)
1325 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1326
1327 if (stats[ST_F_CHECK_DURATION].type)
1328 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1329
1330 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1331
1332 if (*field_str(stats, ST_F_LAST_CHK)) {
1333 chunk_appendf(out, ": ");
1334 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1335 chunk_htmlencode(out, &src);
1336 }
1337 chunk_appendf(out, "</div></u>");
1338 }
1339 else
1340 chunk_appendf(out, "</td><td>");
1341
1342 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001343 /* weight / uweight */
1344 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001345 /* act, bck */
1346 "<td class=ac>%s</td><td class=ac>%s</td>"
1347 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001348 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001349 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1350 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1351
1352 /* check failures: unique, fatal, down time */
1353 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1354 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1355 }
1356 else if (stats[ST_F_CHKFAIL].type) {
1357 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1358
1359 if (stats[ST_F_HANAFAIL].type)
1360 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1361
1362 chunk_appendf(out,
1363 "<div class=tips>Failed Health Checks%s</div></u></td>"
1364 "<td>%lld</td><td>%s</td>"
1365 "",
1366 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1367 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1368 }
1369 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1370 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1371 chunk_appendf(out,
1372 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1373 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1374 }
1375 else
1376 chunk_appendf(out, "<td colspan=3></td>");
1377
1378 /* throttle */
1379 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001380 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001381 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001382 chunk_appendf(out, "<td class=ac>-</td>");
1383
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001384 if (flags & STAT_SHMODULES) {
1385 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001386 chunk_appendf(out, "<td>");
1387
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001388 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001389 chunk_appendf(out,
1390 "<u>%s<div class=tips><table class=det>",
1391 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001392 for (j = 0; j < mod->stats_count; ++j) {
1393 chunk_appendf(out,
1394 "<tr><th>%s</th><td>%s</td></tr>",
1395 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1396 ++i;
1397 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001398 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001399 } else {
1400 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001401 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001402
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001403 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001404 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001405 }
1406
1407 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001408 }
1409 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1410 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001411 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001412 /* Column sub-heading for Enable or Disable server */
1413 chunk_appendf(out, "<td></td>");
1414 }
1415 chunk_appendf(out,
1416 "<td class=ac>"
1417 /* name */
1418 "%s<a name=\"%s/Backend\"></a>"
1419 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1420 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001421 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001422 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1423
Willy Tarreau708c4162019-10-09 10:19:16 +02001424 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001425 /* balancing */
1426 chunk_appendf(out, "<div class=tips>balancing: %s",
1427 field_str(stats, ST_F_ALGO));
1428
1429 /* cookie */
1430 if (stats[ST_F_COOKIE].type) {
1431 chunk_appendf(out, ", cookie: '");
1432 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1433 chunk_htmlencode(out, &src);
1434 chunk_appendf(out, "'");
1435 }
1436 chunk_appendf(out, "</div>");
1437 }
1438
1439 chunk_appendf(out,
1440 "%s</td>"
1441 /* queue : current, max */
1442 "<td>%s</td><td>%s</td><td></td>"
1443 /* sessions rate : current, max, limit */
1444 "<td>%s</td><td>%s</td><td></td>"
1445 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001446 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001447 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1448 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1449
1450 chunk_appendf(out,
1451 /* sessions: current, max, limit, total */
1452 "<td>%s</td><td>%s</td><td>%s</td>"
1453 "<td><u>%s<div class=tips><table class=det>"
1454 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1455 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001456 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 +01001457 U2H(stats[ST_F_STOT].u.u64),
1458 U2H(stats[ST_F_STOT].u.u64));
1459
1460 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1461 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1462 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001463 "<tr><th>New connections:</th><td>%s</td></tr>"
1464 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001465 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1466 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1467 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1468 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1469 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1470 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1471 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1472 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001473 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1474 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001475 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001476 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001477 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001478 U2H(stats[ST_F_CONNECT].u.u64),
1479 U2H(stats[ST_F_REUSE].u.u64),
1480 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1481 (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 +01001482 U2H(stats[ST_F_REQ_TOT].u.u64),
1483 U2H(stats[ST_F_HRSP_1XX].u.u64),
1484 U2H(stats[ST_F_HRSP_2XX].u.u64),
1485 U2H(stats[ST_F_COMP_RSP].u.u64),
1486 stats[ST_F_HRSP_2XX].u.u64 ?
1487 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1488 U2H(stats[ST_F_HRSP_3XX].u.u64),
1489 U2H(stats[ST_F_HRSP_4XX].u.u64),
1490 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001491 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001492 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1493 U2H(stats[ST_F_CACHE_HITS].u.u64),
1494 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1495 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001496 U2H(stats[ST_F_WREW].u.u64),
1497 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001498 }
1499
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001500 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1501 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1502 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1503 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1504 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001505 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001506 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1507 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1508 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1509 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001510
1511 chunk_appendf(out,
1512 "</table></div></u></td>"
1513 /* sessions: lbtot, last */
1514 "<td>%s</td><td>%s</td>"
1515 /* bytes: in */
1516 "<td>%s</td>"
1517 "",
1518 U2H(stats[ST_F_LBTOT].u.u64),
1519 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1520 U2H(stats[ST_F_BIN].u.u64));
1521
1522 chunk_appendf(out,
1523 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1524 "<td>%s%s<div class=tips><table class=det>"
1525 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1526 "<tr><th>Compression in:</th><td>%s</td></tr>"
1527 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1528 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1529 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1530 "</table></div>%s</td>",
1531 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1532 U2H(stats[ST_F_BOUT].u.u64),
1533 U2H(stats[ST_F_BOUT].u.u64),
1534 U2H(stats[ST_F_COMP_IN].u.u64),
1535 U2H(stats[ST_F_COMP_OUT].u.u64),
1536 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1537 U2H(stats[ST_F_COMP_BYP].u.u64),
1538 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1539 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,
1540 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1541
1542 chunk_appendf(out,
1543 /* denied: req, resp */
1544 "<td>%s</td><td>%s</td>"
1545 /* errors : request, connect */
1546 "<td></td><td>%s</td>"
1547 /* errors : response */
1548 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1549 /* warnings: retries, redispatches */
1550 "<td>%lld</td><td>%lld</td>"
1551 /* backend status: reflect backend status (up/down): we display UP
1552 * if the backend has known working servers or if it has no server at
1553 * all (eg: for stats). Then we display the total weight, number of
1554 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001555 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001556 "<td class=ac>%d</td><td class=ac>%d</td>"
1557 "",
1558 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1559 U2H(stats[ST_F_ECON].u.u64),
1560 U2H(stats[ST_F_ERESP].u.u64),
1561 (long long)stats[ST_F_CLI_ABRT].u.u64,
1562 (long long)stats[ST_F_SRV_ABRT].u.u64,
1563 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1564 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1565 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 +02001566 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001567 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1568
1569 chunk_appendf(out,
1570 /* rest of backend: nothing, down transitions, total downtime, throttle */
1571 "<td class=ac>&nbsp;</td><td>%d</td>"
1572 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001573 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001574 stats[ST_F_CHKDOWN].u.u32,
1575 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001576
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001577 if (flags & STAT_SHMODULES) {
1578 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001579 chunk_appendf(out, "<td>");
1580
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001581 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001582 chunk_appendf(out,
1583 "<u>%s<div class=tips><table class=det>",
1584 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001585 for (j = 0; j < mod->stats_count; ++j) {
1586 chunk_appendf(out,
1587 "<tr><th>%s</th><td>%s</td></tr>",
1588 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1589 ++i;
1590 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001591 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001592 } else {
1593 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001594 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001595
1596 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001597 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001598 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001599
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001600 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001601 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001602
William Lallemand74c24fb2016-11-21 17:18:36 +01001603 return 1;
1604}
1605
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001606int stats_dump_one_line(const struct field *stats, size_t stats_count,
1607 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001608{
Simon Horman05ee2132017-01-04 09:37:25 +01001609 int ret;
1610
William Lallemand74c24fb2016-11-21 17:18:36 +01001611 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001612 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001613 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001614 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001615 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001616 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001617 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001618 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001619
1620 if (ret)
1621 appctx->ctx.stats.flags |= STAT_STARTED;
1622
1623 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001624}
1625
William Dauchy0ef54392021-01-17 18:27:45 +01001626/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001627 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1628 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1629 * this value, or if the selected field is not implemented for frontends, the
1630 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001631 */
William Dauchy0ef54392021-01-17 18:27:45 +01001632int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1633 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001634{
William Dauchy0ef54392021-01-17 18:27:45 +01001635 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001636
William Lallemand74c24fb2016-11-21 17:18:36 +01001637 if (len < ST_F_TOTAL_FIELDS)
1638 return 0;
1639
William Dauchy0ef54392021-01-17 18:27:45 +01001640 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001641 struct field metric = { 0 };
1642
William Dauchy0ef54392021-01-17 18:27:45 +01001643 switch (current_field) {
1644 case ST_F_PXNAME:
1645 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1646 break;
1647 case ST_F_SVNAME:
1648 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1649 break;
1650 case ST_F_MODE:
1651 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1652 break;
1653 case ST_F_SCUR:
1654 metric = mkf_u32(0, px->feconn);
1655 break;
1656 case ST_F_SMAX:
1657 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1658 break;
1659 case ST_F_SLIM:
1660 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1661 break;
1662 case ST_F_STOT:
1663 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1664 break;
1665 case ST_F_BIN:
1666 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1667 break;
1668 case ST_F_BOUT:
1669 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1670 break;
1671 case ST_F_DREQ:
1672 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1673 break;
1674 case ST_F_DRESP:
1675 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1676 break;
1677 case ST_F_EREQ:
1678 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1679 break;
1680 case ST_F_DCON:
1681 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1682 break;
1683 case ST_F_DSES:
1684 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1685 break;
1686 case ST_F_STATUS:
1687 metric = mkf_str(FO_STATUS, px->disabled ? "STOP" : "OPEN");
1688 break;
1689 case ST_F_PID:
1690 metric = mkf_u32(FO_KEY, relative_pid);
1691 break;
1692 case ST_F_IID:
1693 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1694 break;
1695 case ST_F_SID:
1696 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1697 break;
1698 case ST_F_TYPE:
1699 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1700 break;
1701 case ST_F_RATE:
1702 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1703 break;
1704 case ST_F_RATE_LIM:
1705 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1706 break;
1707 case ST_F_RATE_MAX:
1708 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1709 break;
1710 case ST_F_WREW:
1711 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1712 break;
1713 case ST_F_EINT:
1714 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1715 break;
1716 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001717 if (px->mode == PR_MODE_HTTP)
1718 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001719 break;
1720 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001721 if (px->mode == PR_MODE_HTTP)
1722 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001723 break;
1724 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001725 if (px->mode == PR_MODE_HTTP)
1726 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001727 break;
1728 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001729 if (px->mode == PR_MODE_HTTP)
1730 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001731 break;
1732 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001733 if (px->mode == PR_MODE_HTTP)
1734 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001735 break;
1736 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001737 if (px->mode == PR_MODE_HTTP)
1738 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001739 break;
1740 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001741 if (px->mode == PR_MODE_HTTP)
1742 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001743 break;
1744 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001745 if (px->mode == PR_MODE_HTTP)
1746 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001747 break;
1748 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001749 if (px->mode == PR_MODE_HTTP)
1750 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001751 break;
1752 case ST_F_REQ_RATE:
1753 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1754 break;
1755 case ST_F_REQ_RATE_MAX:
1756 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1757 break;
1758 case ST_F_REQ_TOT:
1759 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1760 break;
1761 case ST_F_COMP_IN:
1762 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1763 break;
1764 case ST_F_COMP_OUT:
1765 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1766 break;
1767 case ST_F_COMP_BYP:
1768 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1769 break;
1770 case ST_F_COMP_RSP:
1771 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1772 break;
1773 case ST_F_CONN_RATE:
1774 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1775 break;
1776 case ST_F_CONN_RATE_MAX:
1777 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1778 break;
1779 case ST_F_CONN_TOT:
1780 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1781 break;
1782 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001783 /* not used for frontends. If a specific metric
1784 * is requested, return an error. Otherwise continue.
1785 */
1786 if (selected_field != NULL)
1787 return 0;
1788 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001789 }
1790 stats[current_field] = metric;
1791 if (selected_field != NULL)
1792 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001793 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001794 return 1;
1795}
1796
1797/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1798 * the state from stream interface <si>. The caller is responsible for clearing
1799 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1800 */
1801static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1802{
1803 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001804 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1805 struct stats_module *mod;
1806 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001807
1808 if (!(px->cap & PR_CAP_FE))
1809 return 0;
1810
1811 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1812 return 0;
1813
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001814 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1815
William Dauchy0ef54392021-01-17 18:27:45 +01001816 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001817 return 0;
1818
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001819 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1820 void *counters;
1821
1822 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1823 stats_count += mod->stats_count;
1824 continue;
1825 }
1826
1827 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1828 mod->fill_stats(counters, stats + stats_count);
1829 stats_count += mod->stats_count;
1830 }
1831
1832 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001833}
1834
William Dauchy655e14e2021-02-14 23:22:54 +01001835/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1836 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1837 * this length is less then this value, the function returns 0, otherwise, it
1838 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1839 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001840 */
1841int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001842 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001843{
William Dauchy655e14e2021-02-14 23:22:54 +01001844 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001845 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001846
1847 if (len < ST_F_TOTAL_FIELDS)
1848 return 0;
1849
1850 if (!l->counters)
1851 return 0;
1852
1853 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001854
William Dauchy655e14e2021-02-14 23:22:54 +01001855 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1856 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001857
William Dauchy655e14e2021-02-14 23:22:54 +01001858 switch (current_field) {
1859 case ST_F_PXNAME:
1860 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1861 break;
1862 case ST_F_SVNAME:
1863 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1864 break;
1865 case ST_F_MODE:
1866 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1867 break;
1868 case ST_F_SCUR:
1869 metric = mkf_u32(0, l->nbconn);
1870 break;
1871 case ST_F_SMAX:
1872 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1873 break;
1874 case ST_F_SLIM:
1875 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1876 break;
1877 case ST_F_STOT:
1878 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1879 break;
1880 case ST_F_BIN:
1881 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1882 break;
1883 case ST_F_BOUT:
1884 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1885 break;
1886 case ST_F_DREQ:
1887 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1888 break;
1889 case ST_F_DRESP:
1890 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1891 break;
1892 case ST_F_EREQ:
1893 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1894 break;
1895 case ST_F_DCON:
1896 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1897 break;
1898 case ST_F_DSES:
1899 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1900 break;
1901 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001902 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001903 break;
1904 case ST_F_PID:
1905 metric = mkf_u32(FO_KEY, relative_pid);
1906 break;
1907 case ST_F_IID:
1908 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1909 break;
1910 case ST_F_SID:
1911 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1912 break;
1913 case ST_F_TYPE:
1914 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1915 break;
1916 case ST_F_WREW:
1917 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1918 break;
1919 case ST_F_EINT:
1920 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1921 break;
1922 case ST_F_ADDR:
1923 if (flags & STAT_SHLGNDS) {
1924 char str[INET6_ADDRSTRLEN];
1925 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001926
William Dauchy655e14e2021-02-14 23:22:54 +01001927 port = get_host_port(&l->rx.addr);
1928 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1929 case AF_INET:
1930 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1931 chunk_appendf(out, "%s:%d", str, port);
1932 break;
1933 case AF_INET6:
1934 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1935 chunk_appendf(out, "[%s]:%d", str, port);
1936 break;
1937 case AF_UNIX:
1938 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1939 break;
1940 case -1:
1941 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1942 chunk_strcat(out, strerror(errno));
1943 break;
1944 default: /* address family not supported */
1945 break;
1946 }
1947 }
1948 break;
1949 default:
1950 /* not used for listen. If a specific metric
1951 * is requested, return an error. Otherwise continue.
1952 */
1953 if (selected_field != NULL)
1954 return 0;
1955 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001956 }
William Dauchy655e14e2021-02-14 23:22:54 +01001957 stats[current_field] = metric;
1958 if (selected_field != NULL)
1959 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001960 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001961 return 1;
1962}
1963
1964/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001965 * from stream interface <si>. The caller is responsible for clearing the trash
1966 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001967 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001968static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001969{
1970 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001971 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1972 struct stats_module *mod;
1973 size_t stats_count = ST_F_TOTAL_FIELDS;
1974
1975 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001976
William Dauchy655e14e2021-02-14 23:22:54 +01001977 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats,
1978 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001979 return 0;
1980
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001981 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1982 void *counters;
1983
1984 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1985 stats_count += mod->stats_count;
1986 continue;
1987 }
1988
1989 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
1990 mod->fill_stats(counters, stats + stats_count);
1991 stats_count += mod->stats_count;
1992 }
1993
1994 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001995}
1996
1997enum srv_stats_state {
1998 SRV_STATS_STATE_DOWN = 0,
1999 SRV_STATS_STATE_DOWN_AGENT,
2000 SRV_STATS_STATE_GOING_UP,
2001 SRV_STATS_STATE_UP_GOING_DOWN,
2002 SRV_STATS_STATE_UP,
2003 SRV_STATS_STATE_NOLB_GOING_DOWN,
2004 SRV_STATS_STATE_NOLB,
2005 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2006 SRV_STATS_STATE_DRAIN,
2007 SRV_STATS_STATE_DRAIN_AGENT,
2008 SRV_STATS_STATE_NO_CHECK,
2009
2010 SRV_STATS_STATE_COUNT, /* Must be last */
2011};
2012
2013static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2014 [SRV_STATS_STATE_DOWN] = "DOWN",
2015 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2016 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2017 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2018 [SRV_STATS_STATE_UP] = "UP",
2019 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2020 [SRV_STATS_STATE_NOLB] = "NOLB",
2021 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2022 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2023 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2024 [SRV_STATS_STATE_NO_CHECK] = "no check"
2025};
2026
William Dauchyd3a9a492021-01-25 17:29:03 +01002027/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002028 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002029static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2030 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002031{
Emeric Brun52a91d32017-08-31 14:41:55 +02002032 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002033 if ((ref->check.state & CHK_ST_ENABLED) &&
2034 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002035 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002036 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002037 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002038 }
2039
Emeric Brun52a91d32017-08-31 14:41:55 +02002040 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002041 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002042 *state = SRV_STATS_STATE_DRAIN_AGENT;
2043 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2044 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002045 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002046 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002047 }
2048
William Dauchyd3a9a492021-01-25 17:29:03 +01002049 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2050 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002051 }
2052 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002053 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002054 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2055 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002056 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002057 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002058 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002059 }
2060 }
2061 else { /* stopped */
2062 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002063 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002064 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002065 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002066 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002067 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002068 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002069 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002070 }
2071 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002072}
William Lallemand74c24fb2016-11-21 17:18:36 +01002073
William Dauchyd3a9a492021-01-25 17:29:03 +01002074/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2075 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2076 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2077 * this value, or if the selected field is not implemented for servers, the
2078 * function returns 0, otherwise, it returns 1. <flags> can take the value
2079 * STAT_SHLGNDS.
2080 */
2081int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2082 struct field *stats, int len,
2083 enum stat_field *selected_field)
2084{
2085 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2086 struct server *via = sv->track ? sv->track : sv;
2087 struct server *ref = via;
2088 enum srv_stats_state state = 0;
2089 char str[INET6_ADDRSTRLEN];
2090 struct buffer *out = get_trash_chunk();
2091 char *fld_status;
2092 long long srv_samples_counter;
2093 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002094
William Dauchyd3a9a492021-01-25 17:29:03 +01002095 if (len < ST_F_TOTAL_FIELDS)
2096 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002097
William Dauchyd3a9a492021-01-25 17:29:03 +01002098 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002099
William Dauchyd3a9a492021-01-25 17:29:03 +01002100 /* compute state for later use */
2101 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2102 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2103 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2104 /* we have "via" which is the tracked server as described in the configuration,
2105 * and "ref" which is the checked server and the end of the chain.
2106 */
2107 while (ref->track)
2108 ref = ref->track;
2109 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002110 }
2111
William Dauchyd3a9a492021-01-25 17:29:03 +01002112 /* compue time values for later use */
2113 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2114 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2115 *selected_field == ST_F_TTIME) {
2116 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2117 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2118 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002119 }
2120
William Dauchyd3a9a492021-01-25 17:29:03 +01002121 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2122 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002123
William Dauchyd3a9a492021-01-25 17:29:03 +01002124 switch (current_field) {
2125 case ST_F_PXNAME:
2126 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2127 break;
2128 case ST_F_SVNAME:
2129 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2130 break;
2131 case ST_F_MODE:
2132 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002133 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002134 case ST_F_QCUR:
William Dauchyd3a9a492021-01-25 17:29:03 +01002135 metric = mkf_u32(0, sv->nbpend);
2136 break;
2137 case ST_F_QMAX:
2138 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2139 break;
2140 case ST_F_SCUR:
2141 metric = mkf_u32(0, sv->cur_sess);
2142 break;
2143 case ST_F_SMAX:
2144 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2145 break;
2146 case ST_F_SLIM:
2147 if (sv->maxconn)
2148 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2149 break;
2150 case ST_F_SRV_ICUR:
2151 metric = mkf_u32(0, sv->curr_idle_conns);
2152 break;
2153 case ST_F_SRV_ILIM:
2154 if (sv->max_idle_conns != -1)
2155 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2156 break;
2157 case ST_F_STOT:
2158 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2159 break;
2160 case ST_F_BIN:
2161 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2162 break;
2163 case ST_F_BOUT:
2164 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2165 break;
2166 case ST_F_DRESP:
2167 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2168 break;
2169 case ST_F_ECON:
2170 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2171 break;
2172 case ST_F_ERESP:
2173 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2174 break;
2175 case ST_F_WRETR:
2176 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2177 break;
2178 case ST_F_WREDIS:
2179 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2180 break;
2181 case ST_F_WREW:
2182 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2183 break;
2184 case ST_F_EINT:
2185 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2186 break;
2187 case ST_F_CONNECT:
2188 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2189 break;
2190 case ST_F_REUSE:
2191 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2192 break;
2193 case ST_F_IDLE_CONN_CUR:
2194 metric = mkf_u32(0, sv->curr_idle_nb);
2195 break;
2196 case ST_F_SAFE_CONN_CUR:
2197 metric = mkf_u32(0, sv->curr_safe_nb);
2198 break;
2199 case ST_F_USED_CONN_CUR:
2200 metric = mkf_u32(0, sv->curr_used_conns);
2201 break;
2202 case ST_F_NEED_CONN_EST:
2203 metric = mkf_u32(0, sv->est_need_conns);
2204 break;
2205 case ST_F_STATUS:
2206 fld_status = chunk_newstr(out);
2207 if (sv->cur_admin & SRV_ADMF_RMAINT)
2208 chunk_appendf(out, "MAINT (resolution)");
2209 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2210 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2211 else if (sv->cur_admin & SRV_ADMF_MAINT)
2212 chunk_appendf(out, "MAINT");
2213 else
2214 chunk_appendf(out,
2215 srv_hlt_st[state],
2216 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2217 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002218
William Dauchyd3a9a492021-01-25 17:29:03 +01002219 metric = mkf_str(FO_STATUS, fld_status);
2220 break;
2221 case ST_F_LASTCHG:
2222 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2223 break;
2224 case ST_F_WEIGHT:
2225 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2226 break;
2227 case ST_F_UWEIGHT:
2228 metric = mkf_u32(FN_AVG, sv->uweight);
2229 break;
2230 case ST_F_ACT:
2231 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2232 break;
2233 case ST_F_BCK:
2234 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2235 break;
2236 case ST_F_CHKFAIL:
2237 if (sv->check.state & CHK_ST_ENABLED)
2238 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2239 break;
2240 case ST_F_CHKDOWN:
2241 if (sv->check.state & CHK_ST_ENABLED)
2242 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2243 break;
2244 case ST_F_DOWNTIME:
2245 if (sv->check.state & CHK_ST_ENABLED)
2246 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2247 break;
2248 case ST_F_QLIMIT:
2249 if (sv->maxqueue)
2250 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2251 break;
2252 case ST_F_PID:
2253 metric = mkf_u32(FO_KEY, relative_pid);
2254 break;
2255 case ST_F_IID:
2256 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2257 break;
2258 case ST_F_SID:
2259 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2260 break;
2261 case ST_F_THROTTLE:
2262 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2263 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2264 break;
2265 case ST_F_LBTOT:
2266 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2267 break;
2268 case ST_F_TRACKED:
2269 if (sv->track) {
2270 char *fld_track = chunk_newstr(out);
2271 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2272 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2273 }
2274 break;
2275 case ST_F_TYPE:
2276 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2277 break;
2278 case ST_F_RATE:
2279 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2280 break;
2281 case ST_F_RATE_MAX:
2282 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2283 break;
2284 case ST_F_CHECK_STATUS:
2285 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2286 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002287
William Dauchyd3a9a492021-01-25 17:29:03 +01002288 fld_chksts = chunk_newstr(out);
2289 chunk_strcat(out, "* "); // for check in progress
2290 chunk_strcat(out, get_check_status_info(sv->check.status));
2291 if (!(sv->check.state & CHK_ST_INPROGRESS))
2292 fld_chksts += 2; // skip "* "
2293 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
2294 }
2295 break;
2296 case ST_F_CHECK_CODE:
2297 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2298 sv->check.status >= HCHK_STATUS_L57DATA)
2299 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2300 break;
2301 case ST_F_CHECK_DURATION:
2302 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2303 sv->check.status >= HCHK_STATUS_CHECKED)
2304 metric = mkf_u64(FN_DURATION, sv->check.duration);
2305 break;
2306 case ST_F_CHECK_DESC:
2307 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2308 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2309 break;
2310 case ST_F_LAST_CHK:
2311 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2312 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2313 break;
2314 case ST_F_CHECK_RISE:
2315 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2316 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2317 break;
2318 case ST_F_CHECK_FALL:
2319 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2320 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2321 break;
2322 case ST_F_CHECK_HEALTH:
2323 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2324 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2325 break;
2326 case ST_F_REQ_TOT:
2327 if (px->mode == PR_MODE_HTTP)
2328 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2329 break;
2330 case ST_F_HRSP_1XX:
2331 if (px->mode == PR_MODE_HTTP)
2332 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2333 break;
2334 case ST_F_HRSP_2XX:
2335 if (px->mode == PR_MODE_HTTP)
2336 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2337 break;
2338 case ST_F_HRSP_3XX:
2339 if (px->mode == PR_MODE_HTTP)
2340 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2341 break;
2342 case ST_F_HRSP_4XX:
2343 if (px->mode == PR_MODE_HTTP)
2344 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2345 break;
2346 case ST_F_HRSP_5XX:
2347 if (px->mode == PR_MODE_HTTP)
2348 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2349 break;
2350 case ST_F_HRSP_OTHER:
2351 if (px->mode == PR_MODE_HTTP)
2352 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2353 break;
2354 case ST_F_HANAFAIL:
2355 if (ref->observe)
2356 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2357 break;
2358 case ST_F_CLI_ABRT:
2359 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2360 break;
2361 case ST_F_SRV_ABRT:
2362 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2363 break;
2364 case ST_F_LASTSESS:
2365 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2366 break;
2367 case ST_F_QTIME:
2368 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2369 break;
2370 case ST_F_CTIME:
2371 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2372 break;
2373 case ST_F_RTIME:
2374 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2375 break;
2376 case ST_F_TTIME:
2377 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2378 break;
2379 case ST_F_QT_MAX:
2380 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2381 break;
2382 case ST_F_CT_MAX:
2383 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2384 break;
2385 case ST_F_RT_MAX:
2386 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2387 break;
2388 case ST_F_TT_MAX:
2389 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2390 break;
2391 case ST_F_ADDR:
2392 if (flags & STAT_SHLGNDS) {
2393 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2394 case AF_INET:
2395 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2396 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2397 break;
2398 case AF_INET6:
2399 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2400 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2401 break;
2402 case AF_UNIX:
2403 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2404 break;
2405 case -1:
2406 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2407 chunk_strcat(out, strerror(errno));
2408 break;
2409 default: /* address family not supported */
2410 break;
2411 }
2412 }
2413 break;
2414 case ST_F_COOKIE:
2415 if (flags & STAT_SHLGNDS && sv->cookie)
2416 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2417 break;
2418 default:
2419 /* not used for servers. If a specific metric
2420 * is requested, return an error. Otherwise continue.
2421 */
2422 if (selected_field != NULL)
2423 return 0;
2424 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002425 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002426 stats[current_field] = metric;
2427 if (selected_field != NULL)
2428 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002429 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002430 return 1;
2431}
2432
2433/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02002434 * from stream interface <si>, and server state <state>. The caller is
2435 * responsible for clearing the trash if needed. Returns non-zero if it emits
2436 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002437 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002438static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002439{
2440 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002441 struct stats_module *mod;
2442 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2443 size_t stats_count = ST_F_TOTAL_FIELDS;
2444
2445 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002446
William Dauchyd3a9a492021-01-25 17:29:03 +01002447 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats,
2448 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002449 return 0;
2450
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002451 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2452 void *counters;
2453
2454 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2455 continue;
2456
2457 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2458 stats_count += mod->stats_count;
2459 continue;
2460 }
2461
2462 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2463 mod->fill_stats(counters, stats + stats_count);
2464 stats_count += mod->stats_count;
2465 }
2466
2467 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002468}
2469
William Dauchyda3b4662021-01-25 17:29:01 +01002470/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002471 */
William Dauchyda3b4662021-01-25 17:29:01 +01002472static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002473{
William Dauchyda3b4662021-01-25 17:29:01 +01002474 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002475 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002476
William Dauchyda3b4662021-01-25 17:29:01 +01002477 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002478 for (srv = px->srv; srv; srv = srv->next) {
2479 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002480 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002481 if (srv_currently_usable(srv) &&
2482 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002483 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002484 }
William Dauchyda3b4662021-01-25 17:29:01 +01002485 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002486 }
2487
Willy Tarreaubd715102020-10-23 22:44:30 +02002488 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2489 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002490 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002491 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2492
William Dauchyda3b4662021-01-25 17:29:01 +01002493 /* use tmp variable then assign result to make gcc happy */
2494 *nbup = nbup_tmp;
2495 *nbsrv = nbsrv_tmp;
2496 *totuw = totuw_tmp;
2497}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002498
William Dauchyda3b4662021-01-25 17:29:01 +01002499/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2500 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2501 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2502 * this value, or if the selected field is not implemented for backends, the
2503 * function returns 0, otherwise, it returns 1. <flags> can take the value
2504 * STAT_SHLGNDS.
2505 */
2506int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2507 enum stat_field *selected_field)
2508{
2509 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2510 long long be_samples_counter;
2511 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2512 struct buffer *out = get_trash_chunk();
2513 int nbup, nbsrv, totuw;
2514 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002515
William Dauchyda3b4662021-01-25 17:29:01 +01002516 if (len < ST_F_TOTAL_FIELDS)
2517 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002518
William Dauchyda3b4662021-01-25 17:29:01 +01002519 nbup = nbsrv = totuw = 0;
2520 /* some srv values compute for later if we either select all fields or
2521 * need them for one of the mentioned ones */
2522 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2523 *selected_field == ST_F_UWEIGHT)
2524 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002525
William Dauchyda3b4662021-01-25 17:29:01 +01002526 /* same here but specific to time fields */
2527 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2528 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2529 *selected_field == ST_F_TTIME) {
2530 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2531 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2532 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002533 }
2534
William Dauchyda3b4662021-01-25 17:29:01 +01002535 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2536 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002537
William Dauchyda3b4662021-01-25 17:29:01 +01002538 switch (current_field) {
2539 case ST_F_PXNAME:
2540 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2541 break;
2542 case ST_F_SVNAME:
2543 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2544 break;
2545 case ST_F_MODE:
2546 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2547 break;
2548 case ST_F_QCUR:
2549 metric = mkf_u32(0, px->nbpend);
2550 break;
2551 case ST_F_QMAX:
2552 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2553 break;
2554 case ST_F_SCUR:
2555 metric = mkf_u32(0, px->beconn);
2556 break;
2557 case ST_F_SMAX:
2558 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2559 break;
2560 case ST_F_SLIM:
2561 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2562 break;
2563 case ST_F_STOT:
2564 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2565 break;
2566 case ST_F_BIN:
2567 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2568 break;
2569 case ST_F_BOUT:
2570 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2571 break;
2572 case ST_F_DREQ:
2573 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2574 break;
2575 case ST_F_DRESP:
2576 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2577 break;
2578 case ST_F_ECON:
2579 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2580 break;
2581 case ST_F_ERESP:
2582 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2583 break;
2584 case ST_F_WRETR:
2585 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2586 break;
2587 case ST_F_WREDIS:
2588 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2589 break;
2590 case ST_F_WREW:
2591 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2592 break;
2593 case ST_F_EINT:
2594 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2595 break;
2596 case ST_F_CONNECT:
2597 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2598 break;
2599 case ST_F_REUSE:
2600 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2601 break;
2602 case ST_F_STATUS:
2603 fld = chunk_newstr(out);
2604 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2605 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2606 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2607 metric = mkf_str(FO_STATUS, fld);
2608 break;
2609 case ST_F_WEIGHT:
2610 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2611 break;
2612 case ST_F_UWEIGHT:
2613 metric = mkf_u32(FN_AVG, totuw);
2614 break;
2615 case ST_F_ACT:
2616 metric = mkf_u32(0, px->srv_act);
2617 break;
2618 case ST_F_BCK:
2619 metric = mkf_u32(0, px->srv_bck);
2620 break;
2621 case ST_F_CHKDOWN:
2622 metric = mkf_u64(FN_COUNTER, px->down_trans);
2623 break;
2624 case ST_F_LASTCHG:
2625 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2626 break;
2627 case ST_F_DOWNTIME:
2628 if (px->srv)
2629 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2630 break;
2631 case ST_F_PID:
2632 metric = mkf_u32(FO_KEY, relative_pid);
2633 break;
2634 case ST_F_IID:
2635 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2636 break;
2637 case ST_F_SID:
2638 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2639 break;
2640 case ST_F_LBTOT:
2641 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2642 break;
2643 case ST_F_TYPE:
2644 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2645 break;
2646 case ST_F_RATE:
2647 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2648 break;
2649 case ST_F_RATE_MAX:
2650 metric = mkf_u32(0, px->be_counters.sps_max);
2651 break;
2652 case ST_F_COOKIE:
2653 if (flags & STAT_SHLGNDS && px->cookie_name)
2654 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2655 break;
2656 case ST_F_ALGO:
2657 if (flags & STAT_SHLGNDS)
2658 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2659 break;
2660 case ST_F_REQ_TOT:
2661 if (px->mode == PR_MODE_HTTP)
2662 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2663 break;
2664 case ST_F_HRSP_1XX:
2665 if (px->mode == PR_MODE_HTTP)
2666 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2667 break;
2668 case ST_F_HRSP_2XX:
2669 if (px->mode == PR_MODE_HTTP)
2670 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2671 break;
2672 case ST_F_HRSP_3XX:
2673 if (px->mode == PR_MODE_HTTP)
2674 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2675 break;
2676 case ST_F_HRSP_4XX:
2677 if (px->mode == PR_MODE_HTTP)
2678 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2679 break;
2680 case ST_F_HRSP_5XX:
2681 if (px->mode == PR_MODE_HTTP)
2682 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2683 break;
2684 case ST_F_HRSP_OTHER:
2685 if (px->mode == PR_MODE_HTTP)
2686 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2687 break;
2688 case ST_F_CACHE_LOOKUPS:
2689 if (px->mode == PR_MODE_HTTP)
2690 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2691 break;
2692 case ST_F_CACHE_HITS:
2693 if (px->mode == PR_MODE_HTTP)
2694 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2695 break;
2696 case ST_F_CLI_ABRT:
2697 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2698 break;
2699 case ST_F_SRV_ABRT:
2700 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2701 break;
2702 case ST_F_COMP_IN:
2703 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2704 break;
2705 case ST_F_COMP_OUT:
2706 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2707 break;
2708 case ST_F_COMP_BYP:
2709 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2710 break;
2711 case ST_F_COMP_RSP:
2712 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2713 break;
2714 case ST_F_LASTSESS:
2715 metric = mkf_s32(FN_AGE, be_lastsession(px));
2716 break;
2717 case ST_F_QTIME:
2718 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2719 break;
2720 case ST_F_CTIME:
2721 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2722 break;
2723 case ST_F_RTIME:
2724 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2725 break;
2726 case ST_F_TTIME:
2727 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2728 break;
2729 case ST_F_QT_MAX:
2730 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2731 break;
2732 case ST_F_CT_MAX:
2733 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2734 break;
2735 case ST_F_RT_MAX:
2736 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2737 break;
2738 case ST_F_TT_MAX:
2739 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2740 break;
2741 default:
2742 /* not used for backends. If a specific metric
2743 * is requested, return an error. Otherwise continue.
2744 */
2745 if (selected_field != NULL)
2746 return 0;
2747 continue;
2748 }
2749 stats[current_field] = metric;
2750 if (selected_field != NULL)
2751 break;
2752 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002753 return 1;
2754}
2755
2756/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002757 * interface <si>. The caller is responsible for clearing the trash if needed.
2758 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002759 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002760static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002761{
2762 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002763 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2764 struct stats_module *mod;
2765 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002766
2767 if (!(px->cap & PR_CAP_BE))
2768 return 0;
2769
2770 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2771 return 0;
2772
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002773 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2774
William Dauchyda3b4662021-01-25 17:29:01 +01002775 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002776 return 0;
2777
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002778 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2779 struct extra_counters *counters;
2780
2781 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2782 continue;
2783
2784 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2785 stats_count += mod->stats_count;
2786 continue;
2787 }
2788
2789 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2790 mod->fill_stats(counters, stats + stats_count);
2791 stats_count += mod->stats_count;
2792 }
2793
2794 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002795}
2796
2797/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2798 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2799 * for clearing the trash if needed.
2800 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002801static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002802{
2803 struct appctx *appctx = __objt_appctx(si->end);
2804 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002805 struct stats_module *mod;
2806 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002807
2808 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2809 /* A form to enable/disable this proxy servers */
2810
2811 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2812 scope_txt[0] = 0;
2813 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002814 const char *scope_ptr = stats_scope_ptr(appctx, si);
2815
William Lallemand74c24fb2016-11-21 17:18:36 +01002816 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002817 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002818 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2819 }
2820
2821 chunk_appendf(&trash,
2822 "<form method=\"post\">");
2823 }
2824
2825 /* print a new table */
2826 chunk_appendf(&trash,
2827 "<table class=\"tbl\" width=\"100%%\">\n"
2828 "<tr class=\"titre\">"
2829 "<th class=\"pxname\" width=\"10%%\">");
2830
2831 chunk_appendf(&trash,
2832 "<a name=\"%s\"></a>%s"
2833 "<a class=px href=\"#%s\">%s</a>",
2834 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002835 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002836 px->id, px->id);
2837
Willy Tarreau676c29e2019-10-09 10:50:01 +02002838 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002839 /* cap, mode, id */
2840 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2841 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2842 px->uuid);
2843 chunk_appendf(&trash, "</div>");
2844 }
2845
2846 chunk_appendf(&trash,
2847 "%s</th>"
2848 "<th class=\"%s\" width=\"90%%\">%s</th>"
2849 "</tr>\n"
2850 "</table>\n"
2851 "<table class=\"tbl\" width=\"100%%\">\n"
2852 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002853 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002854 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2855
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002856 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002857 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002858 if ((px->cap & PR_CAP_BE) && px->srv)
2859 chunk_appendf(&trash,
2860 "<th rowspan=2 width=1><input type=\"checkbox\" "
2861 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2862 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2863 px->id,
2864 px->id);
2865 else
2866 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002867 }
2868
2869 chunk_appendf(&trash,
2870 "<th rowspan=2></th>"
2871 "<th colspan=3>Queue</th>"
2872 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2873 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2874 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002875 "<th colspan=9>Server</th>");
2876
2877 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2878 // calculate the count of module for colspan attribute
2879 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2880 ++stats_module_len;
2881 }
2882 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2883 stats_module_len);
2884 }
2885
2886 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002887 "</tr>\n"
2888 "<tr class=\"titre\">"
2889 "<th>Cur</th><th>Max</th><th>Limit</th>"
2890 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2891 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2892 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2893 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2894 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2895 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002896 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002897
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002898 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2899 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2900 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2901 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002902 }
2903
2904 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002905}
2906
2907/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2908 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2909 */
2910static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2911{
2912 struct appctx *appctx = __objt_appctx(si->end);
2913 chunk_appendf(&trash, "</table>");
2914
2915 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2916 /* close the form used to enable/disable this proxy servers */
2917 chunk_appendf(&trash,
2918 "Choose the action to perform on the checked servers : "
2919 "<select name=action>"
2920 "<option value=\"\"></option>"
2921 "<option value=\"ready\">Set state to READY</option>"
2922 "<option value=\"drain\">Set state to DRAIN</option>"
2923 "<option value=\"maint\">Set state to MAINT</option>"
2924 "<option value=\"dhlth\">Health: disable checks</option>"
2925 "<option value=\"ehlth\">Health: enable checks</option>"
2926 "<option value=\"hrunn\">Health: force UP</option>"
2927 "<option value=\"hnolb\">Health: force NOLB</option>"
2928 "<option value=\"hdown\">Health: force DOWN</option>"
2929 "<option value=\"dagent\">Agent: disable checks</option>"
2930 "<option value=\"eagent\">Agent: enable checks</option>"
2931 "<option value=\"arunn\">Agent: force UP</option>"
2932 "<option value=\"adown\">Agent: force DOWN</option>"
2933 "<option value=\"shutdown\">Kill Sessions</option>"
2934 "</select>"
2935 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2936 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2937 "</form>",
2938 px->uuid);
2939 }
2940
2941 chunk_appendf(&trash, "<p>\n");
2942}
2943
2944/*
2945 * Dumps statistics for a proxy. The output is sent to the stream interface's
2946 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2947 * buffer space, or non-zero if everything completed. This function is used
2948 * both by the CLI and the HTTP entry points, and is able to dump the output
2949 * in HTML or CSV formats. If the later, <uri> must be NULL.
2950 */
Christopher Fauletef779222018-10-31 08:47:01 +01002951int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2952 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002953{
2954 struct appctx *appctx = __objt_appctx(si->end);
2955 struct stream *s = si_strm(si);
2956 struct channel *rep = si_ic(si);
2957 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2958 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002959
2960 chunk_reset(&trash);
2961
2962 switch (appctx->ctx.stats.px_st) {
2963 case STAT_PX_ST_INIT:
2964 /* we are on a new proxy */
2965 if (uri && uri->scope) {
2966 /* we have a limited scope, we have to check the proxy name */
2967 struct stat_scope *scope;
2968 int len;
2969
2970 len = strlen(px->id);
2971 scope = uri->scope;
2972
2973 while (scope) {
2974 /* match exact proxy name */
2975 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2976 break;
2977
2978 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002979 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01002980 break;
2981 scope = scope->next;
2982 }
2983
2984 /* proxy name not found : don't dump anything */
2985 if (scope == NULL)
2986 return 1;
2987 }
2988
2989 /* if the user has requested a limited output and the proxy
2990 * name does not match, skip it.
2991 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002992 if (appctx->ctx.stats.scope_len) {
2993 const char *scope_ptr = stats_scope_ptr(appctx, si);
2994
2995 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2996 return 1;
2997 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002998
2999 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3000 (appctx->ctx.stats.iid != -1) &&
3001 (px->uuid != appctx->ctx.stats.iid))
3002 return 1;
3003
3004 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
3005 /* fall through */
3006
3007 case STAT_PX_ST_TH:
3008 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02003009 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003010 if (!stats_putchk(rep, htx, &trash))
3011 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003012 }
3013
3014 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
3015 /* fall through */
3016
3017 case STAT_PX_ST_FE:
3018 /* print the frontend */
3019 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003020 if (!stats_putchk(rep, htx, &trash))
3021 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003022 }
3023
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003024 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01003025 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
3026 /* fall through */
3027
3028 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003029 /* obj2 points to listeners list as initialized above */
3030 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003031 if (htx) {
3032 if (htx_almost_full(htx))
3033 goto full;
3034 }
3035 else {
3036 if (buffer_almost_full(&rep->buf))
3037 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003038 }
3039
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003040 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003041 if (!l->counters)
3042 continue;
3043
3044 if (appctx->ctx.stats.flags & STAT_BOUND) {
3045 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
3046 break;
3047
3048 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
3049 continue;
3050 }
3051
3052 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02003053 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003054 if (!stats_putchk(rep, htx, &trash))
3055 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003056 }
3057 }
3058
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003059 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01003060 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
3061 /* fall through */
3062
3063 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003064 /* obj2 points to servers list as initialized above */
3065 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01003066 if (htx) {
3067 if (htx_almost_full(htx))
3068 goto full;
3069 }
3070 else {
3071 if (buffer_almost_full(&rep->buf))
3072 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003073 }
3074
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003075 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01003076
3077 if (appctx->ctx.stats.flags & STAT_BOUND) {
3078 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
3079 break;
3080
3081 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
3082 continue;
3083 }
3084
Willy Tarreau3e320362020-10-23 17:28:57 +02003085 /* do not report disabled servers */
3086 if (appctx->ctx.stats.flags & STAT_HIDE_MAINT &&
3087 sv->cur_admin & SRV_ADMF_MAINT) {
3088 continue;
3089 }
3090
William Lallemand74c24fb2016-11-21 17:18:36 +01003091 svs = sv;
3092 while (svs->track)
3093 svs = svs->track;
3094
3095 /* do not report servers which are DOWN and not changing state */
3096 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003097 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3098 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003099 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3100 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3101 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3102 continue;
3103 }
3104
Willy Tarreau43241ff2019-10-09 11:27:51 +02003105 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003106 if (!stats_putchk(rep, htx, &trash))
3107 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003108 }
3109 } /* for sv */
3110
3111 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
3112 /* fall through */
3113
3114 case STAT_PX_ST_BE:
3115 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02003116 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003117 if (!stats_putchk(rep, htx, &trash))
3118 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003119 }
3120
3121 appctx->ctx.stats.px_st = STAT_PX_ST_END;
3122 /* fall through */
3123
3124 case STAT_PX_ST_END:
3125 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3126 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003127 if (!stats_putchk(rep, htx, &trash))
3128 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003129 }
3130
3131 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
3132 /* fall through */
3133
3134 case STAT_PX_ST_FIN:
3135 return 1;
3136
3137 default:
3138 /* unknown state, we should put an abort() here ! */
3139 return 1;
3140 }
Christopher Fauletef779222018-10-31 08:47:01 +01003141
3142 full:
3143 si_rx_room_blk(si);
3144 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003145}
3146
3147/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3148 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3149 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003150static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003151{
3152 /* WARNING! This must fit in the first buffer !!! */
3153 chunk_appendf(&trash,
3154 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3155 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3156 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03003157 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003158 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3159 "<style type=\"text/css\"><!--\n"
3160 "body {"
3161 " font-family: arial, helvetica, sans-serif;"
3162 " font-size: 12px;"
3163 " font-weight: normal;"
3164 " color: black;"
3165 " background: white;"
3166 "}\n"
3167 "th,td {"
3168 " font-size: 10px;"
3169 "}\n"
3170 "h1 {"
3171 " font-size: x-large;"
3172 " margin-bottom: 0.5em;"
3173 "}\n"
3174 "h2 {"
3175 " font-family: helvetica, arial;"
3176 " font-size: x-large;"
3177 " font-weight: bold;"
3178 " font-style: italic;"
3179 " color: #6020a0;"
3180 " margin-top: 0em;"
3181 " margin-bottom: 0em;"
3182 "}\n"
3183 "h3 {"
3184 " font-family: helvetica, arial;"
3185 " font-size: 16px;"
3186 " font-weight: bold;"
3187 " color: #b00040;"
3188 " background: #e8e8d0;"
3189 " margin-top: 0em;"
3190 " margin-bottom: 0em;"
3191 "}\n"
3192 "li {"
3193 " margin-top: 0.25em;"
3194 " margin-right: 2em;"
3195 "}\n"
3196 ".hr {margin-top: 0.25em;"
3197 " border-color: black;"
3198 " border-bottom-style: solid;"
3199 "}\n"
3200 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3201 ".total {background: #20D0D0;color: #ffff80;}\n"
3202 ".frontend {background: #e8e8d0;}\n"
3203 ".socket {background: #d0d0d0;}\n"
3204 ".backend {background: #e8e8d0;}\n"
3205 ".active_down {background: #ff9090;}\n"
3206 ".active_going_up {background: #ffd020;}\n"
3207 ".active_going_down {background: #ffffa0;}\n"
3208 ".active_up {background: #c0ffc0;}\n"
3209 ".active_nolb {background: #20a0ff;}\n"
3210 ".active_draining {background: #20a0FF;}\n"
3211 ".active_no_check {background: #e0e0e0;}\n"
3212 ".backup_down {background: #ff9090;}\n"
3213 ".backup_going_up {background: #ff80ff;}\n"
3214 ".backup_going_down {background: #c060ff;}\n"
3215 ".backup_up {background: #b0d0ff;}\n"
3216 ".backup_nolb {background: #90b0e0;}\n"
3217 ".backup_draining {background: #cc9900;}\n"
3218 ".backup_no_check {background: #e0e0e0;}\n"
3219 ".maintain {background: #c07820;}\n"
3220 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3221 "\n"
3222 "a.px:link {color: #ffff40; text-decoration: none;}"
3223 "a.px:visited {color: #ffff40; text-decoration: none;}"
3224 "a.px:hover {color: #ffffff; text-decoration: none;}"
3225 "a.lfsb:link {color: #000000; text-decoration: none;}"
3226 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3227 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3228 "\n"
3229 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3230 "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"
3231 "table.tbl td.ac { text-align: center;}\n"
3232 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3233 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3234 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3235 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3236 "\n"
3237 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3238 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3239 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3240 "table.det { border-collapse: collapse; border-style: none; }\n"
3241 "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"
3242 "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"
3243 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
3244 "div.tips {\n"
3245 " display:block;\n"
3246 " visibility:hidden;\n"
3247 " z-index:2147483647;\n"
3248 " position:absolute;\n"
3249 " padding:2px 4px 3px;\n"
3250 " background:#f0f060; color:#000000;\n"
3251 " border:1px solid #7040c0;\n"
3252 " white-space:nowrap;\n"
3253 " font-style:normal;font-size:11px;font-weight:normal;\n"
3254 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3255 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3256 "}\n"
3257 "u:hover div.tips {visibility:visible;}\n"
3258 "-->\n"
3259 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003260 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01003261 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003262 );
3263}
3264
3265/* Dumps the HTML stats information block to the trash for and uses the state from
3266 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
3267 * for clearing the trash if needed.
3268 */
3269static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
3270{
3271 struct appctx *appctx = __objt_appctx(si->end);
3272 unsigned int up = (now.tv_sec - start_date.tv_sec);
3273 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01003274 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02003275 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3276
3277 /* Turn the bytes per second to bits per second and take care of the
3278 * usual ethernet overhead in order to help figure how far we are from
3279 * interface saturation since it's the only case which usually matters.
3280 * For this we count the total size of an Ethernet frame on the wire
3281 * including preamble and IFG (1538) for the largest TCP segment it
3282 * transports (1448 with TCP timestamps). This is not valid for smaller
3283 * packets (under-estimated), but it gives a reasonably accurate
3284 * estimation of how far we are from uplink saturation.
3285 */
3286 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003287
3288 /* WARNING! this has to fit the first packet too.
3289 * We are around 3.5 kB, add adding entries will
3290 * become tricky if we want to support 4kB buffers !
3291 */
3292 chunk_appendf(&trash,
3293 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3294 PRODUCT_NAME "%s</a></h1>\n"
3295 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3296 "<hr width=\"100%%\" class=\"hr\">\n"
3297 "<h3>&gt; General process information</h3>\n"
3298 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003299 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003300 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3301 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3302 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003303 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003304 "Running tasks: %d/%d; idle = %d %%<br>\n"
3305 "</td><td align=\"center\" nowrap>\n"
3306 "<table class=\"lgd\"><tr>\n"
3307 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3308 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3309 "</tr><tr>\n"
3310 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3311 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3312 "</tr><tr>\n"
3313 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3314 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3315 "</tr><tr>\n"
3316 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3317 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3318 "</tr><tr>\n"
3319 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3320 "</tr><tr>\n"
3321 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3322 "</tr></table>\n"
3323 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3324 "</td>"
3325 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3326 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3327 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003328 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
3329 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
3330 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3331 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
3332 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01003333 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003334 up / 86400, (up % 86400) / 3600,
3335 (up % 3600) / 60, (up % 60),
3336 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3337 global.rlimit_memmax ? " MB" : "",
3338 global.rlimit_nofile,
3339 global.maxsock, global.maxconn, global.maxpipes,
3340 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003341 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3342 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau955a11e2021-02-24 17:03:30 +01003343 total_run_queues(), total_allocated_tasks(), ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01003344 );
3345
3346 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003347 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003348 scope_txt[appctx->ctx.stats.scope_len] = '\0';
3349
3350 chunk_appendf(&trash,
3351 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
3352 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
3353 STAT_SCOPE_TXT_MAXLEN);
3354
3355 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3356 scope_txt[0] = 0;
3357 if (appctx->ctx.stats.scope_len) {
3358 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003359 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003360 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3361 }
3362
3363 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
3364 chunk_appendf(&trash,
3365 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3366 uri->uri_prefix,
3367 "",
3368 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3369 scope_txt);
3370 else
3371 chunk_appendf(&trash,
3372 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3373 uri->uri_prefix,
3374 ";up",
3375 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3376 scope_txt);
3377
3378 if (uri->refresh > 0) {
3379 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
3380 chunk_appendf(&trash,
3381 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3382 uri->uri_prefix,
3383 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3384 "",
3385 scope_txt);
3386 else
3387 chunk_appendf(&trash,
3388 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3389 uri->uri_prefix,
3390 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3391 ";norefresh",
3392 scope_txt);
3393 }
3394
3395 chunk_appendf(&trash,
3396 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3397 uri->uri_prefix,
3398 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3399 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3400 scope_txt);
3401
3402 chunk_appendf(&trash,
3403 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3404 uri->uri_prefix,
3405 (uri->refresh > 0) ? ";norefresh" : "",
3406 scope_txt);
3407
3408 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003409 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3410 uri->uri_prefix,
3411 (uri->refresh > 0) ? ";norefresh" : "",
3412 scope_txt, uri->uri_prefix);
3413
3414 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003415 "</ul></td>"
3416 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3417 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3418 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3419 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3420 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3421 "</ul>"
3422 "</td>"
3423 "</tr></table>\n"
3424 ""
3425 );
3426
3427 if (appctx->ctx.stats.st_code) {
3428 switch (appctx->ctx.stats.st_code) {
3429 case STAT_STATUS_DONE:
3430 chunk_appendf(&trash,
3431 "<p><div class=active_up>"
3432 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3433 "Action processed successfully."
3434 "</div>\n", uri->uri_prefix,
3435 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3436 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3437 scope_txt);
3438 break;
3439 case STAT_STATUS_NONE:
3440 chunk_appendf(&trash,
3441 "<p><div class=active_going_down>"
3442 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3443 "Nothing has changed."
3444 "</div>\n", uri->uri_prefix,
3445 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3446 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3447 scope_txt);
3448 break;
3449 case STAT_STATUS_PART:
3450 chunk_appendf(&trash,
3451 "<p><div class=active_going_down>"
3452 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3453 "Action partially processed.<br>"
3454 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3455 "</div>\n", uri->uri_prefix,
3456 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3457 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3458 scope_txt);
3459 break;
3460 case STAT_STATUS_ERRP:
3461 chunk_appendf(&trash,
3462 "<p><div class=active_down>"
3463 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3464 "Action not processed because of invalid parameters."
3465 "<ul>"
3466 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003467 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003468 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3469 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3470 "</ul>"
3471 "</div>\n", uri->uri_prefix,
3472 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3473 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3474 scope_txt);
3475 break;
3476 case STAT_STATUS_EXCD:
3477 chunk_appendf(&trash,
3478 "<p><div class=active_down>"
3479 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3480 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3481 "You should retry with less servers at a time.</b>"
3482 "</div>\n", uri->uri_prefix,
3483 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3484 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3485 scope_txt);
3486 break;
3487 case STAT_STATUS_DENY:
3488 chunk_appendf(&trash,
3489 "<p><div class=active_down>"
3490 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3491 "<b>Action denied.</b>"
3492 "</div>\n", uri->uri_prefix,
3493 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3494 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3495 scope_txt);
3496 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003497 case STAT_STATUS_IVAL:
3498 chunk_appendf(&trash,
3499 "<p><div class=active_down>"
3500 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3501 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3502 "</div>\n", uri->uri_prefix,
3503 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3504 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3505 scope_txt);
3506 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003507 default:
3508 chunk_appendf(&trash,
3509 "<p><div class=active_no_check>"
3510 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3511 "Unexpected result."
3512 "</div>\n", uri->uri_prefix,
3513 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3514 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3515 scope_txt);
3516 }
3517 chunk_appendf(&trash, "<p>\n");
3518 }
3519}
3520
3521/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3522 * for clearing the trash if needed.
3523 */
3524static void stats_dump_html_end()
3525{
3526 chunk_appendf(&trash, "</body></html>\n");
3527}
3528
Simon Horman05ee2132017-01-04 09:37:25 +01003529/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3530 * for clearing it if needed.
3531 */
3532static void stats_dump_json_header()
3533{
3534 chunk_strcat(&trash, "[");
3535}
3536
3537
3538/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3539 * for clearing the trash if needed.
3540 */
3541static void stats_dump_json_end()
3542{
3543 chunk_strcat(&trash, "]");
3544}
3545
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003546/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3547 * a pointer to the current server/listener.
3548 */
3549static int stats_dump_proxies(struct stream_interface *si,
3550 struct htx *htx,
3551 struct uri_auth *uri)
3552{
3553 struct appctx *appctx = __objt_appctx(si->end);
3554 struct channel *rep = si_ic(si);
3555 struct proxy *px;
3556
3557 /* dump proxies */
3558 while (appctx->ctx.stats.obj1) {
3559 if (htx) {
3560 if (htx_almost_full(htx))
3561 goto full;
3562 }
3563 else {
3564 if (buffer_almost_full(&rep->buf))
3565 goto full;
3566 }
3567
3568 px = appctx->ctx.stats.obj1;
3569 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02003570 if (!px->disabled && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003571 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
3572 return 0;
3573 }
3574
3575 appctx->ctx.stats.obj1 = px->next;
3576 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3577 }
3578
3579 return 1;
3580
3581 full:
3582 si_rx_room_blk(si);
3583 return 0;
3584}
3585
William Lallemand74c24fb2016-11-21 17:18:36 +01003586/* This function dumps statistics onto the stream interface's read buffer in
3587 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3588 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3589 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3590 * and the stream must be closed, or -1 in case of any error. This function is
3591 * used by both the CLI and the HTTP handlers.
3592 */
Christopher Fauletef779222018-10-31 08:47:01 +01003593static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
3594 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003595{
3596 struct appctx *appctx = __objt_appctx(si->end);
3597 struct channel *rep = si_ic(si);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003598 enum stats_domain domain = appctx->ctx.stats.domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003599
3600 chunk_reset(&trash);
3601
3602 switch (appctx->st2) {
3603 case STAT_ST_INIT:
3604 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3605 /* fall through */
3606
3607 case STAT_ST_HEAD:
3608 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003609 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003610 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3611 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003612 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003613 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003614 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003615 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003616
Christopher Fauletef779222018-10-31 08:47:01 +01003617 if (!stats_putchk(rep, htx, &trash))
3618 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003619
Christopher Faulet6338a082019-09-09 15:50:54 +02003620 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3621 appctx->st2 = STAT_ST_FIN;
3622 return 1;
3623 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003624 appctx->st2 = STAT_ST_INFO;
3625 /* fall through */
3626
3627 case STAT_ST_INFO:
3628 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3629 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003630 if (!stats_putchk(rep, htx, &trash))
3631 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003632 }
3633
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003634 if (domain == STATS_DOMAIN_PROXY)
3635 appctx->ctx.stats.obj1 = proxies_list;
3636
William Lallemand74c24fb2016-11-21 17:18:36 +01003637 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3638 appctx->st2 = STAT_ST_LIST;
3639 /* fall through */
3640
3641 case STAT_ST_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003642 switch (domain) {
3643 case STATS_DOMAIN_DNS:
Emeric Brund3b44952021-01-06 16:01:02 +01003644 if (!stats_dump_resolvers(si, stat_l[domain],
3645 stat_count[domain],
3646 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003647 return 0;
3648 }
3649 break;
3650
3651 case STATS_DOMAIN_PROXY:
3652 default:
3653 /* dump proxies */
3654 if (!stats_dump_proxies(si, htx, uri))
3655 return 0;
3656 break;
3657 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003658
3659 appctx->st2 = STAT_ST_END;
3660 /* fall through */
3661
3662 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003663 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3664 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3665 stats_dump_html_end();
3666 else
3667 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003668 if (!stats_putchk(rep, htx, &trash))
3669 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003670 }
3671
3672 appctx->st2 = STAT_ST_FIN;
3673 /* fall through */
3674
3675 case STAT_ST_FIN:
3676 return 1;
3677
3678 default:
3679 /* unknown state ! */
3680 appctx->st2 = STAT_ST_FIN;
3681 return -1;
3682 }
Christopher Fauletef779222018-10-31 08:47:01 +01003683
3684 full:
3685 si_rx_room_blk(si);
3686 return 0;
3687
William Lallemand74c24fb2016-11-21 17:18:36 +01003688}
3689
3690/* We reached the stats page through a POST request. The appctx is
3691 * expected to have already been allocated by the caller.
3692 * Parse the posted data and enable/disable servers if necessary.
3693 * Returns 1 if request was parsed or zero if it needs more data.
3694 */
3695static int stats_process_http_post(struct stream_interface *si)
3696{
3697 struct stream *s = si_strm(si);
3698 struct appctx *appctx = objt_appctx(si->end);
3699
3700 struct proxy *px = NULL;
3701 struct server *sv = NULL;
3702
3703 char key[LINESIZE];
3704 int action = ST_ADM_ACTION_NONE;
3705 int reprocess = 0;
3706
3707 int total_servers = 0;
3708 int altered_servers = 0;
3709
3710 char *first_param, *cur_param, *next_param, *end_params;
3711 char *st_cur_param = NULL;
3712 char *st_next_param = NULL;
3713
Christopher Fauleta3618372018-12-13 21:59:56 +01003714 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003715
Christopher Fauletb7f88902019-07-15 21:56:43 +02003716 struct htx *htx = htxbuf(&s->req.buf);
3717 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003718
Christopher Fauletb7f88902019-07-15 21:56:43 +02003719 /* we need more data */
3720 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3721 /* check if we can receive more */
3722 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3723 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3724 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003725 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003726 goto wait;
3727 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003728
Christopher Fauletb7f88902019-07-15 21:56:43 +02003729 /* The request was fully received. Copy data */
3730 blk = htx_get_head_blk(htx);
3731 while (blk) {
3732 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003733
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003734 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003735 break;
3736 if (type == HTX_BLK_DATA) {
3737 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003738
Christopher Fauletb7f88902019-07-15 21:56:43 +02003739 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003740 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3741 goto out;
3742 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003743 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003744 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003745 }
3746
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003747 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003748 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003749 cur_param = next_param = end_params;
3750 *end_params = '\0';
3751
3752 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3753
3754 /*
3755 * Parse the parameters in reverse order to only store the last value.
3756 * From the html form, the backend and the action are at the end.
3757 */
3758 while (cur_param > first_param) {
3759 char *value;
3760 int poffset, plen;
3761
3762 cur_param--;
3763
3764 if ((*cur_param == '&') || (cur_param == first_param)) {
3765 reprocess_servers:
3766 /* Parse the key */
3767 poffset = (cur_param != first_param ? 1 : 0);
3768 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3769 if ((plen > 0) && (plen <= sizeof(key))) {
3770 strncpy(key, cur_param + poffset, plen);
3771 key[plen - 1] = '\0';
3772 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003773 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003774 goto out;
3775 }
3776
3777 /* Parse the value */
3778 value = key;
3779 while (*value != '\0' && *value != '=') {
3780 value++;
3781 }
3782 if (*value == '=') {
3783 /* Ok, a value is found, we can mark the end of the key */
3784 *value++ = '\0';
3785 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003786 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003787 break;
3788
3789 /* Now we can check the key to see what to do */
3790 if (!px && (strcmp(key, "b") == 0)) {
3791 if ((px = proxy_be_by_name(value)) == NULL) {
3792 /* the backend name is unknown or ambiguous (duplicate names) */
3793 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3794 goto out;
3795 }
3796 }
3797 else if (!action && (strcmp(key, "action") == 0)) {
3798 if (strcmp(value, "ready") == 0) {
3799 action = ST_ADM_ACTION_READY;
3800 }
3801 else if (strcmp(value, "drain") == 0) {
3802 action = ST_ADM_ACTION_DRAIN;
3803 }
3804 else if (strcmp(value, "maint") == 0) {
3805 action = ST_ADM_ACTION_MAINT;
3806 }
3807 else if (strcmp(value, "shutdown") == 0) {
3808 action = ST_ADM_ACTION_SHUTDOWN;
3809 }
3810 else if (strcmp(value, "dhlth") == 0) {
3811 action = ST_ADM_ACTION_DHLTH;
3812 }
3813 else if (strcmp(value, "ehlth") == 0) {
3814 action = ST_ADM_ACTION_EHLTH;
3815 }
3816 else if (strcmp(value, "hrunn") == 0) {
3817 action = ST_ADM_ACTION_HRUNN;
3818 }
3819 else if (strcmp(value, "hnolb") == 0) {
3820 action = ST_ADM_ACTION_HNOLB;
3821 }
3822 else if (strcmp(value, "hdown") == 0) {
3823 action = ST_ADM_ACTION_HDOWN;
3824 }
3825 else if (strcmp(value, "dagent") == 0) {
3826 action = ST_ADM_ACTION_DAGENT;
3827 }
3828 else if (strcmp(value, "eagent") == 0) {
3829 action = ST_ADM_ACTION_EAGENT;
3830 }
3831 else if (strcmp(value, "arunn") == 0) {
3832 action = ST_ADM_ACTION_ARUNN;
3833 }
3834 else if (strcmp(value, "adown") == 0) {
3835 action = ST_ADM_ACTION_ADOWN;
3836 }
3837 /* else these are the old supported methods */
3838 else if (strcmp(value, "disable") == 0) {
3839 action = ST_ADM_ACTION_DISABLE;
3840 }
3841 else if (strcmp(value, "enable") == 0) {
3842 action = ST_ADM_ACTION_ENABLE;
3843 }
3844 else if (strcmp(value, "stop") == 0) {
3845 action = ST_ADM_ACTION_STOP;
3846 }
3847 else if (strcmp(value, "start") == 0) {
3848 action = ST_ADM_ACTION_START;
3849 }
3850 else {
3851 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3852 goto out;
3853 }
3854 }
3855 else if (strcmp(key, "s") == 0) {
3856 if (!(px && action)) {
3857 /*
3858 * Indicates that we'll need to reprocess the parameters
3859 * as soon as backend and action are known
3860 */
3861 if (!reprocess) {
3862 st_cur_param = cur_param;
3863 st_next_param = next_param;
3864 }
3865 reprocess = 1;
3866 }
3867 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003868 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003869 switch (action) {
3870 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003871 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003872 altered_servers++;
3873 total_servers++;
3874 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3875 }
3876 break;
3877 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003878 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003879 altered_servers++;
3880 total_servers++;
3881 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3882 }
3883 break;
3884 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003885 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003886 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3887 altered_servers++;
3888 total_servers++;
3889 }
3890 break;
3891 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003892 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003893 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3894 altered_servers++;
3895 total_servers++;
3896 }
3897 break;
3898 case ST_ADM_ACTION_DHLTH:
3899 if (sv->check.state & CHK_ST_CONFIGURED) {
3900 sv->check.state &= ~CHK_ST_ENABLED;
3901 altered_servers++;
3902 total_servers++;
3903 }
3904 break;
3905 case ST_ADM_ACTION_EHLTH:
3906 if (sv->check.state & CHK_ST_CONFIGURED) {
3907 sv->check.state |= CHK_ST_ENABLED;
3908 altered_servers++;
3909 total_servers++;
3910 }
3911 break;
3912 case ST_ADM_ACTION_HRUNN:
3913 if (!(sv->track)) {
3914 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003915 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003916 altered_servers++;
3917 total_servers++;
3918 }
3919 break;
3920 case ST_ADM_ACTION_HNOLB:
3921 if (!(sv->track)) {
3922 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003923 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003924 altered_servers++;
3925 total_servers++;
3926 }
3927 break;
3928 case ST_ADM_ACTION_HDOWN:
3929 if (!(sv->track)) {
3930 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003931 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003932 altered_servers++;
3933 total_servers++;
3934 }
3935 break;
3936 case ST_ADM_ACTION_DAGENT:
3937 if (sv->agent.state & CHK_ST_CONFIGURED) {
3938 sv->agent.state &= ~CHK_ST_ENABLED;
3939 altered_servers++;
3940 total_servers++;
3941 }
3942 break;
3943 case ST_ADM_ACTION_EAGENT:
3944 if (sv->agent.state & CHK_ST_CONFIGURED) {
3945 sv->agent.state |= CHK_ST_ENABLED;
3946 altered_servers++;
3947 total_servers++;
3948 }
3949 break;
3950 case ST_ADM_ACTION_ARUNN:
3951 if (sv->agent.state & CHK_ST_ENABLED) {
3952 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003953 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003954 altered_servers++;
3955 total_servers++;
3956 }
3957 break;
3958 case ST_ADM_ACTION_ADOWN:
3959 if (sv->agent.state & CHK_ST_ENABLED) {
3960 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003961 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003962 altered_servers++;
3963 total_servers++;
3964 }
3965 break;
3966 case ST_ADM_ACTION_READY:
3967 srv_adm_set_ready(sv);
3968 altered_servers++;
3969 total_servers++;
3970 break;
3971 case ST_ADM_ACTION_DRAIN:
3972 srv_adm_set_drain(sv);
3973 altered_servers++;
3974 total_servers++;
3975 break;
3976 case ST_ADM_ACTION_MAINT:
3977 srv_adm_set_maint(sv);
3978 altered_servers++;
3979 total_servers++;
3980 break;
3981 case ST_ADM_ACTION_SHUTDOWN:
Willy Tarreauc3914d42020-09-24 08:39:22 +02003982 if (!px->disabled) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003983 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003984 altered_servers++;
3985 total_servers++;
3986 }
3987 break;
3988 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003989 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003990 } else {
3991 /* the server name is unknown or ambiguous (duplicate names) */
3992 total_servers++;
3993 }
3994 }
3995 if (reprocess && px && action) {
3996 /* Now, we know the backend and the action chosen by the user.
3997 * We can safely restart from the first server parameter
3998 * to reprocess them
3999 */
4000 cur_param = st_cur_param;
4001 next_param = st_next_param;
4002 reprocess = 0;
4003 goto reprocess_servers;
4004 }
4005
4006 next_param = cur_param;
4007 }
4008 }
4009
4010 if (total_servers == 0) {
4011 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4012 }
4013 else if (altered_servers == 0) {
4014 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
4015 }
4016 else if (altered_servers == total_servers) {
4017 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
4018 }
4019 else {
4020 appctx->ctx.stats.st_code = STAT_STATUS_PART;
4021 }
4022 out:
4023 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004024 wait:
4025 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4026 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004027}
4028
4029
Christopher Fauletb7f88902019-07-15 21:56:43 +02004030static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004031{
4032 struct stream *s = si_strm(si);
4033 struct uri_auth *uri = s->be->uri_auth;
4034 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004035 struct htx_sl *sl;
4036 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004037
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004038 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);
4039 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4040 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004041 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004042 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004043
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004044 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004045 goto full;
4046 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
4047 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4048 goto full;
4049 }
Christopher Faulet6338a082019-09-09 15:50:54 +02004050 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
4051 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4052 goto full;
4053 }
Christopher Fauletef779222018-10-31 08:47:01 +01004054 else {
4055 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4056 goto full;
4057 }
4058
4059 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
4060 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004061 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004062 goto full;
4063 }
4064
4065 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4066 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4067 goto full;
4068 }
4069
4070 if (!htx_add_endof(htx, HTX_BLK_EOH))
4071 goto full;
4072
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004073 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004074 return 1;
4075
4076 full:
4077 htx_reset(htx);
4078 si_rx_room_blk(si);
4079 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004080}
4081
Christopher Fauletef779222018-10-31 08:47:01 +01004082
Christopher Fauletb7f88902019-07-15 21:56:43 +02004083static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004084{
4085 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4086 struct stream *s = si_strm(si);
4087 struct uri_auth *uri = s->be->uri_auth;
4088 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004089 struct htx_sl *sl;
4090 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004091
4092 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4093 scope_txt[0] = 0;
4094 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01004095 const char *scope_ptr = stats_scope_ptr(appctx, si);
4096
Christopher Fauletef779222018-10-31 08:47:01 +01004097 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004098 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01004099 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
4100 }
4101
4102 /* We don't want to land on the posted stats page because a refresh will
4103 * repost the data. We don't want this to happen on accident so we redirect
4104 * the browse to the stats page with a GET.
4105 */
4106 chunk_printf(&trash, "%s;st=%s%s%s%s",
4107 uri->uri_prefix,
4108 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4109 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4110 stat_status_codes[appctx->ctx.stats.st_code]) ?
4111 stat_status_codes[appctx->ctx.stats.st_code] :
4112 stat_status_codes[STAT_STATUS_UNKN],
4113 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4114 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4115 scope_txt);
4116
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004117 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4118 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4119 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004120 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004121 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004122
4123 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004124 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4125 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4126 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4127 goto full;
4128
4129 if (!htx_add_endof(htx, HTX_BLK_EOH))
4130 goto full;
4131
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004132 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004133 return 1;
4134
4135full:
4136 htx_reset(htx);
4137 si_rx_room_blk(si);
4138 return 0;
4139}
William Lallemand74c24fb2016-11-21 17:18:36 +01004140
Simon Horman05ee2132017-01-04 09:37:25 +01004141
William Lallemand74c24fb2016-11-21 17:18:36 +01004142/* This I/O handler runs as an applet embedded in a stream interface. It is
4143 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4144 * appctx->st0 contains the operation in progress (dump, done). The handler
4145 * automatically unregisters itself once transfer is complete.
4146 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004147static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004148{
4149 struct stream_interface *si = appctx->owner;
4150 struct stream *s = si_strm(si);
4151 struct channel *req = si_oc(si);
4152 struct channel *res = si_ic(si);
4153 struct htx *req_htx, *res_htx;
4154
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004155 /* only proxy stats are available via http */
4156 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
4157
Christopher Fauletef779222018-10-31 08:47:01 +01004158 res_htx = htx_from_buf(&res->buf);
4159
4160 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4161 goto out;
4162
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004163 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004164 if (!b_size(&res->buf)) {
4165 si_rx_room_blk(si);
4166 goto out;
4167 }
4168
4169 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004170 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4171 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004172
4173 /* all states are processed in sequence */
4174 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02004175 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004176 if (s->txn->meth == HTTP_METH_HEAD)
4177 appctx->st0 = STAT_HTTP_DONE;
4178 else
4179 appctx->st0 = STAT_HTTP_DUMP;
4180 }
4181 }
4182
4183 if (appctx->st0 == STAT_HTTP_DUMP) {
4184 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
4185 appctx->st0 = STAT_HTTP_DONE;
4186 }
4187
4188 if (appctx->st0 == STAT_HTTP_POST) {
4189 if (stats_process_http_post(si))
4190 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004191 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004192 appctx->st0 = STAT_HTTP_DONE;
4193 }
4194
4195 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02004196 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004197 appctx->st0 = STAT_HTTP_DONE;
4198 }
4199
4200 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004201 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
4202 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004203 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004204 }
4205
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004206 if (appctx->st0 == STAT_HTTP_END) {
4207 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004208 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004209 si_shutr(si);
4210 }
4211
4212 /* eat the whole request */
4213 if (co_data(req)) {
4214 req_htx = htx_from_buf(&req->buf);
4215 co_htx_skip(req, req_htx, co_data(req));
4216 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004217 }
4218 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004219
Christopher Fauletef779222018-10-31 08:47:01 +01004220 out:
4221 /* we have left the request in the buffer for the case where we
4222 * process a POST, and this automatically re-enables activity on
4223 * read. It's better to indicate that we want to stop reading when
4224 * we're sending, so that we know there's at most one direction
4225 * deciding to wake the applet up. It saves it from looping when
4226 * emitting large blocks into small TCP windows.
4227 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004228 htx_to_buf(res_htx, &res->buf);
4229 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01004230 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01004231}
4232
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004233/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004234static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004235 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004236{
4237 int field;
4238
4239 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4240 if (!field_format(info, field))
4241 continue;
4242
Willy Tarreaueaa55372019-10-09 07:39:11 +02004243 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004244 return 0;
4245 if (!stats_emit_raw_data_field(out, &info[field]))
4246 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004247 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4248 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004249 if (!chunk_strcat(out, "\n"))
4250 return 0;
4251 }
4252 return 1;
4253}
4254
4255/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004256static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004257 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004258{
4259 int field;
4260
4261 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4262 if (!field_format(info, field))
4263 continue;
4264
Willy Tarreaueaa55372019-10-09 07:39:11 +02004265 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 +01004266 return 0;
4267 if (!stats_emit_field_tags(out, &info[field], ':'))
4268 return 0;
4269 if (!stats_emit_typed_data_field(out, &info[field]))
4270 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004271 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4272 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004273 if (!chunk_strcat(out, "\n"))
4274 return 0;
4275 }
4276 return 1;
4277}
4278
4279/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004280 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004281 * INF_TOTAL_FIELDS. If this length is less then this value, the
4282 * function returns 0, otherwise, it returns 1.
4283 */
4284int stats_fill_info(struct field *info, int len)
4285{
4286 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02004287 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004288
4289#ifdef USE_OPENSSL
4290 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
4291 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
4292 int ssl_reuse = 0;
4293
4294 if (ssl_key_rate < ssl_sess_rate) {
4295 /* count the ssl reuse ratio and avoid overflows in both directions */
4296 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
4297 }
4298#endif
4299
4300 if (len < INF_TOTAL_FIELDS)
4301 return 0;
4302
4303 chunk_reset(out);
4304 memset(info, 0, sizeof(*info) * len);
4305
4306 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004307 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004308 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004309 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004310
Yves Lafon95317282018-02-26 11:10:37 +01004311 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004312 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
4313 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
4314 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4315
4316 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
4317 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
4318
4319 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
William Dauchydefd1562021-01-15 22:41:38 +01004320 info[INF_START_TIME_SEC] = mkf_u32(FN_DURATION, start_date.tv_sec);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004321 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004322 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004323 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004324 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004325 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004326 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004327 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4328 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4329 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4330 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4331 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4332 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4333 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4334 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4335#ifdef USE_OPENSSL
4336 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
4337 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
4338 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
4339#endif
4340 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4341 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4342 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
4343 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
4344 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4345 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
4346 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
4347 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4348 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4349
4350#ifdef USE_OPENSSL
4351 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
4352 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4353 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
4354 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
4355 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
4356 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
4357 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
4358 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4359 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4360 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4361#endif
4362 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
4363 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
4364 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4365#ifdef USE_ZLIB
4366 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4367 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4368#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004369 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004370 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreau81036f22019-05-20 19:24:50 +02004371 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004372 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4373 if (global.desc)
4374 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004375 info[INF_STOPPING] = mkf_u32(0, stopping);
4376 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004377 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004378 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004379 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004380 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004381 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004382 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004383 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004384 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004385 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004386 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 +02004387 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004388 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004389
4390 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4391 chunk_appendf(out, "%#x", get_tainted());
4392
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004393 return 1;
4394}
4395
4396/* This function dumps information onto the stream interface's read buffer.
4397 * It returns 0 as long as it does not complete, non-zero upon completion.
4398 * No state is used.
4399 */
4400static int stats_dump_info_to_buffer(struct stream_interface *si)
4401{
4402 struct appctx *appctx = __objt_appctx(si->end);
4403
4404 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
4405 return 0;
4406
4407 chunk_reset(&trash);
4408
4409 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004410 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01004411 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004412 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004413 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02004414 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004415
Willy Tarreau06d80a92017-10-19 14:32:15 +02004416 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004417 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004418 return 0;
4419 }
4420
4421 return 1;
4422}
4423
Simon Horman6f6bb382017-01-04 09:37:26 +01004424/* This function dumps the schema onto the stream interface's read buffer.
4425 * It returns 0 as long as it does not complete, non-zero upon completion.
4426 * No state is used.
4427 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004428 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004429 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4430 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004431static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004432{
4433
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004434 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004435
4436 chunk_strcat(out,
4437 "{"
4438 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4439 "\"oneOf\":["
4440 "{"
4441 "\"title\":\"Info\","
4442 "\"type\":\"array\","
4443 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004444 "\"title\":\"InfoItem\","
4445 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004446 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004447 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4448 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4449 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4450 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4451 "},"
4452 "\"required\":[\"field\",\"processNum\",\"tags\","
4453 "\"value\"]"
4454 "}"
4455 "},"
4456 "{"
4457 "\"title\":\"Stat\","
4458 "\"type\":\"array\","
4459 "\"items\":{"
4460 "\"title\":\"InfoItem\","
4461 "\"type\":\"object\","
4462 "\"properties\":{"
4463 "\"objType\":{"
4464 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4465 "\"Server\",\"Unknown\"]"
4466 "},"
4467 "\"proxyId\":{"
4468 "\"type\":\"integer\","
4469 "\"minimum\":0"
4470 "},"
4471 "\"id\":{"
4472 "\"type\":\"integer\","
4473 "\"minimum\":0"
4474 "},"
4475 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4476 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4477 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4478 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4479 "},"
4480 "\"required\":[\"objType\",\"proxyId\",\"id\","
4481 "\"field\",\"processNum\",\"tags\","
4482 "\"value\"]"
4483 "}"
4484 "},"
4485 "{"
4486 "\"title\":\"Error\","
4487 "\"type\":\"object\","
4488 "\"properties\":{"
4489 "\"errorStr\":{"
4490 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004491 "}"
4492 "},"
4493 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004494 "}"
4495 "],"
4496 "\"definitions\":{"
4497 "\"field\":{"
4498 "\"type\":\"object\","
4499 "\"pos\":{"
4500 "\"type\":\"integer\","
4501 "\"minimum\":0"
4502 "},"
4503 "\"name\":{"
4504 "\"type\":\"string\""
4505 "},"
4506 "\"required\":[\"pos\",\"name\"]"
4507 "},"
4508 "\"processNum\":{"
4509 "\"type\":\"integer\","
4510 "\"minimum\":1"
4511 "},"
4512 "\"tags\":{"
4513 "\"type\":\"object\","
4514 "\"origin\":{"
4515 "\"type\":\"string\","
4516 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4517 "\"Config\",\"Product\",\"Unknown\"]"
4518 "},"
4519 "\"nature\":{"
4520 "\"type\":\"string\","
4521 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4522 "\"Rate\",\"Counter\",\"Duration\","
4523 "\"Age\",\"Time\",\"Name\",\"Output\","
4524 "\"Avg\", \"Unknown\"]"
4525 "},"
4526 "\"scope\":{"
4527 "\"type\":\"string\","
4528 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4529 "\"System\",\"Unknown\"]"
4530 "},"
4531 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4532 "},"
4533 "\"typedValue\":{"
4534 "\"type\":\"object\","
4535 "\"oneOf\":["
4536 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4537 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4538 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4539 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4540 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4541 "],"
4542 "\"definitions\":{"
4543 "\"s32Value\":{"
4544 "\"properties\":{"
4545 "\"type\":{"
4546 "\"type\":\"string\","
4547 "\"enum\":[\"s32\"]"
4548 "},"
4549 "\"value\":{"
4550 "\"type\":\"integer\","
4551 "\"minimum\":-2147483648,"
4552 "\"maximum\":2147483647"
4553 "}"
4554 "},"
4555 "\"required\":[\"type\",\"value\"]"
4556 "},"
4557 "\"s64Value\":{"
4558 "\"properties\":{"
4559 "\"type\":{"
4560 "\"type\":\"string\","
4561 "\"enum\":[\"s64\"]"
4562 "},"
4563 "\"value\":{"
4564 "\"type\":\"integer\","
4565 "\"minimum\":-9007199254740991,"
4566 "\"maximum\":9007199254740991"
4567 "}"
4568 "},"
4569 "\"required\":[\"type\",\"value\"]"
4570 "},"
4571 "\"u32Value\":{"
4572 "\"properties\":{"
4573 "\"type\":{"
4574 "\"type\":\"string\","
4575 "\"enum\":[\"u32\"]"
4576 "},"
4577 "\"value\":{"
4578 "\"type\":\"integer\","
4579 "\"minimum\":0,"
4580 "\"maximum\":4294967295"
4581 "}"
4582 "},"
4583 "\"required\":[\"type\",\"value\"]"
4584 "},"
4585 "\"u64Value\":{"
4586 "\"properties\":{"
4587 "\"type\":{"
4588 "\"type\":\"string\","
4589 "\"enum\":[\"u64\"]"
4590 "},"
4591 "\"value\":{"
4592 "\"type\":\"integer\","
4593 "\"minimum\":0,"
4594 "\"maximum\":9007199254740991"
4595 "}"
4596 "},"
4597 "\"required\":[\"type\",\"value\"]"
4598 "},"
4599 "\"strValue\":{"
4600 "\"properties\":{"
4601 "\"type\":{"
4602 "\"type\":\"string\","
4603 "\"enum\":[\"str\"]"
4604 "},"
4605 "\"value\":{\"type\":\"string\"}"
4606 "},"
4607 "\"required\":[\"type\",\"value\"]"
4608 "},"
4609 "\"unknownValue\":{"
4610 "\"properties\":{"
4611 "\"type\":{"
4612 "\"type\":\"integer\","
4613 "\"minimum\":0"
4614 "},"
4615 "\"value\":{"
4616 "\"type\":\"string\","
4617 "\"enum\":[\"unknown\"]"
4618 "}"
4619 "},"
4620 "\"required\":[\"type\",\"value\"]"
4621 "}"
4622 "}"
4623 "}"
4624 "}"
4625 "}");
4626
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004627 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004628 chunk_reset(out);
4629 chunk_appendf(out,
4630 "{\"errorStr\":\"output buffer too short\"}");
4631 }
4632}
4633
4634/* This function dumps the schema onto the stream interface's read buffer.
4635 * It returns 0 as long as it does not complete, non-zero upon completion.
4636 * No state is used.
4637 */
4638static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
4639{
4640 chunk_reset(&trash);
4641
4642 stats_dump_json_schema(&trash);
4643
Willy Tarreau06d80a92017-10-19 14:32:15 +02004644 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004645 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01004646 return 0;
4647 }
4648
4649 return 1;
4650}
4651
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004652static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004653{
4654 struct proxy *px;
4655 struct server *sv;
4656 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004657 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004658 int clrall = 0;
4659
4660 if (strcmp(args[2], "all") == 0)
4661 clrall = 1;
4662
4663 /* check permissions */
4664 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4665 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4666 return 1;
4667
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004668 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004669 if (clrall) {
4670 memset(&px->be_counters, 0, sizeof(px->be_counters));
4671 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4672 }
4673 else {
4674 px->be_counters.conn_max = 0;
4675 px->be_counters.p.http.rps_max = 0;
4676 px->be_counters.sps_max = 0;
4677 px->be_counters.cps_max = 0;
4678 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004679 px->be_counters.qtime_max = 0;
4680 px->be_counters.ctime_max = 0;
4681 px->be_counters.dtime_max = 0;
4682 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004683
4684 px->fe_counters.conn_max = 0;
4685 px->fe_counters.p.http.rps_max = 0;
4686 px->fe_counters.sps_max = 0;
4687 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004688 }
4689
4690 for (sv = px->srv; sv; sv = sv->next)
4691 if (clrall)
4692 memset(&sv->counters, 0, sizeof(sv->counters));
4693 else {
4694 sv->counters.cur_sess_max = 0;
4695 sv->counters.nbpend_max = 0;
4696 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004697 sv->counters.qtime_max = 0;
4698 sv->counters.ctime_max = 0;
4699 sv->counters.dtime_max = 0;
4700 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004701 }
4702
4703 list_for_each_entry(li, &px->conf.listeners, by_fe)
4704 if (li->counters) {
4705 if (clrall)
4706 memset(li->counters, 0, sizeof(*li->counters));
4707 else
4708 li->counters->conn_max = 0;
4709 }
4710 }
4711
4712 global.cps_max = 0;
4713 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004714 global.ssl_max = 0;
4715 global.ssl_fe_keys_max = 0;
4716 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004717
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004718 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4719 if (!mod->clearable && !clrall)
4720 continue;
4721
4722 for (px = proxies_list; px; px = px->next) {
4723 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4724
4725 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4726 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4727 mod,
4728 mod->counters,
4729 mod->counters_size);
4730 }
4731
4732 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4733 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4734 mod,
4735 mod->counters,
4736 mod->counters_size);
4737 }
4738
4739 if (mod_cap & STATS_PX_CAP_SRV) {
4740 for (sv = px->srv; sv; sv = sv->next) {
4741 EXTRA_COUNTERS_INIT(sv->extra_counters,
4742 mod,
4743 mod->counters,
4744 mod->counters_size);
4745 }
4746 }
4747
4748 if (mod_cap & STATS_PX_CAP_LI) {
4749 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4750 EXTRA_COUNTERS_INIT(li->extra_counters,
4751 mod,
4752 mod->counters,
4753 mod->counters_size);
4754 }
4755 }
4756 }
4757 }
4758
Emeric Brund3b44952021-01-06 16:01:02 +01004759 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_DNS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004760
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004761 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004762 return 1;
4763}
4764
4765
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004766static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004767{
Willy Tarreau2f397382019-10-09 11:43:59 +02004768 int arg = 2;
4769
Willy Tarreaud25fc792016-12-16 12:33:47 +01004770 appctx->ctx.stats.scope_str = 0;
4771 appctx->ctx.stats.scope_len = 0;
4772 appctx->ctx.stats.flags = 0;
4773
Willy Tarreau2f397382019-10-09 11:43:59 +02004774 while (*args[arg]) {
4775 if (strcmp(args[arg], "typed") == 0)
4776 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4777 else if (strcmp(args[arg], "json") == 0)
4778 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4779 else if (strcmp(args[arg], "desc") == 0)
4780 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4781 arg++;
4782 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004783 return 0;
4784}
4785
4786
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004787static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004788{
Willy Tarreau2f397382019-10-09 11:43:59 +02004789 int arg = 2;
4790
Willy Tarreaud25fc792016-12-16 12:33:47 +01004791 appctx->ctx.stats.scope_str = 0;
4792 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004793 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4794
4795 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
4796 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004797
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004798 /* proxy is the default domain */
4799 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004800 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004801 ++args;
4802
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004803 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004804 ++args;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004805 } else if (strcmp(args[arg], "dns") == 0) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004806 appctx->ctx.stats.domain = STATS_DOMAIN_DNS;
4807 ++args;
4808 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004809 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004810 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004811 }
4812
4813 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4814 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004815 struct proxy *px;
4816
Willy Tarreau2f397382019-10-09 11:43:59 +02004817 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004818 if (px)
4819 appctx->ctx.stats.iid = px->uuid;
4820 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004821 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004822
Willy Tarreau9d008692019-08-09 11:21:01 +02004823 if (!appctx->ctx.stats.iid)
4824 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004825
Willy Tarreau2b812e22016-11-22 16:18:05 +01004826 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004827 appctx->ctx.stats.type = atoi(args[arg+1]);
4828 appctx->ctx.stats.sid = atoi(args[arg+2]);
4829 arg += 3;
4830 }
4831
4832 while (*args[arg]) {
4833 if (strcmp(args[arg], "typed") == 0)
4834 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4835 else if (strcmp(args[arg], "json") == 0)
4836 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4837 else if (strcmp(args[arg], "desc") == 0)
4838 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02004839 else if (strcmp(args[arg], "no-maint") == 0)
4840 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02004841 else if (strcmp(args[arg], "up") == 0)
4842 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02004843 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004844 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004845
Willy Tarreau2b812e22016-11-22 16:18:05 +01004846 return 0;
4847}
4848
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004849static int cli_io_handler_dump_info(struct appctx *appctx)
4850{
4851 return stats_dump_info_to_buffer(appctx->owner);
4852}
4853
Willy Tarreau2b812e22016-11-22 16:18:05 +01004854/* This I/O handler runs as an applet embedded in a stream interface. It is
4855 * used to send raw stats over a socket.
4856 */
4857static int cli_io_handler_dump_stat(struct appctx *appctx)
4858{
Christopher Fauletef779222018-10-31 08:47:01 +01004859 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004860}
4861
Simon Horman6f6bb382017-01-04 09:37:26 +01004862static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4863{
4864 return stats_dump_json_schema_to_buffer(appctx->owner);
4865}
4866
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01004867int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
4868 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004869{
4870 struct stats_module *mod;
4871
4872 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
4873
4874 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4875 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4876 continue;
4877
4878 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
4879 }
4880
4881 EXTRA_COUNTERS_ALLOC(*counters, 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_INIT(*counters, mod, mod->counters, mod->counters_size);
4888 }
4889
4890 return 1;
4891
4892 alloc_failed:
4893 return 0;
4894}
4895
4896/* Initialize and allocate all extra counters for a proxy and its attached
4897 * servers/listeners with all already registered stats module
4898 */
4899int stats_allocate_proxy_counters(struct proxy *px)
4900{
4901 struct server *sv;
4902 struct listener *li;
4903
4904 if (px->cap & PR_CAP_FE) {
4905 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
4906 COUNTERS_FE,
4907 STATS_PX_CAP_FE)) {
4908 return 0;
4909 }
4910 }
4911
4912 if (px->cap & PR_CAP_BE) {
4913 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
4914 COUNTERS_BE,
4915 STATS_PX_CAP_BE)) {
4916 return 0;
4917 }
4918 }
4919
4920 for (sv = px->srv; sv; sv = sv->next) {
4921 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
4922 COUNTERS_SV,
4923 STATS_PX_CAP_SRV)) {
4924 return 0;
4925 }
4926 }
4927
4928 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4929 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
4930 COUNTERS_LI,
4931 STATS_PX_CAP_LI)) {
4932 return 0;
4933 }
4934 }
4935
4936 return 1;
4937}
4938
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004939void stats_register_module(struct stats_module *m)
4940{
4941 const uint8_t domain = stats_get_domain(m->domain_flags);
4942
Willy Tarreau2b718102021-04-21 07:32:39 +02004943 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004944 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004945}
4946
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004947static int allocate_stats_px_postcheck(void)
4948{
4949 struct stats_module *mod;
4950 size_t i = ST_F_TOTAL_FIELDS;
4951 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004952 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004953
4954 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
4955
4956 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
4957 if (!stat_f[STATS_DOMAIN_PROXY]) {
4958 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
4959 err_code |= ERR_ALERT | ERR_FATAL;
4960 return err_code;
4961 }
4962
4963 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
4964 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
4965
4966 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4967 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
4968 mod->stats,
4969 mod->stats_count * sizeof(struct name_desc));
4970 i += mod->stats_count;
4971 }
4972
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004973 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004974 if (!stats_allocate_proxy_counters(px)) {
4975 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
4976 err_code |= ERR_ALERT | ERR_FATAL;
4977 return err_code;
4978 }
4979 }
4980
Christopher Fauletde79cd22021-01-06 07:41:56 +01004981 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004982
4983 return err_code;
4984}
4985
4986REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
4987
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004988static int allocate_stats_dns_postcheck(void)
4989{
4990 struct stats_module *mod;
4991 size_t i = 0;
4992 int err_code = 0;
4993
4994 stat_f[STATS_DOMAIN_DNS] = malloc(stat_count[STATS_DOMAIN_DNS] * sizeof(struct name_desc));
4995 if (!stat_f[STATS_DOMAIN_DNS]) {
4996 ha_alert("stats: cannot allocate all fields for dns statistics\n");
4997 err_code |= ERR_ALERT | ERR_FATAL;
4998 return err_code;
4999 }
5000
5001 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_DNS], list) {
5002 memcpy(stat_f[STATS_DOMAIN_DNS] + i,
5003 mod->stats,
5004 mod->stats_count * sizeof(struct name_desc));
5005 i += mod->stats_count;
5006 }
5007
Emeric Brund3b44952021-01-06 16:01:02 +01005008 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_DNS])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005009 ha_alert("stats: cannot allocate all counters for dns statistics\n");
5010 err_code |= ERR_ALERT | ERR_FATAL;
5011 return err_code;
5012 }
5013
Christopher Fauletde79cd22021-01-06 07:41:56 +01005014 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005015
5016 return err_code;
5017}
5018
5019REGISTER_CONFIG_POSTPARSER("allocate-stats-dns", allocate_stats_dns_postcheck);
5020
Christopher Fauletde79cd22021-01-06 07:41:56 +01005021static int allocate_stat_lines_per_thread(void)
5022{
5023 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5024
5025 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5026 const int domain = domains[i];
5027
5028 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5029 if (!stat_l[domain])
5030 return 0;
5031 }
5032 return 1;
5033}
5034
5035REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5036
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005037static int allocate_trash_counters(void)
5038{
5039 struct stats_module *mod;
5040 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5041 size_t max_counters_size = 0;
5042
5043 /* calculate the greatest counters used by any stats modules */
5044 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5045 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5046 max_counters_size = mod->counters_size > max_counters_size ?
5047 mod->counters_size : max_counters_size;
5048 }
5049 }
5050
5051 /* allocate the trash with the size of the greatest counters */
5052 if (max_counters_size) {
5053 trash_counters = malloc(max_counters_size);
5054 if (!trash_counters) {
5055 ha_alert("stats: cannot allocate trash counters for statistics\n");
5056 return 0;
5057 }
5058 }
5059
5060 return 1;
5061}
5062
5063REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5064
Christopher Fauletde79cd22021-01-06 07:41:56 +01005065static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005066{
5067 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5068
5069 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5070 const int domain = domains[i];
5071
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005072 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005073 }
5074}
5075
5076
5077REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5078
5079static void deinit_stats(void)
5080{
5081 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5082
5083 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5084 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005085
5086 if (stat_f[domain])
5087 free(stat_f[domain]);
5088 }
5089}
5090
5091REGISTER_POST_DEINIT(deinit_stats);
5092
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005093static void free_trash_counters(void)
5094{
5095 if (trash_counters)
5096 free(trash_counters);
5097}
5098
5099REGISTER_PER_THREAD_FREE(free_trash_counters);
5100
Willy Tarreau2b812e22016-11-22 16:18:05 +01005101/* register cli keywords */
5102static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005103 { { "clear", "counters", NULL }, "clear counters [all] : clear max statistics counters (or all counters)", cli_parse_clear_counters, NULL, NULL },
5104 { { "show", "info", NULL }, "show info [desc|json|typed]* : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
5105 { { "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 },
5106 { { "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 +01005107 {{},}
5108}};
5109
Willy Tarreau0108d902018-11-25 19:14:37 +01005110INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5111
William Lallemand74c24fb2016-11-21 17:18:36 +01005112struct applet http_stats_applet = {
5113 .obj_type = OBJ_TYPE_APPLET,
5114 .name = "<STATS>", /* used for logging */
5115 .fct = http_stats_io_handler,
5116 .release = NULL,
5117};
5118
William Lallemand74c24fb2016-11-21 17:18:36 +01005119/*
5120 * Local variables:
5121 * c-indent-level: 8
5122 * c-basic-offset: 8
5123 * End:
5124 */