blob: 73bd68a7b85189152f91257d8b1b7eaf7fb02569 [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 Tarreaueb92deb2020-06-04 10:53:16 +020037#include <haproxy/dns.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020044#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020045#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020046#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020047#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020048#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020049#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020050#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020051#include <haproxy/pipe.h>
52#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020053#include <haproxy/proxy.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)" },
94 [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 +010095 [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 +020096 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +010097 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020098 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +010099 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200100 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
101 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
102 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
103 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
104 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
105 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
106 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
107 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
108 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
109 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
110 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
111 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
112 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
113 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
114 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
115 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
116 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
117 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
118 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
119 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
120 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
121 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
122 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
123 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
124 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
125 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
126 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
127 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
129 [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" },
130 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
131 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
132 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
133 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
134 [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)" },
135 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
136 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
137 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
138 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
139 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
140 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
141 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
142 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
143 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
144 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
145 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
146 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
147 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
148 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
149 [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 +0200150 [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 +0200151 [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 +0200152 [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 +0200153 [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 +0100154 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100155};
156
Willy Tarreaueaa55372019-10-09 07:39:11 +0200157const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200158 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
159 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
160 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
161 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
162 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
163 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
164 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
165 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
166 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
167 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
168 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
169 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
170 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
171 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
172 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
173 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
174 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
175 [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 +0200176 [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 +0200177 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
178 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
179 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
180 [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" },
181 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
182 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
183 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
184 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
185 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
186 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
187 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
188 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
189 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
190 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
191 [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)" },
192 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
193 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
194 [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" },
195 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
196 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
197 [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" },
198 [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" },
199 [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" },
200 [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" },
201 [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" },
202 [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)" },
203 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
204 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
205 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
206 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
207 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
208 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
209 [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" },
210 [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" },
211 [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)" },
212 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
213 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
214 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
215 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
216 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
217 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
218 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
219 [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)" },
220 [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" },
221 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
222 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
223 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
224 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
225 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
226 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
227 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
228 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
229 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
230 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
231 [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" },
232 [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" },
233 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
234 [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" },
235 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
236 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
237 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
238 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
239 [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" },
240 [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" },
241 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
242 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
243 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
244 [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" },
245 [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" },
246 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
247 [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 +0100248 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
249 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
250 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
251 [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 +0100252 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200253 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
254 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
255 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200256 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200257 [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 +0100258};
259
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100260/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100261THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200262
263/* description of statistics (static and dynamic) */
264static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
265static size_t stat_count[STATS_DOMAIN_COUNT];
266
267/* one line for stats */
Christopher Fauletde79cd22021-01-06 07:41:56 +0100268static THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100269
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200270/* list of all registered stats module */
271static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
272 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200273 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_DNS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200274};
275
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100276THREAD_LOCAL void *trash_counters;
277
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200278static inline uint8_t stats_get_domain(uint32_t domain)
279{
280 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
281}
282
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200283static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
284{
285 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
286}
287
Christopher Faulet6338a082019-09-09 15:50:54 +0200288static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100289
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200290int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100291{
292 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100293 if (chk->data >= channel_htx_recv_max(chn, htx))
294 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200295 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100296 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100297 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100298 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100299 }
300 else {
301 if (ci_putchk(chn, chk) == -1)
302 return 0;
303 }
304 return 1;
305}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100306
Christopher Fauleted7a0662019-01-14 11:07:34 +0100307static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
308{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200309 struct channel *req = si_oc(si);
310 struct htx *htx = htxbuf(&req->buf);
311 struct htx_blk *blk;
312 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100313
Christopher Fauletb7f88902019-07-15 21:56:43 +0200314 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100315 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200316 ALREADY_CHECKED(blk);
317 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
318 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100319}
320
William Lallemand74c24fb2016-11-21 17:18:36 +0100321/*
322 * http_stats_io_handler()
323 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
324 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100325 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100326 * -> stats_dump_html_head() // emits the HTML headers
327 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
328 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
329 * -> stats_dump_html_px_hdr()
330 * -> stats_dump_fe_stats()
331 * -> stats_dump_li_stats()
332 * -> stats_dump_sv_stats()
333 * -> stats_dump_be_stats()
334 * -> stats_dump_html_px_end()
335 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100336 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100337 */
338
339
William Lallemand74c24fb2016-11-21 17:18:36 +0100340/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
341 * for clearing it if needed.
342 * NOTE: Some tools happen to rely on the field position instead of its name,
343 * so please only append new fields at the end, never in the middle.
344 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200345static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100346{
347 int field;
348
349 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200350 if (stat_f[domain]) {
351 for (field = 0; field < stat_count[domain]; ++field) {
352 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100353
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200354 /* print special delimiter on proxy stats to mark end of
355 static fields */
356 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
357 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200358 }
359 }
360
William Lallemand74c24fb2016-11-21 17:18:36 +0100361 chunk_appendf(&trash, "\n");
362}
363
William Lallemand74c24fb2016-11-21 17:18:36 +0100364/* Emits a stats field without any surrounding element and properly encoded to
365 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
366 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200367int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100368{
369 switch (field_format(f, 0)) {
370 case FF_EMPTY: return 1;
371 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
372 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
373 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
374 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200375 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100376 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
377 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
378 }
379}
380
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200381const char *field_to_html_str(const struct field *f)
382{
383 switch (field_format(f, 0)) {
384 case FF_S32: return U2H(f->u.s32);
385 case FF_S64: return U2H(f->u.s64);
386 case FF_U64: return U2H(f->u.u64);
387 case FF_U32: return U2H(f->u.u32);
388 case FF_STR: return field_str(f, 0);
389 case FF_EMPTY:
390 default:
391 return "";
392 }
393}
394
William Lallemand74c24fb2016-11-21 17:18:36 +0100395/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
396 * output is supposed to be used on its own line. Returns non-zero on success, 0
397 * if the buffer is full.
398 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200399int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100400{
401 switch (field_format(f, 0)) {
402 case FF_EMPTY: return 1;
403 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
404 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
405 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
406 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200407 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100408 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
409 default: return chunk_appendf(out, "%08x:?", f->type);
410 }
411}
412
Simon Horman05ee2132017-01-04 09:37:25 +0100413/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
414 * the recommendation for interoperable integers in section 6 of RFC 7159.
415 */
416#define JSON_INT_MAX ((1ULL << 53) - 1)
417#define JSON_INT_MIN (0 - JSON_INT_MAX)
418
419/* Emits a stats field value and its type in JSON.
420 * Returns non-zero on success, 0 on error.
421 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200422int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100423{
424 int old_len;
425 char buf[20];
426 const char *type, *value = buf, *quote = "";
427
428 switch (field_format(f, 0)) {
429 case FF_EMPTY: return 1;
430 case FF_S32: type = "\"s32\"";
431 snprintf(buf, sizeof(buf), "%d", f->u.s32);
432 break;
433 case FF_U32: type = "\"u32\"";
434 snprintf(buf, sizeof(buf), "%u", f->u.u32);
435 break;
436 case FF_S64: type = "\"s64\"";
437 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
438 return 0;
439 type = "\"s64\"";
440 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
441 break;
442 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
443 return 0;
444 type = "\"u64\"";
445 snprintf(buf, sizeof(buf), "%llu",
446 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200447 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200448 case FF_FLT: type = "\"flt\"";
449 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100450 break;
451 case FF_STR: type = "\"str\"";
452 value = field_str(f, 0);
453 quote = "\"";
454 break;
455 default: snprintf(buf, sizeof(buf), "%u", f->type);
456 type = buf;
457 value = "unknown";
458 quote = "\"";
459 break;
460 }
461
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200462 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100463 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
464 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100466}
467
William Lallemand74c24fb2016-11-21 17:18:36 +0100468/* Emits an encoding of the field type on 3 characters followed by a delimiter.
469 * Returns non-zero on success, 0 if the buffer is full.
470 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200471int stats_emit_field_tags(struct buffer *out, const struct field *f,
472 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100473{
474 char origin, nature, scope;
475
476 switch (field_origin(f, 0)) {
477 case FO_METRIC: origin = 'M'; break;
478 case FO_STATUS: origin = 'S'; break;
479 case FO_KEY: origin = 'K'; break;
480 case FO_CONFIG: origin = 'C'; break;
481 case FO_PRODUCT: origin = 'P'; break;
482 default: origin = '?'; break;
483 }
484
485 switch (field_nature(f, 0)) {
486 case FN_GAUGE: nature = 'G'; break;
487 case FN_LIMIT: nature = 'L'; break;
488 case FN_MIN: nature = 'm'; break;
489 case FN_MAX: nature = 'M'; break;
490 case FN_RATE: nature = 'R'; break;
491 case FN_COUNTER: nature = 'C'; break;
492 case FN_DURATION: nature = 'D'; break;
493 case FN_AGE: nature = 'A'; break;
494 case FN_TIME: nature = 'T'; break;
495 case FN_NAME: nature = 'N'; break;
496 case FN_OUTPUT: nature = 'O'; break;
497 case FN_AVG: nature = 'a'; break;
498 default: nature = '?'; break;
499 }
500
501 switch (field_scope(f, 0)) {
502 case FS_PROCESS: scope = 'P'; break;
503 case FS_SERVICE: scope = 'S'; break;
504 case FS_SYSTEM: scope = 's'; break;
505 case FS_CLUSTER: scope = 'C'; break;
506 default: scope = '?'; break;
507 }
508
509 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
510}
511
Simon Horman05ee2132017-01-04 09:37:25 +0100512/* Emits an encoding of the field type as JSON.
513 * Returns non-zero on success, 0 if the buffer is full.
514 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200515int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100516{
517 const char *origin, *nature, *scope;
518 int old_len;
519
520 switch (field_origin(f, 0)) {
521 case FO_METRIC: origin = "Metric"; break;
522 case FO_STATUS: origin = "Status"; break;
523 case FO_KEY: origin = "Key"; break;
524 case FO_CONFIG: origin = "Config"; break;
525 case FO_PRODUCT: origin = "Product"; break;
526 default: origin = "Unknown"; break;
527 }
528
529 switch (field_nature(f, 0)) {
530 case FN_GAUGE: nature = "Gauge"; break;
531 case FN_LIMIT: nature = "Limit"; break;
532 case FN_MIN: nature = "Min"; break;
533 case FN_MAX: nature = "Max"; break;
534 case FN_RATE: nature = "Rate"; break;
535 case FN_COUNTER: nature = "Counter"; break;
536 case FN_DURATION: nature = "Duration"; break;
537 case FN_AGE: nature = "Age"; break;
538 case FN_TIME: nature = "Time"; break;
539 case FN_NAME: nature = "Name"; break;
540 case FN_OUTPUT: nature = "Output"; break;
541 case FN_AVG: nature = "Avg"; break;
542 default: nature = "Unknown"; break;
543 }
544
545 switch (field_scope(f, 0)) {
546 case FS_PROCESS: scope = "Process"; break;
547 case FS_SERVICE: scope = "Service"; break;
548 case FS_SYSTEM: scope = "System"; break;
549 case FS_CLUSTER: scope = "Cluster"; break;
550 default: scope = "Unknown"; break;
551 }
552
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200553 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100554 chunk_appendf(out, "\"tags\":{"
555 "\"origin\":\"%s\","
556 "\"nature\":\"%s\","
557 "\"scope\":\"%s\""
558 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200559 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100560}
William Lallemand74c24fb2016-11-21 17:18:36 +0100561
562/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200563static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200564 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200565 unsigned int flags,
566 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100567{
568 int field;
569
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200570 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100571 if (!stats_emit_raw_data_field(out, &stats[field]))
572 return 0;
573 if (!chunk_strcat(out, ","))
574 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200575
576 /* print special delimiter on proxy stats to mark end of
577 static fields */
578 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
579 if (!chunk_strcat(out, "-,"))
580 return 0;
581 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100582 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200583
William Lallemand74c24fb2016-11-21 17:18:36 +0100584 chunk_strcat(out, "\n");
585 return 1;
586}
587
588/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200589static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200590 const struct field *stats,
591 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200592 unsigned int flags,
593 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100594{
595 int field;
596
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200597 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100598 if (!stats[field].type)
599 continue;
600
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200601 switch (domain) {
602 case STATS_DOMAIN_PROXY:
603 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
604 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
605 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
606 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
607 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
608 '?',
609 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
610 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200611 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200612 stats[ST_F_PID].u.u32);
613 break;
614
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200615 case STATS_DOMAIN_DNS:
616 chunk_appendf(out, "D.%d.%s:", field,
617 stat_f[domain][field].name);
618 break;
619
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200620 default:
621 break;
622 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100623
624 if (!stats_emit_field_tags(out, &stats[field], ':'))
625 return 0;
626 if (!stats_emit_typed_data_field(out, &stats[field]))
627 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200628
629 if (flags & STAT_SHOW_FDESC
630 && !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].name)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200631 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200632 }
633
William Lallemand74c24fb2016-11-21 17:18:36 +0100634 if (!chunk_strcat(out, "\n"))
635 return 0;
636 }
637 return 1;
638}
639
Simon Horman05ee2132017-01-04 09:37:25 +0100640/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200641static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200642 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100643{
644 int field;
645 int started = 0;
646
647 if (!chunk_strcat(out, "["))
648 return 0;
649
650 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
651 int old_len;
652
653 if (!field_format(info, field))
654 continue;
655
656 if (started && !chunk_strcat(out, ","))
657 goto err;
658 started = 1;
659
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200660 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100661 chunk_appendf(out,
662 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
663 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200664 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100665 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200666 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100667 goto err;
668
669 if (!stats_emit_json_field_tags(out, &info[field]))
670 goto err;
671
672 if (!stats_emit_json_data_field(out, &info[field]))
673 goto err;
674
675 if (!chunk_strcat(out, "}"))
676 goto err;
677 }
678
679 if (!chunk_strcat(out, "]"))
680 goto err;
681 return 1;
682
683err:
684 chunk_reset(out);
685 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
686 return 0;
687}
688
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200689static void stats_print_proxy_field_json(struct buffer *out,
690 const struct field *stat,
691 const char *name,
692 int pos,
693 uint32_t field_type,
694 uint32_t iid,
695 uint32_t sid,
696 uint32_t pid)
697{
698 const char *obj_type;
699 switch (field_type) {
700 case STATS_TYPE_FE: obj_type = "Frontend"; break;
701 case STATS_TYPE_BE: obj_type = "Backend"; break;
702 case STATS_TYPE_SO: obj_type = "Listener"; break;
703 case STATS_TYPE_SV: obj_type = "Server"; break;
704 default: obj_type = "Unknown"; break;
705 }
706
707 chunk_appendf(out,
708 "{"
709 "\"objType\":\"%s\","
710 "\"proxyId\":%u,"
711 "\"id\":%u,"
712 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
713 "\"processNum\":%u,",
714 obj_type, iid, sid, pos, name, pid);
715}
716
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200717static void stats_print_dns_field_json(struct buffer *out,
718 const struct field *stat,
719 const char *name,
720 int pos)
721{
722 chunk_appendf(out,
723 "{"
724 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
725 pos, name);
726}
727
728
Simon Horman05ee2132017-01-04 09:37:25 +0100729/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200730static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200731 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200732 unsigned int flags,
733 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100734{
735 int field;
736 int started = 0;
737
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200738 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100739 return 0;
740 if (!chunk_strcat(out, "["))
741 return 0;
742
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200743 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100744 int old_len;
745
746 if (!stats[field].type)
747 continue;
748
749 if (started && !chunk_strcat(out, ","))
750 goto err;
751 started = 1;
752
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200754 if (domain == STATS_DOMAIN_PROXY) {
755 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200756 stat_f[domain][field].name,
757 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200758 stats[ST_F_TYPE].u.u32,
759 stats[ST_F_IID].u.u32,
760 stats[ST_F_SID].u.u32,
761 stats[ST_F_PID].u.u32);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200762 } else if (domain == STATS_DOMAIN_DNS) {
763 stats_print_dns_field_json(out, &stats[field],
764 stat_f[domain][field].name,
765 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200766 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200767
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100769 goto err;
770
771 if (!stats_emit_json_field_tags(out, &stats[field]))
772 goto err;
773
774 if (!stats_emit_json_data_field(out, &stats[field]))
775 goto err;
776
777 if (!chunk_strcat(out, "}"))
778 goto err;
779 }
780
781 if (!chunk_strcat(out, "]"))
782 goto err;
783
784 return 1;
785
786err:
787 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200788 if (flags & STAT_STARTED)
789 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100790 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
791 return 0;
792}
793
William Lallemand74c24fb2016-11-21 17:18:36 +0100794/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200795 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200796 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
797 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
798 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100799 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200800static int stats_dump_fields_html(struct buffer *out,
801 const struct field *stats,
802 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100803{
Willy Tarreau83061a82018-07-13 11:56:34 +0200804 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200805 struct stats_module *mod;
806 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100807
808 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
809 chunk_appendf(out,
810 /* name, queue */
811 "<tr class=\"frontend\">");
812
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200813 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100814 /* Column sub-heading for Enable or Disable server */
815 chunk_appendf(out, "<td></td>");
816 }
817
818 chunk_appendf(out,
819 "<td class=ac>"
820 "<a name=\"%s/Frontend\"></a>"
821 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
822 "<td colspan=3></td>"
823 "",
824 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
825
826 chunk_appendf(out,
827 /* sessions rate : current */
828 "<td><u>%s<div class=tips><table class=det>"
829 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
830 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
831 "",
832 U2H(stats[ST_F_RATE].u.u32),
833 U2H(stats[ST_F_CONN_RATE].u.u32),
834 U2H(stats[ST_F_RATE].u.u32));
835
836 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
837 chunk_appendf(out,
838 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
839 U2H(stats[ST_F_REQ_RATE].u.u32));
840
841 chunk_appendf(out,
842 "</table></div></u></td>"
843 /* sessions rate : max */
844 "<td><u>%s<div class=tips><table class=det>"
845 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
846 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
847 "",
848 U2H(stats[ST_F_RATE_MAX].u.u32),
849 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
850 U2H(stats[ST_F_RATE_MAX].u.u32));
851
852 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
853 chunk_appendf(out,
854 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
855 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
856
857 chunk_appendf(out,
858 "</table></div></u></td>"
859 /* sessions rate : limit */
860 "<td>%s</td>",
861 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
862
863 chunk_appendf(out,
864 /* sessions: current, max, limit, total */
865 "<td>%s</td><td>%s</td><td>%s</td>"
866 "<td><u>%s<div class=tips><table class=det>"
867 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
868 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
869 "",
870 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
871 U2H(stats[ST_F_STOT].u.u64),
872 U2H(stats[ST_F_CONN_TOT].u.u64),
873 U2H(stats[ST_F_STOT].u.u64));
874
875 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
876 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
877 chunk_appendf(out,
878 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
879 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
880 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
881 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
882 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
883 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
884 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
885 "<tr><th>- other responses:</th><td>%s</td></tr>"
886 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100887 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
888 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200889 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100890 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100891 "",
892 U2H(stats[ST_F_REQ_TOT].u.u64),
893 U2H(stats[ST_F_HRSP_1XX].u.u64),
894 U2H(stats[ST_F_HRSP_2XX].u.u64),
895 U2H(stats[ST_F_COMP_RSP].u.u64),
896 stats[ST_F_HRSP_2XX].u.u64 ?
897 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
898 U2H(stats[ST_F_HRSP_3XX].u.u64),
899 U2H(stats[ST_F_HRSP_4XX].u.u64),
900 U2H(stats[ST_F_HRSP_5XX].u.u64),
901 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200902 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100903 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
904 U2H(stats[ST_F_CACHE_HITS].u.u64),
905 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
906 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100907 U2H(stats[ST_F_WREW].u.u64),
908 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100909 }
910
911 chunk_appendf(out,
912 "</table></div></u></td>"
913 /* sessions: lbtot, lastsess */
914 "<td></td><td></td>"
915 /* bytes : in */
916 "<td>%s</td>"
917 "",
918 U2H(stats[ST_F_BIN].u.u64));
919
920 chunk_appendf(out,
921 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
922 "<td>%s%s<div class=tips><table class=det>"
923 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
924 "<tr><th>Compression in:</th><td>%s</td></tr>"
925 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
926 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
927 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
928 "</table></div>%s</td>",
929 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
930 U2H(stats[ST_F_BOUT].u.u64),
931 U2H(stats[ST_F_BOUT].u.u64),
932 U2H(stats[ST_F_COMP_IN].u.u64),
933 U2H(stats[ST_F_COMP_OUT].u.u64),
934 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
935 U2H(stats[ST_F_COMP_BYP].u.u64),
936 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
937 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,
938 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
939
940 chunk_appendf(out,
941 /* denied: req, resp */
942 "<td>%s</td><td>%s</td>"
943 /* errors : request, connect, response */
944 "<td>%s</td><td></td><td></td>"
945 /* warnings: retries, redispatches */
946 "<td></td><td></td>"
947 /* server status : reflect frontend status */
948 "<td class=ac>%s</td>"
949 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200950 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100951 "",
952 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
953 U2H(stats[ST_F_EREQ].u.u64),
954 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200955
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200956 if (flags & STAT_SHMODULES) {
957 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100958 chunk_appendf(out, "<td>");
959
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200960 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100961 chunk_appendf(out,
962 "<u>%s<div class=tips><table class=det>",
963 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200964 for (j = 0; j < mod->stats_count; ++j) {
965 chunk_appendf(out,
966 "<tr><th>%s</th><td>%s</td></tr>",
967 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
968 ++i;
969 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100970 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200971 } else {
972 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200973 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200974
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100975 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200976 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200977 }
978
979 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100980 }
981 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
982 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200983 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100984 /* Column sub-heading for Enable or Disable server */
985 chunk_appendf(out, "<td></td>");
986 }
987
988 chunk_appendf(out,
989 /* frontend name, listener name */
990 "<td class=ac><a name=\"%s/+%s\"></a>%s"
991 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
992 "",
993 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200994 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100995 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
996
Willy Tarreau708c4162019-10-09 10:19:16 +0200997 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100998 chunk_appendf(out, "<div class=tips>");
999
Willy Tarreau90807112020-02-25 08:16:33 +01001000 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001001 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1002 else if (*field_str(stats, ST_F_ADDR) == '[')
1003 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1004 else if (*field_str(stats, ST_F_ADDR))
1005 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1006
1007 /* id */
1008 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1009 }
1010
1011 chunk_appendf(out,
1012 /* queue */
1013 "%s</td><td colspan=3></td>"
1014 /* sessions rate: current, max, limit */
1015 "<td colspan=3>&nbsp;</td>"
1016 /* sessions: current, max, limit, total, lbtot, lastsess */
1017 "<td>%s</td><td>%s</td><td>%s</td>"
1018 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1019 /* bytes: in, out */
1020 "<td>%s</td><td>%s</td>"
1021 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001022 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001023 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1024 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1025
1026 chunk_appendf(out,
1027 /* denied: req, resp */
1028 "<td>%s</td><td>%s</td>"
1029 /* errors: request, connect, response */
1030 "<td>%s</td><td></td><td></td>"
1031 /* warnings: retries, redispatches */
1032 "<td></td><td></td>"
1033 /* server status: reflect listener status */
1034 "<td class=ac>%s</td>"
1035 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001036 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001037 "",
1038 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1039 U2H(stats[ST_F_EREQ].u.u64),
1040 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001041
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001042 if (flags & STAT_SHMODULES) {
1043 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001044 chunk_appendf(out, "<td>");
1045
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001046 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001047 chunk_appendf(out,
1048 "<u>%s<div class=tips><table class=det>",
1049 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001050 for (j = 0; j < mod->stats_count; ++j) {
1051 chunk_appendf(out,
1052 "<tr><th>%s</th><td>%s</td></tr>",
1053 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1054 ++i;
1055 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001056 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001057 } else {
1058 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001059 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001060
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001061 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001062 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001063 }
1064
1065 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001066 }
1067 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1068 const char *style;
1069
1070 /* determine the style to use depending on the server's state,
1071 * its health and weight. There isn't a 1-to-1 mapping between
1072 * state and styles for the cases where the server is (still)
1073 * up. The reason is that we don't want to report nolb and
1074 * drain with the same color.
1075 */
1076
1077 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1078 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1079 style = "down";
1080 }
1081 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
1082 style = "going_up";
1083 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001084 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1085 style = "draining";
1086 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001087 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
1088 style = "going_down";
1089 }
1090 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1091 style = "nolb";
1092 }
1093 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1094 style = "no_check";
1095 }
1096 else if (!stats[ST_F_CHKFAIL].type ||
1097 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1098 /* no check or max health = UP */
1099 if (stats[ST_F_WEIGHT].u.u32)
1100 style = "up";
1101 else
1102 style = "draining";
1103 }
1104 else {
1105 style = "going_down";
1106 }
1107
Willy Tarreau7b524852020-08-11 10:26:36 +02001108 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001109 chunk_appendf(out, "<tr class=\"maintain\">");
1110 else
1111 chunk_appendf(out,
1112 "<tr class=\"%s_%s\">",
1113 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1114
1115
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001116 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001117 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001118 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1119 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001120 field_str(stats, ST_F_SVNAME));
1121
1122 chunk_appendf(out,
1123 "<td class=ac><a name=\"%s/%s\"></a>%s"
1124 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1125 "",
1126 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001127 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001128 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1129
Willy Tarreau708c4162019-10-09 10:19:16 +02001130 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001131 chunk_appendf(out, "<div class=tips>");
1132
Willy Tarreau90807112020-02-25 08:16:33 +01001133 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001134 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1135 else if (*field_str(stats, ST_F_ADDR) == '[')
1136 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1137 else if (*field_str(stats, ST_F_ADDR))
1138 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1139
1140 /* id */
1141 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1142
1143 /* cookie */
1144 if (stats[ST_F_COOKIE].type) {
1145 chunk_appendf(out, ", cookie: '");
1146 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1147 chunk_htmlencode(out, &src);
1148 chunk_appendf(out, "'");
1149 }
1150
1151 chunk_appendf(out, "</div>");
1152 }
1153
1154 chunk_appendf(out,
1155 /* queue : current, max, limit */
1156 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1157 /* sessions rate : current, max, limit */
1158 "<td>%s</td><td>%s</td><td></td>"
1159 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001160 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001161 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1162 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1163
1164 chunk_appendf(out,
1165 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001166 "<td><u>%s<div class=tips>"
1167 "<table class=det>"
1168 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001169 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001170 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001171 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1172 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001173 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001174 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1175 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1176 "</table></div></u>"
1177 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001178 "<td><u>%s<div class=tips><table class=det>"
1179 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1180 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001181 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001182 U2H(stats[ST_F_SCUR].u.u32),
1183 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1184 U2H(stats[ST_F_SRV_ICUR].u.u32),
1185 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1186 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001187 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001188
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001189 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1190 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1191 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001192 U2H(stats[ST_F_STOT].u.u64),
1193 U2H(stats[ST_F_STOT].u.u64));
1194
1195 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1196 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001197 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001198 "<tr><th>New connections:</th><td>%s</td></tr>"
1199 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001200 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001201 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1202 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1203 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1204 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1205 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1206 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001207 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001208 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001209 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001210 U2H(stats[ST_F_CONNECT].u.u64),
1211 U2H(stats[ST_F_REUSE].u.u64),
1212 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1213 (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 +02001214 U2H(stats[ST_F_REQ_TOT].u.u64),
1215 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1216 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1217 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1218 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1219 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1220 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1221 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1222 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1223 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1224 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1225 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1226 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001227 U2H(stats[ST_F_WREW].u.u64),
1228 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001229 }
1230
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001231 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1232 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1233 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1234 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1235 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001236 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001237 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1238 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1239 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1240 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001241
1242 chunk_appendf(out,
1243 "</table></div></u></td>"
1244 /* sessions: lbtot, last */
1245 "<td>%s</td><td>%s</td>",
1246 U2H(stats[ST_F_LBTOT].u.u64),
1247 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1248
1249 chunk_appendf(out,
1250 /* bytes : in, out */
1251 "<td>%s</td><td>%s</td>"
1252 /* denied: req, resp */
1253 "<td></td><td>%s</td>"
1254 /* errors : request, connect */
1255 "<td></td><td>%s</td>"
1256 /* errors : response */
1257 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1258 /* warnings: retries, redispatches */
1259 "<td>%lld</td><td>%lld</td>"
1260 "",
1261 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1262 U2H(stats[ST_F_DRESP].u.u64),
1263 U2H(stats[ST_F_ECON].u.u64),
1264 U2H(stats[ST_F_ERESP].u.u64),
1265 (long long)stats[ST_F_CLI_ABRT].u.u64,
1266 (long long)stats[ST_F_SRV_ABRT].u.u64,
1267 (long long)stats[ST_F_WRETR].u.u64,
1268 (long long)stats[ST_F_WREDIS].u.u64);
1269
1270 /* status, last change */
1271 chunk_appendf(out, "<td class=ac>");
1272
1273 /* FIXME!!!!
1274 * LASTCHG should contain the last change for *this* server and must be computed
1275 * properly above, as was done below, ie: this server if maint, otherwise ref server
1276 * if tracking. Note that ref is either local or remote depending on tracking.
1277 */
1278
1279
Willy Tarreau7b524852020-08-11 10:26:36 +02001280 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001281 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1282 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001283 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001284 chunk_strcat(out, "<i>no check</i>");
1285 }
1286 else {
1287 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 +02001288 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001289 if (stats[ST_F_CHECK_HEALTH].u.u32)
1290 chunk_strcat(out, " &uarr;");
1291 }
1292 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1293 chunk_strcat(out, " &darr;");
1294 }
1295
Willy Tarreau7b524852020-08-11 10:26:36 +02001296 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001297 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1298 chunk_appendf(out,
1299 "</td><td class=ac><u> %s",
1300 field_str(stats, ST_F_AGENT_STATUS));
1301
1302 if (stats[ST_F_AGENT_CODE].type)
1303 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1304
1305 if (stats[ST_F_AGENT_DURATION].type)
1306 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1307
1308 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1309
1310 if (*field_str(stats, ST_F_LAST_AGT)) {
1311 chunk_appendf(out, ": ");
1312 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1313 chunk_htmlencode(out, &src);
1314 }
1315 chunk_appendf(out, "</div></u>");
1316 }
1317 else if (stats[ST_F_CHECK_STATUS].type) {
1318 chunk_appendf(out,
1319 "</td><td class=ac><u> %s",
1320 field_str(stats, ST_F_CHECK_STATUS));
1321
1322 if (stats[ST_F_CHECK_CODE].type)
1323 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1324
1325 if (stats[ST_F_CHECK_DURATION].type)
1326 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1327
1328 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1329
1330 if (*field_str(stats, ST_F_LAST_CHK)) {
1331 chunk_appendf(out, ": ");
1332 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1333 chunk_htmlencode(out, &src);
1334 }
1335 chunk_appendf(out, "</div></u>");
1336 }
1337 else
1338 chunk_appendf(out, "</td><td>");
1339
1340 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001341 /* weight / uweight */
1342 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001343 /* act, bck */
1344 "<td class=ac>%s</td><td class=ac>%s</td>"
1345 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001346 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001347 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1348 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1349
1350 /* check failures: unique, fatal, down time */
1351 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1352 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1353 }
1354 else if (stats[ST_F_CHKFAIL].type) {
1355 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1356
1357 if (stats[ST_F_HANAFAIL].type)
1358 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1359
1360 chunk_appendf(out,
1361 "<div class=tips>Failed Health Checks%s</div></u></td>"
1362 "<td>%lld</td><td>%s</td>"
1363 "",
1364 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1365 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1366 }
1367 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1368 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1369 chunk_appendf(out,
1370 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1371 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1372 }
1373 else
1374 chunk_appendf(out, "<td colspan=3></td>");
1375
1376 /* throttle */
1377 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001378 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001379 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001380 chunk_appendf(out, "<td class=ac>-</td>");
1381
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001382 if (flags & STAT_SHMODULES) {
1383 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001384 chunk_appendf(out, "<td>");
1385
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001386 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001387 chunk_appendf(out,
1388 "<u>%s<div class=tips><table class=det>",
1389 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001390 for (j = 0; j < mod->stats_count; ++j) {
1391 chunk_appendf(out,
1392 "<tr><th>%s</th><td>%s</td></tr>",
1393 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1394 ++i;
1395 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001396 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001397 } else {
1398 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001399 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001400
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001401 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001402 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001403 }
1404
1405 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001406 }
1407 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1408 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001409 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001410 /* Column sub-heading for Enable or Disable server */
1411 chunk_appendf(out, "<td></td>");
1412 }
1413 chunk_appendf(out,
1414 "<td class=ac>"
1415 /* name */
1416 "%s<a name=\"%s/Backend\"></a>"
1417 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1418 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001419 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001420 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1421
Willy Tarreau708c4162019-10-09 10:19:16 +02001422 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001423 /* balancing */
1424 chunk_appendf(out, "<div class=tips>balancing: %s",
1425 field_str(stats, ST_F_ALGO));
1426
1427 /* cookie */
1428 if (stats[ST_F_COOKIE].type) {
1429 chunk_appendf(out, ", cookie: '");
1430 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1431 chunk_htmlencode(out, &src);
1432 chunk_appendf(out, "'");
1433 }
1434 chunk_appendf(out, "</div>");
1435 }
1436
1437 chunk_appendf(out,
1438 "%s</td>"
1439 /* queue : current, max */
1440 "<td>%s</td><td>%s</td><td></td>"
1441 /* sessions rate : current, max, limit */
1442 "<td>%s</td><td>%s</td><td></td>"
1443 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001444 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001445 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1446 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1447
1448 chunk_appendf(out,
1449 /* sessions: current, max, limit, total */
1450 "<td>%s</td><td>%s</td><td>%s</td>"
1451 "<td><u>%s<div class=tips><table class=det>"
1452 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1453 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001454 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 +01001455 U2H(stats[ST_F_STOT].u.u64),
1456 U2H(stats[ST_F_STOT].u.u64));
1457
1458 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1459 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1460 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001461 "<tr><th>New connections:</th><td>%s</td></tr>"
1462 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001463 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1464 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1465 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1466 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1467 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1468 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1469 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1470 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001471 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1472 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001473 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001474 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001475 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001476 U2H(stats[ST_F_CONNECT].u.u64),
1477 U2H(stats[ST_F_REUSE].u.u64),
1478 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1479 (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 +01001480 U2H(stats[ST_F_REQ_TOT].u.u64),
1481 U2H(stats[ST_F_HRSP_1XX].u.u64),
1482 U2H(stats[ST_F_HRSP_2XX].u.u64),
1483 U2H(stats[ST_F_COMP_RSP].u.u64),
1484 stats[ST_F_HRSP_2XX].u.u64 ?
1485 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1486 U2H(stats[ST_F_HRSP_3XX].u.u64),
1487 U2H(stats[ST_F_HRSP_4XX].u.u64),
1488 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001489 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001490 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1491 U2H(stats[ST_F_CACHE_HITS].u.u64),
1492 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1493 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001494 U2H(stats[ST_F_WREW].u.u64),
1495 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001496 }
1497
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001498 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1499 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1500 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1501 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1502 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001503 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001504 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1505 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1506 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1507 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001508
1509 chunk_appendf(out,
1510 "</table></div></u></td>"
1511 /* sessions: lbtot, last */
1512 "<td>%s</td><td>%s</td>"
1513 /* bytes: in */
1514 "<td>%s</td>"
1515 "",
1516 U2H(stats[ST_F_LBTOT].u.u64),
1517 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1518 U2H(stats[ST_F_BIN].u.u64));
1519
1520 chunk_appendf(out,
1521 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1522 "<td>%s%s<div class=tips><table class=det>"
1523 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1524 "<tr><th>Compression in:</th><td>%s</td></tr>"
1525 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1526 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1527 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1528 "</table></div>%s</td>",
1529 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1530 U2H(stats[ST_F_BOUT].u.u64),
1531 U2H(stats[ST_F_BOUT].u.u64),
1532 U2H(stats[ST_F_COMP_IN].u.u64),
1533 U2H(stats[ST_F_COMP_OUT].u.u64),
1534 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1535 U2H(stats[ST_F_COMP_BYP].u.u64),
1536 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1537 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,
1538 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1539
1540 chunk_appendf(out,
1541 /* denied: req, resp */
1542 "<td>%s</td><td>%s</td>"
1543 /* errors : request, connect */
1544 "<td></td><td>%s</td>"
1545 /* errors : response */
1546 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1547 /* warnings: retries, redispatches */
1548 "<td>%lld</td><td>%lld</td>"
1549 /* backend status: reflect backend status (up/down): we display UP
1550 * if the backend has known working servers or if it has no server at
1551 * all (eg: for stats). Then we display the total weight, number of
1552 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001553 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001554 "<td class=ac>%d</td><td class=ac>%d</td>"
1555 "",
1556 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1557 U2H(stats[ST_F_ECON].u.u64),
1558 U2H(stats[ST_F_ERESP].u.u64),
1559 (long long)stats[ST_F_CLI_ABRT].u.u64,
1560 (long long)stats[ST_F_SRV_ABRT].u.u64,
1561 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1562 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1563 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 +02001564 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001565 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1566
1567 chunk_appendf(out,
1568 /* rest of backend: nothing, down transitions, total downtime, throttle */
1569 "<td class=ac>&nbsp;</td><td>%d</td>"
1570 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001571 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001572 stats[ST_F_CHKDOWN].u.u32,
1573 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001574
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001575 if (flags & STAT_SHMODULES) {
1576 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001577 chunk_appendf(out, "<td>");
1578
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001579 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001580 chunk_appendf(out,
1581 "<u>%s<div class=tips><table class=det>",
1582 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001583 for (j = 0; j < mod->stats_count; ++j) {
1584 chunk_appendf(out,
1585 "<tr><th>%s</th><td>%s</td></tr>",
1586 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1587 ++i;
1588 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001589 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001590 } else {
1591 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001592 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001593
1594 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001595 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001596 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001597
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001598 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001599 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001600
William Lallemand74c24fb2016-11-21 17:18:36 +01001601 return 1;
1602}
1603
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001604int stats_dump_one_line(const struct field *stats, size_t stats_count,
1605 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001606{
Simon Horman05ee2132017-01-04 09:37:25 +01001607 int ret;
1608
William Lallemand74c24fb2016-11-21 17:18:36 +01001609 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001610 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001611 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001612 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001613 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001614 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001615 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001616 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001617
1618 if (ret)
1619 appctx->ctx.stats.flags |= STAT_STARTED;
1620
1621 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001622}
1623
1624/* Fill <stats> with the frontend statistics. <stats> is
1625 * preallocated array of length <len>. The length of the array
1626 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1627 * this value, the function returns 0, otherwise, it returns 1.
1628 */
1629int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1630{
1631 if (len < ST_F_TOTAL_FIELDS)
1632 return 0;
1633
William Lallemand74c24fb2016-11-21 17:18:36 +01001634 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1635 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1636 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1637 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1638 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1639 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1640 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1641 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1642 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1643 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1644 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1645 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1646 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1647 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
Willy Tarreauc3914d42020-09-24 08:39:22 +02001648 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->disabled ? "STOP" : "OPEN");
William Lallemand74c24fb2016-11-21 17:18:36 +01001649 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1650 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1651 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1652 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1653 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1654 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1655 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001656 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001657 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001658
1659 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1660 if (px->mode == PR_MODE_HTTP) {
1661 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1662 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1663 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1664 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1665 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1666 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1667 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001668 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1669 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001670 }
1671
1672 /* requests : req_rate, req_rate_max, req_tot, */
1673 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1674 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1675 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1676
1677 /* compression: in, out, bypassed, responses */
1678 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1679 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1680 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1681 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1682
1683 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1684 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1685 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1686 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1687
1688 return 1;
1689}
1690
1691/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1692 * the state from stream interface <si>. The caller is responsible for clearing
1693 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1694 */
1695static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1696{
1697 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001698 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1699 struct stats_module *mod;
1700 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001701
1702 if (!(px->cap & PR_CAP_FE))
1703 return 0;
1704
1705 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1706 return 0;
1707
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001708 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1709
William Lallemand74c24fb2016-11-21 17:18:36 +01001710 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1711 return 0;
1712
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001713 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1714 void *counters;
1715
1716 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1717 stats_count += mod->stats_count;
1718 continue;
1719 }
1720
1721 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1722 mod->fill_stats(counters, stats + stats_count);
1723 stats_count += mod->stats_count;
1724 }
1725
1726 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001727}
1728
1729/* Fill <stats> with the listener statistics. <stats> is
1730 * preallocated array of length <len>. The length of the array
1731 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1732 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001733 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001734 */
1735int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1736 struct field *stats, int len)
1737{
Willy Tarreau83061a82018-07-13 11:56:34 +02001738 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001739
1740 if (len < ST_F_TOTAL_FIELDS)
1741 return 0;
1742
1743 if (!l->counters)
1744 return 0;
1745
1746 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001747
1748 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1749 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1750 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1751 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1752 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1753 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1754 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1755 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1756 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1757 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1758 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1759 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1760 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1761 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001762 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (!l->maxconn || l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
William Lallemand74c24fb2016-11-21 17:18:36 +01001763 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1764 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1765 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1766 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001767 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001768 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001769
Willy Tarreau708c4162019-10-09 10:19:16 +02001770 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001771 char str[INET6_ADDRSTRLEN];
1772 int port;
1773
Willy Tarreau37159062020-08-27 07:48:42 +02001774 port = get_host_port(&l->rx.addr);
1775 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001776 case AF_INET:
1777 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1778 chunk_appendf(out, "%s:%d", str, port);
1779 break;
1780 case AF_INET6:
1781 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1782 chunk_appendf(out, "[%s]:%d", str, port);
1783 break;
1784 case AF_UNIX:
1785 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1786 break;
1787 case -1:
1788 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1789 chunk_strcat(out, strerror(errno));
1790 break;
1791 default: /* address family not supported */
1792 break;
1793 }
1794 }
1795
1796 return 1;
1797}
1798
1799/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001800 * from stream interface <si>. The caller is responsible for clearing the trash
1801 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001802 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001803static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001804{
1805 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001806 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1807 struct stats_module *mod;
1808 size_t stats_count = ST_F_TOTAL_FIELDS;
1809
1810 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001811
Willy Tarreau43241ff2019-10-09 11:27:51 +02001812 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001813 return 0;
1814
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001815 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1816 void *counters;
1817
1818 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1819 stats_count += mod->stats_count;
1820 continue;
1821 }
1822
1823 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
1824 mod->fill_stats(counters, stats + stats_count);
1825 stats_count += mod->stats_count;
1826 }
1827
1828 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001829}
1830
1831enum srv_stats_state {
1832 SRV_STATS_STATE_DOWN = 0,
1833 SRV_STATS_STATE_DOWN_AGENT,
1834 SRV_STATS_STATE_GOING_UP,
1835 SRV_STATS_STATE_UP_GOING_DOWN,
1836 SRV_STATS_STATE_UP,
1837 SRV_STATS_STATE_NOLB_GOING_DOWN,
1838 SRV_STATS_STATE_NOLB,
1839 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1840 SRV_STATS_STATE_DRAIN,
1841 SRV_STATS_STATE_DRAIN_AGENT,
1842 SRV_STATS_STATE_NO_CHECK,
1843
1844 SRV_STATS_STATE_COUNT, /* Must be last */
1845};
1846
1847static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1848 [SRV_STATS_STATE_DOWN] = "DOWN",
1849 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1850 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1851 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1852 [SRV_STATS_STATE_UP] = "UP",
1853 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1854 [SRV_STATS_STATE_NOLB] = "NOLB",
1855 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1856 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1857 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1858 [SRV_STATS_STATE_NO_CHECK] = "no check"
1859};
1860
1861/* Fill <stats> with the server statistics. <stats> is
1862 * preallocated array of length <len>. The length of the array
1863 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1864 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001865 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001866 */
1867int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1868 struct field *stats, int len)
1869{
1870 struct server *via, *ref;
1871 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001872 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001873 enum srv_stats_state state;
1874 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001875 long long srv_samples_counter;
1876 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001877
1878 if (len < ST_F_TOTAL_FIELDS)
1879 return 0;
1880
William Lallemand74c24fb2016-11-21 17:18:36 +01001881 /* we have "via" which is the tracked server as described in the configuration,
1882 * and "ref" which is the checked server and the end of the chain.
1883 */
1884 via = sv->track ? sv->track : sv;
1885 ref = via;
1886 while (ref->track)
1887 ref = ref->track;
1888
Emeric Brun52a91d32017-08-31 14:41:55 +02001889 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001890 if ((ref->check.state & CHK_ST_ENABLED) &&
1891 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1892 state = SRV_STATS_STATE_UP_GOING_DOWN;
1893 } else {
1894 state = SRV_STATS_STATE_UP;
1895 }
1896
Emeric Brun52a91d32017-08-31 14:41:55 +02001897 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001898 if (ref->agent.state & CHK_ST_ENABLED)
1899 state = SRV_STATS_STATE_DRAIN_AGENT;
1900 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1901 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1902 else
1903 state = SRV_STATS_STATE_DRAIN;
1904 }
1905
1906 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1907 state = SRV_STATS_STATE_NO_CHECK;
1908 }
1909 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001910 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001911 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1912 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1913 state = SRV_STATS_STATE_NOLB;
1914 } else {
1915 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1916 }
1917 }
1918 else { /* stopped */
1919 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1920 state = SRV_STATS_STATE_DOWN_AGENT;
1921 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1922 state = SRV_STATS_STATE_DOWN; /* DOWN */
1923 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1924 state = SRV_STATS_STATE_GOING_UP;
1925 } else {
1926 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1927 }
1928 }
1929
1930 chunk_reset(out);
1931
1932 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1933 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1934 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1935 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1936 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1937 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1938 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1939
1940 if (sv->maxconn)
1941 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1942
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001943 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1944 if (sv->max_idle_conns != -1)
1945 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1946
William Lallemand74c24fb2016-11-21 17:18:36 +01001947 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1948 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1949 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001950 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001951 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1952 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1953 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1954 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001955 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001956 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001957 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1958 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001959
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001960 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1961 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1962 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001963 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001964
William Lallemand74c24fb2016-11-21 17:18:36 +01001965 /* status */
1966 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001967 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001968 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001969 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001970 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001971 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001972 chunk_appendf(out, "MAINT");
1973 else
1974 chunk_appendf(out,
1975 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001976 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1977 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001978
1979 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1980 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001981 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
Willy Tarreaubd715102020-10-23 22:44:30 +02001982 stats[ST_F_UWEIGHT] = mkf_u32(FN_AVG, sv->uweight);
William Lallemand74c24fb2016-11-21 17:18:36 +01001983 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1984 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1985
1986 /* check failures: unique, fatal; last change, total downtime */
1987 if (sv->check.state & CHK_ST_ENABLED) {
1988 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1989 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1990 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1991 }
1992
1993 if (sv->maxqueue)
1994 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1995
1996 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1997 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1998 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1999
Emeric Brun52a91d32017-08-31 14:41:55 +02002000 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01002001 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
2002
2003 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2004
2005 if (sv->track) {
2006 char *fld_track = chunk_newstr(out);
2007
2008 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2009 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2010 }
2011
2012 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2013 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2014 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
2015
2016 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2017 const char *fld_chksts;
2018
2019 fld_chksts = chunk_newstr(out);
2020 chunk_strcat(out, "* "); // for check in progress
2021 chunk_strcat(out, get_check_status_info(sv->check.status));
2022 if (!(sv->check.state & CHK_ST_INPROGRESS))
2023 fld_chksts += 2; // skip "* "
2024 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
2025
2026 if (sv->check.status >= HCHK_STATUS_L57DATA)
2027 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
2028
2029 if (sv->check.status >= HCHK_STATUS_CHECKED)
2030 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
2031
2032 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2033 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
2034 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2035 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2036 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2037 }
2038
2039 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2040 const char *fld_chksts;
2041
2042 fld_chksts = chunk_newstr(out);
2043 chunk_strcat(out, "* "); // for check in progress
2044 chunk_strcat(out, get_check_status_info(sv->agent.status));
2045 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2046 fld_chksts += 2; // skip "* "
2047 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
2048
2049 if (sv->agent.status >= HCHK_STATUS_L57DATA)
2050 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
2051
2052 if (sv->agent.status >= HCHK_STATUS_CHECKED)
2053 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
2054
2055 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2056 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
2057 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2058 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2059 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2060 }
2061
2062 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2063 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02002064 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01002065 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2066 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2067 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2068 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2069 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2070 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2071 }
2072
2073 if (ref->observe)
2074 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2075
2076 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2077 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2078 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
2079
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002080 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2081 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2082 srv_samples_window = srv_samples_counter;
2083
2084 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2085 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2086 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2087 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01002088
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002089 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
2090 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
2091 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
2092 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
2093
Willy Tarreau708c4162019-10-09 10:19:16 +02002094 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002095 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2096 case AF_INET:
2097 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01002098 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01002099 break;
2100 case AF_INET6:
2101 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01002102 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01002103 break;
2104 case AF_UNIX:
2105 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2106 break;
2107 case -1:
2108 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2109 chunk_strcat(out, strerror(errno));
2110 break;
2111 default: /* address family not supported */
2112 break;
2113 }
2114
2115 if (sv->cookie)
2116 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2117 }
2118
2119 return 1;
2120}
2121
2122/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02002123 * from stream interface <si>, and server state <state>. The caller is
2124 * responsible for clearing the trash if needed. Returns non-zero if it emits
2125 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002126 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002127static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002128{
2129 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002130 struct stats_module *mod;
2131 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2132 size_t stats_count = ST_F_TOTAL_FIELDS;
2133
2134 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002135
Willy Tarreau43241ff2019-10-09 11:27:51 +02002136 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002137 return 0;
2138
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002139 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2140 void *counters;
2141
2142 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2143 continue;
2144
2145 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2146 stats_count += mod->stats_count;
2147 continue;
2148 }
2149
2150 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2151 mod->fill_stats(counters, stats + stats_count);
2152 stats_count += mod->stats_count;
2153 }
2154
2155 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002156}
2157
2158/* Fill <stats> with the backend statistics. <stats> is
2159 * preallocated array of length <len>. The length of the array
2160 * must be at least ST_F_TOTAL_FIELDS. If this length is less
2161 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02002162 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01002163 */
2164int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
2165{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002166 long long be_samples_counter;
2167 unsigned int be_samples_window = TIME_STATS_SAMPLES;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002168 struct buffer *out = get_trash_chunk();
2169 const struct server *srv;
2170 int nbup, nbsrv;
Willy Tarreaubd715102020-10-23 22:44:30 +02002171 int totuw;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002172 char *fld;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002173
William Lallemand74c24fb2016-11-21 17:18:36 +01002174 if (len < ST_F_TOTAL_FIELDS)
2175 return 0;
2176
Willy Tarreaubd715102020-10-23 22:44:30 +02002177 totuw = 0;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002178 nbup = nbsrv = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002179 for (srv = px->srv; srv; srv = srv->next) {
2180 if (srv->cur_state != SRV_ST_STOPPED) {
2181 nbup++;
2182 if (srv_currently_usable(srv) &&
2183 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
2184 totuw += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002185 }
Willy Tarreaubd715102020-10-23 22:44:30 +02002186 nbsrv++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002187 }
2188
Willy Tarreaubd715102020-10-23 22:44:30 +02002189 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2190 if (!px->srv_act && px->lbprm.fbck)
2191 totuw = px->lbprm.fbck->uweight;
2192 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2193
William Lallemand74c24fb2016-11-21 17:18:36 +01002194 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2195 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2196 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2197 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
2198 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01002199 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01002200 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
2201 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2202 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2203 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2204 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2205 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2206 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2207 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2208 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2209 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
2210 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002211 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01002212 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01002213 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
2214 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002215
2216 fld = chunk_newstr(out);
2217 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2218 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2219 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2220
2221 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld);
William Lallemand74c24fb2016-11-21 17:18:36 +01002222 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
Willy Tarreaubd715102020-10-23 22:44:30 +02002223 stats[ST_F_UWEIGHT] = mkf_u32(FN_AVG, totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002224 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
2225 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
2226 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
2227 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2228 if (px->srv)
2229 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
2230
2231 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
2232 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2233 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
2234 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2235 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2236 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2237 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
2238
Willy Tarreau708c4162019-10-09 10:19:16 +02002239 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002240 if (px->cookie_name)
2241 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2242 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2243 }
2244
2245 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2246 if (px->mode == PR_MODE_HTTP) {
2247 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2248 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2249 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2250 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2251 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2252 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2253 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01002254 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2255 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01002256 }
2257
2258 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2259 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2260
2261 /* compression: in, out, bypassed, responses */
2262 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2263 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2264 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2265 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2266 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
2267
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002268 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2269 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2270 be_samples_window = be_samples_counter;
2271
2272 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2273 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2274 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2275 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01002276
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002277 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2278 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2279 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2280 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2281
William Lallemand74c24fb2016-11-21 17:18:36 +01002282 return 1;
2283}
2284
2285/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002286 * interface <si>. The caller is responsible for clearing the trash if needed.
2287 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002288 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002289static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002290{
2291 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002292 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2293 struct stats_module *mod;
2294 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002295
2296 if (!(px->cap & PR_CAP_BE))
2297 return 0;
2298
2299 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2300 return 0;
2301
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002302 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2303
Willy Tarreau43241ff2019-10-09 11:27:51 +02002304 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002305 return 0;
2306
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002307 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2308 struct extra_counters *counters;
2309
2310 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2311 continue;
2312
2313 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2314 stats_count += mod->stats_count;
2315 continue;
2316 }
2317
2318 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2319 mod->fill_stats(counters, stats + stats_count);
2320 stats_count += mod->stats_count;
2321 }
2322
2323 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002324}
2325
2326/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2327 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2328 * for clearing the trash if needed.
2329 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002330static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002331{
2332 struct appctx *appctx = __objt_appctx(si->end);
2333 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002334 struct stats_module *mod;
2335 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002336
2337 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2338 /* A form to enable/disable this proxy servers */
2339
2340 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2341 scope_txt[0] = 0;
2342 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002343 const char *scope_ptr = stats_scope_ptr(appctx, si);
2344
William Lallemand74c24fb2016-11-21 17:18:36 +01002345 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002346 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002347 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2348 }
2349
2350 chunk_appendf(&trash,
2351 "<form method=\"post\">");
2352 }
2353
2354 /* print a new table */
2355 chunk_appendf(&trash,
2356 "<table class=\"tbl\" width=\"100%%\">\n"
2357 "<tr class=\"titre\">"
2358 "<th class=\"pxname\" width=\"10%%\">");
2359
2360 chunk_appendf(&trash,
2361 "<a name=\"%s\"></a>%s"
2362 "<a class=px href=\"#%s\">%s</a>",
2363 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002364 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002365 px->id, px->id);
2366
Willy Tarreau676c29e2019-10-09 10:50:01 +02002367 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002368 /* cap, mode, id */
2369 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2370 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2371 px->uuid);
2372 chunk_appendf(&trash, "</div>");
2373 }
2374
2375 chunk_appendf(&trash,
2376 "%s</th>"
2377 "<th class=\"%s\" width=\"90%%\">%s</th>"
2378 "</tr>\n"
2379 "</table>\n"
2380 "<table class=\"tbl\" width=\"100%%\">\n"
2381 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002382 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002383 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2384
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002385 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002386 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002387 if ((px->cap & PR_CAP_BE) && px->srv)
2388 chunk_appendf(&trash,
2389 "<th rowspan=2 width=1><input type=\"checkbox\" "
2390 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2391 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2392 px->id,
2393 px->id);
2394 else
2395 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002396 }
2397
2398 chunk_appendf(&trash,
2399 "<th rowspan=2></th>"
2400 "<th colspan=3>Queue</th>"
2401 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2402 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2403 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002404 "<th colspan=9>Server</th>");
2405
2406 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2407 // calculate the count of module for colspan attribute
2408 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2409 ++stats_module_len;
2410 }
2411 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2412 stats_module_len);
2413 }
2414
2415 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002416 "</tr>\n"
2417 "<tr class=\"titre\">"
2418 "<th>Cur</th><th>Max</th><th>Limit</th>"
2419 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2420 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2421 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2422 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2423 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2424 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002425 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002426
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002427 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2428 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2429 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2430 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002431 }
2432
2433 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002434}
2435
2436/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2437 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2438 */
2439static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2440{
2441 struct appctx *appctx = __objt_appctx(si->end);
2442 chunk_appendf(&trash, "</table>");
2443
2444 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2445 /* close the form used to enable/disable this proxy servers */
2446 chunk_appendf(&trash,
2447 "Choose the action to perform on the checked servers : "
2448 "<select name=action>"
2449 "<option value=\"\"></option>"
2450 "<option value=\"ready\">Set state to READY</option>"
2451 "<option value=\"drain\">Set state to DRAIN</option>"
2452 "<option value=\"maint\">Set state to MAINT</option>"
2453 "<option value=\"dhlth\">Health: disable checks</option>"
2454 "<option value=\"ehlth\">Health: enable checks</option>"
2455 "<option value=\"hrunn\">Health: force UP</option>"
2456 "<option value=\"hnolb\">Health: force NOLB</option>"
2457 "<option value=\"hdown\">Health: force DOWN</option>"
2458 "<option value=\"dagent\">Agent: disable checks</option>"
2459 "<option value=\"eagent\">Agent: enable checks</option>"
2460 "<option value=\"arunn\">Agent: force UP</option>"
2461 "<option value=\"adown\">Agent: force DOWN</option>"
2462 "<option value=\"shutdown\">Kill Sessions</option>"
2463 "</select>"
2464 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2465 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2466 "</form>",
2467 px->uuid);
2468 }
2469
2470 chunk_appendf(&trash, "<p>\n");
2471}
2472
2473/*
2474 * Dumps statistics for a proxy. The output is sent to the stream interface's
2475 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2476 * buffer space, or non-zero if everything completed. This function is used
2477 * both by the CLI and the HTTP entry points, and is able to dump the output
2478 * in HTML or CSV formats. If the later, <uri> must be NULL.
2479 */
Christopher Fauletef779222018-10-31 08:47:01 +01002480int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2481 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002482{
2483 struct appctx *appctx = __objt_appctx(si->end);
2484 struct stream *s = si_strm(si);
2485 struct channel *rep = si_ic(si);
2486 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2487 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002488
2489 chunk_reset(&trash);
2490
2491 switch (appctx->ctx.stats.px_st) {
2492 case STAT_PX_ST_INIT:
2493 /* we are on a new proxy */
2494 if (uri && uri->scope) {
2495 /* we have a limited scope, we have to check the proxy name */
2496 struct stat_scope *scope;
2497 int len;
2498
2499 len = strlen(px->id);
2500 scope = uri->scope;
2501
2502 while (scope) {
2503 /* match exact proxy name */
2504 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2505 break;
2506
2507 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002508 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01002509 break;
2510 scope = scope->next;
2511 }
2512
2513 /* proxy name not found : don't dump anything */
2514 if (scope == NULL)
2515 return 1;
2516 }
2517
2518 /* if the user has requested a limited output and the proxy
2519 * name does not match, skip it.
2520 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002521 if (appctx->ctx.stats.scope_len) {
2522 const char *scope_ptr = stats_scope_ptr(appctx, si);
2523
2524 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2525 return 1;
2526 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002527
2528 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2529 (appctx->ctx.stats.iid != -1) &&
2530 (px->uuid != appctx->ctx.stats.iid))
2531 return 1;
2532
2533 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2534 /* fall through */
2535
2536 case STAT_PX_ST_TH:
2537 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002538 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002539 if (!stats_putchk(rep, htx, &trash))
2540 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002541 }
2542
2543 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2544 /* fall through */
2545
2546 case STAT_PX_ST_FE:
2547 /* print the frontend */
2548 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002549 if (!stats_putchk(rep, htx, &trash))
2550 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002551 }
2552
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002553 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01002554 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2555 /* fall through */
2556
2557 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002558 /* obj2 points to listeners list as initialized above */
2559 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002560 if (htx) {
2561 if (htx_almost_full(htx))
2562 goto full;
2563 }
2564 else {
2565 if (buffer_almost_full(&rep->buf))
2566 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002567 }
2568
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002569 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01002570 if (!l->counters)
2571 continue;
2572
2573 if (appctx->ctx.stats.flags & STAT_BOUND) {
2574 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2575 break;
2576
2577 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2578 continue;
2579 }
2580
2581 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002582 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002583 if (!stats_putchk(rep, htx, &trash))
2584 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002585 }
2586 }
2587
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002588 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01002589 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2590 /* fall through */
2591
2592 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002593 /* obj2 points to servers list as initialized above */
2594 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002595 if (htx) {
2596 if (htx_almost_full(htx))
2597 goto full;
2598 }
2599 else {
2600 if (buffer_almost_full(&rep->buf))
2601 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002602 }
2603
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002604 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01002605
2606 if (appctx->ctx.stats.flags & STAT_BOUND) {
2607 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2608 break;
2609
2610 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2611 continue;
2612 }
2613
Willy Tarreau3e320362020-10-23 17:28:57 +02002614 /* do not report disabled servers */
2615 if (appctx->ctx.stats.flags & STAT_HIDE_MAINT &&
2616 sv->cur_admin & SRV_ADMF_MAINT) {
2617 continue;
2618 }
2619
William Lallemand74c24fb2016-11-21 17:18:36 +01002620 svs = sv;
2621 while (svs->track)
2622 svs = svs->track;
2623
2624 /* do not report servers which are DOWN and not changing state */
2625 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002626 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2627 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002628 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2629 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2630 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2631 continue;
2632 }
2633
Willy Tarreau43241ff2019-10-09 11:27:51 +02002634 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002635 if (!stats_putchk(rep, htx, &trash))
2636 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002637 }
2638 } /* for sv */
2639
2640 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2641 /* fall through */
2642
2643 case STAT_PX_ST_BE:
2644 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002645 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002646 if (!stats_putchk(rep, htx, &trash))
2647 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002648 }
2649
2650 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2651 /* fall through */
2652
2653 case STAT_PX_ST_END:
2654 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2655 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002656 if (!stats_putchk(rep, htx, &trash))
2657 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002658 }
2659
2660 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2661 /* fall through */
2662
2663 case STAT_PX_ST_FIN:
2664 return 1;
2665
2666 default:
2667 /* unknown state, we should put an abort() here ! */
2668 return 1;
2669 }
Christopher Fauletef779222018-10-31 08:47:01 +01002670
2671 full:
2672 si_rx_room_blk(si);
2673 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002674}
2675
2676/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2677 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2678 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002679static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002680{
2681 /* WARNING! This must fit in the first buffer !!! */
2682 chunk_appendf(&trash,
2683 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2684 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2685 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03002686 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002687 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2688 "<style type=\"text/css\"><!--\n"
2689 "body {"
2690 " font-family: arial, helvetica, sans-serif;"
2691 " font-size: 12px;"
2692 " font-weight: normal;"
2693 " color: black;"
2694 " background: white;"
2695 "}\n"
2696 "th,td {"
2697 " font-size: 10px;"
2698 "}\n"
2699 "h1 {"
2700 " font-size: x-large;"
2701 " margin-bottom: 0.5em;"
2702 "}\n"
2703 "h2 {"
2704 " font-family: helvetica, arial;"
2705 " font-size: x-large;"
2706 " font-weight: bold;"
2707 " font-style: italic;"
2708 " color: #6020a0;"
2709 " margin-top: 0em;"
2710 " margin-bottom: 0em;"
2711 "}\n"
2712 "h3 {"
2713 " font-family: helvetica, arial;"
2714 " font-size: 16px;"
2715 " font-weight: bold;"
2716 " color: #b00040;"
2717 " background: #e8e8d0;"
2718 " margin-top: 0em;"
2719 " margin-bottom: 0em;"
2720 "}\n"
2721 "li {"
2722 " margin-top: 0.25em;"
2723 " margin-right: 2em;"
2724 "}\n"
2725 ".hr {margin-top: 0.25em;"
2726 " border-color: black;"
2727 " border-bottom-style: solid;"
2728 "}\n"
2729 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2730 ".total {background: #20D0D0;color: #ffff80;}\n"
2731 ".frontend {background: #e8e8d0;}\n"
2732 ".socket {background: #d0d0d0;}\n"
2733 ".backend {background: #e8e8d0;}\n"
2734 ".active_down {background: #ff9090;}\n"
2735 ".active_going_up {background: #ffd020;}\n"
2736 ".active_going_down {background: #ffffa0;}\n"
2737 ".active_up {background: #c0ffc0;}\n"
2738 ".active_nolb {background: #20a0ff;}\n"
2739 ".active_draining {background: #20a0FF;}\n"
2740 ".active_no_check {background: #e0e0e0;}\n"
2741 ".backup_down {background: #ff9090;}\n"
2742 ".backup_going_up {background: #ff80ff;}\n"
2743 ".backup_going_down {background: #c060ff;}\n"
2744 ".backup_up {background: #b0d0ff;}\n"
2745 ".backup_nolb {background: #90b0e0;}\n"
2746 ".backup_draining {background: #cc9900;}\n"
2747 ".backup_no_check {background: #e0e0e0;}\n"
2748 ".maintain {background: #c07820;}\n"
2749 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2750 "\n"
2751 "a.px:link {color: #ffff40; text-decoration: none;}"
2752 "a.px:visited {color: #ffff40; text-decoration: none;}"
2753 "a.px:hover {color: #ffffff; text-decoration: none;}"
2754 "a.lfsb:link {color: #000000; text-decoration: none;}"
2755 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2756 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2757 "\n"
2758 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2759 "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"
2760 "table.tbl td.ac { text-align: center;}\n"
2761 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2762 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2763 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2764 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2765 "\n"
2766 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2767 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2768 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2769 "table.det { border-collapse: collapse; border-style: none; }\n"
2770 "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"
2771 "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"
2772 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2773 "div.tips {\n"
2774 " display:block;\n"
2775 " visibility:hidden;\n"
2776 " z-index:2147483647;\n"
2777 " position:absolute;\n"
2778 " padding:2px 4px 3px;\n"
2779 " background:#f0f060; color:#000000;\n"
2780 " border:1px solid #7040c0;\n"
2781 " white-space:nowrap;\n"
2782 " font-style:normal;font-size:11px;font-weight:normal;\n"
2783 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2784 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2785 "}\n"
2786 "u:hover div.tips {visibility:visible;}\n"
2787 "-->\n"
2788 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002789 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002790 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002791 );
2792}
2793
2794/* Dumps the HTML stats information block to the trash for and uses the state from
2795 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2796 * for clearing the trash if needed.
2797 */
2798static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2799{
2800 struct appctx *appctx = __objt_appctx(si->end);
2801 unsigned int up = (now.tv_sec - start_date.tv_sec);
2802 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002803 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002804 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2805
2806 /* Turn the bytes per second to bits per second and take care of the
2807 * usual ethernet overhead in order to help figure how far we are from
2808 * interface saturation since it's the only case which usually matters.
2809 * For this we count the total size of an Ethernet frame on the wire
2810 * including preamble and IFG (1538) for the largest TCP segment it
2811 * transports (1448 with TCP timestamps). This is not valid for smaller
2812 * packets (under-estimated), but it gives a reasonably accurate
2813 * estimation of how far we are from uplink saturation.
2814 */
2815 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002816
2817 /* WARNING! this has to fit the first packet too.
2818 * We are around 3.5 kB, add adding entries will
2819 * become tricky if we want to support 4kB buffers !
2820 */
2821 chunk_appendf(&trash,
2822 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2823 PRODUCT_NAME "%s</a></h1>\n"
2824 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2825 "<hr width=\"100%%\" class=\"hr\">\n"
2826 "<h3>&gt; General process information</h3>\n"
2827 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002828 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002829 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2830 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2831 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002832 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002833 "Running tasks: %d/%d; idle = %d %%<br>\n"
2834 "</td><td align=\"center\" nowrap>\n"
2835 "<table class=\"lgd\"><tr>\n"
2836 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2837 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2838 "</tr><tr>\n"
2839 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2840 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2841 "</tr><tr>\n"
2842 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2843 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2844 "</tr><tr>\n"
2845 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2846 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2847 "</tr><tr>\n"
2848 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2849 "</tr><tr>\n"
2850 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2851 "</tr></table>\n"
2852 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2853 "</td>"
2854 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2855 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2856 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002857 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2858 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2859 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2860 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2861 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002862 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002863 up / 86400, (up % 86400) / 3600,
2864 (up % 3600) / 60, (up % 60),
2865 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2866 global.rlimit_memmax ? " MB" : "",
2867 global.rlimit_nofile,
2868 global.maxsock, global.maxconn, global.maxpipes,
2869 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002870 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2871 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002872 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002873 );
2874
2875 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002876 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002877 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2878
2879 chunk_appendf(&trash,
2880 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2881 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2882 STAT_SCOPE_TXT_MAXLEN);
2883
2884 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2885 scope_txt[0] = 0;
2886 if (appctx->ctx.stats.scope_len) {
2887 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002888 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002889 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2890 }
2891
2892 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2893 chunk_appendf(&trash,
2894 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2895 uri->uri_prefix,
2896 "",
2897 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2898 scope_txt);
2899 else
2900 chunk_appendf(&trash,
2901 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2902 uri->uri_prefix,
2903 ";up",
2904 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2905 scope_txt);
2906
2907 if (uri->refresh > 0) {
2908 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2909 chunk_appendf(&trash,
2910 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2911 uri->uri_prefix,
2912 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2913 "",
2914 scope_txt);
2915 else
2916 chunk_appendf(&trash,
2917 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2918 uri->uri_prefix,
2919 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2920 ";norefresh",
2921 scope_txt);
2922 }
2923
2924 chunk_appendf(&trash,
2925 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2926 uri->uri_prefix,
2927 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2928 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2929 scope_txt);
2930
2931 chunk_appendf(&trash,
2932 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2933 uri->uri_prefix,
2934 (uri->refresh > 0) ? ";norefresh" : "",
2935 scope_txt);
2936
2937 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002938 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2939 uri->uri_prefix,
2940 (uri->refresh > 0) ? ";norefresh" : "",
2941 scope_txt, uri->uri_prefix);
2942
2943 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002944 "</ul></td>"
2945 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2946 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2947 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2948 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2949 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2950 "</ul>"
2951 "</td>"
2952 "</tr></table>\n"
2953 ""
2954 );
2955
2956 if (appctx->ctx.stats.st_code) {
2957 switch (appctx->ctx.stats.st_code) {
2958 case STAT_STATUS_DONE:
2959 chunk_appendf(&trash,
2960 "<p><div class=active_up>"
2961 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2962 "Action processed successfully."
2963 "</div>\n", uri->uri_prefix,
2964 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2965 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2966 scope_txt);
2967 break;
2968 case STAT_STATUS_NONE:
2969 chunk_appendf(&trash,
2970 "<p><div class=active_going_down>"
2971 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2972 "Nothing has changed."
2973 "</div>\n", uri->uri_prefix,
2974 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2975 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2976 scope_txt);
2977 break;
2978 case STAT_STATUS_PART:
2979 chunk_appendf(&trash,
2980 "<p><div class=active_going_down>"
2981 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2982 "Action partially processed.<br>"
2983 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2984 "</div>\n", uri->uri_prefix,
2985 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2986 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2987 scope_txt);
2988 break;
2989 case STAT_STATUS_ERRP:
2990 chunk_appendf(&trash,
2991 "<p><div class=active_down>"
2992 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2993 "Action not processed because of invalid parameters."
2994 "<ul>"
2995 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002996 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002997 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2998 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2999 "</ul>"
3000 "</div>\n", uri->uri_prefix,
3001 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3002 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3003 scope_txt);
3004 break;
3005 case STAT_STATUS_EXCD:
3006 chunk_appendf(&trash,
3007 "<p><div class=active_down>"
3008 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3009 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3010 "You should retry with less servers at a time.</b>"
3011 "</div>\n", uri->uri_prefix,
3012 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3013 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3014 scope_txt);
3015 break;
3016 case STAT_STATUS_DENY:
3017 chunk_appendf(&trash,
3018 "<p><div class=active_down>"
3019 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3020 "<b>Action denied.</b>"
3021 "</div>\n", uri->uri_prefix,
3022 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3023 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3024 scope_txt);
3025 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003026 case STAT_STATUS_IVAL:
3027 chunk_appendf(&trash,
3028 "<p><div class=active_down>"
3029 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3030 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3031 "</div>\n", uri->uri_prefix,
3032 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3033 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3034 scope_txt);
3035 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003036 default:
3037 chunk_appendf(&trash,
3038 "<p><div class=active_no_check>"
3039 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3040 "Unexpected result."
3041 "</div>\n", uri->uri_prefix,
3042 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3043 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3044 scope_txt);
3045 }
3046 chunk_appendf(&trash, "<p>\n");
3047 }
3048}
3049
3050/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3051 * for clearing the trash if needed.
3052 */
3053static void stats_dump_html_end()
3054{
3055 chunk_appendf(&trash, "</body></html>\n");
3056}
3057
Simon Horman05ee2132017-01-04 09:37:25 +01003058/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3059 * for clearing it if needed.
3060 */
3061static void stats_dump_json_header()
3062{
3063 chunk_strcat(&trash, "[");
3064}
3065
3066
3067/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3068 * for clearing the trash if needed.
3069 */
3070static void stats_dump_json_end()
3071{
3072 chunk_strcat(&trash, "]");
3073}
3074
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003075/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3076 * a pointer to the current server/listener.
3077 */
3078static int stats_dump_proxies(struct stream_interface *si,
3079 struct htx *htx,
3080 struct uri_auth *uri)
3081{
3082 struct appctx *appctx = __objt_appctx(si->end);
3083 struct channel *rep = si_ic(si);
3084 struct proxy *px;
3085
3086 /* dump proxies */
3087 while (appctx->ctx.stats.obj1) {
3088 if (htx) {
3089 if (htx_almost_full(htx))
3090 goto full;
3091 }
3092 else {
3093 if (buffer_almost_full(&rep->buf))
3094 goto full;
3095 }
3096
3097 px = appctx->ctx.stats.obj1;
3098 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02003099 if (!px->disabled && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003100 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
3101 return 0;
3102 }
3103
3104 appctx->ctx.stats.obj1 = px->next;
3105 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3106 }
3107
3108 return 1;
3109
3110 full:
3111 si_rx_room_blk(si);
3112 return 0;
3113}
3114
William Lallemand74c24fb2016-11-21 17:18:36 +01003115/* This function dumps statistics onto the stream interface's read buffer in
3116 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3117 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3118 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3119 * and the stream must be closed, or -1 in case of any error. This function is
3120 * used by both the CLI and the HTTP handlers.
3121 */
Christopher Fauletef779222018-10-31 08:47:01 +01003122static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
3123 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003124{
3125 struct appctx *appctx = __objt_appctx(si->end);
3126 struct channel *rep = si_ic(si);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003127 enum stats_domain domain = appctx->ctx.stats.domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003128
3129 chunk_reset(&trash);
3130
3131 switch (appctx->st2) {
3132 case STAT_ST_INIT:
3133 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3134 /* fall through */
3135
3136 case STAT_ST_HEAD:
3137 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003138 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003139 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3140 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003141 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003142 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003143 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003144 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003145
Christopher Fauletef779222018-10-31 08:47:01 +01003146 if (!stats_putchk(rep, htx, &trash))
3147 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003148
Christopher Faulet6338a082019-09-09 15:50:54 +02003149 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3150 appctx->st2 = STAT_ST_FIN;
3151 return 1;
3152 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003153 appctx->st2 = STAT_ST_INFO;
3154 /* fall through */
3155
3156 case STAT_ST_INFO:
3157 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3158 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003159 if (!stats_putchk(rep, htx, &trash))
3160 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003161 }
3162
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003163 if (domain == STATS_DOMAIN_PROXY)
3164 appctx->ctx.stats.obj1 = proxies_list;
3165
William Lallemand74c24fb2016-11-21 17:18:36 +01003166 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3167 appctx->st2 = STAT_ST_LIST;
3168 /* fall through */
3169
3170 case STAT_ST_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003171 switch (domain) {
3172 case STATS_DOMAIN_DNS:
3173 if (!stats_dump_dns(si, stat_l[domain],
3174 stat_count[domain],
3175 &stats_module_list[domain])) {
3176 return 0;
3177 }
3178 break;
3179
3180 case STATS_DOMAIN_PROXY:
3181 default:
3182 /* dump proxies */
3183 if (!stats_dump_proxies(si, htx, uri))
3184 return 0;
3185 break;
3186 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003187
3188 appctx->st2 = STAT_ST_END;
3189 /* fall through */
3190
3191 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003192 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3193 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3194 stats_dump_html_end();
3195 else
3196 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003197 if (!stats_putchk(rep, htx, &trash))
3198 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003199 }
3200
3201 appctx->st2 = STAT_ST_FIN;
3202 /* fall through */
3203
3204 case STAT_ST_FIN:
3205 return 1;
3206
3207 default:
3208 /* unknown state ! */
3209 appctx->st2 = STAT_ST_FIN;
3210 return -1;
3211 }
Christopher Fauletef779222018-10-31 08:47:01 +01003212
3213 full:
3214 si_rx_room_blk(si);
3215 return 0;
3216
William Lallemand74c24fb2016-11-21 17:18:36 +01003217}
3218
3219/* We reached the stats page through a POST request. The appctx is
3220 * expected to have already been allocated by the caller.
3221 * Parse the posted data and enable/disable servers if necessary.
3222 * Returns 1 if request was parsed or zero if it needs more data.
3223 */
3224static int stats_process_http_post(struct stream_interface *si)
3225{
3226 struct stream *s = si_strm(si);
3227 struct appctx *appctx = objt_appctx(si->end);
3228
3229 struct proxy *px = NULL;
3230 struct server *sv = NULL;
3231
3232 char key[LINESIZE];
3233 int action = ST_ADM_ACTION_NONE;
3234 int reprocess = 0;
3235
3236 int total_servers = 0;
3237 int altered_servers = 0;
3238
3239 char *first_param, *cur_param, *next_param, *end_params;
3240 char *st_cur_param = NULL;
3241 char *st_next_param = NULL;
3242
Christopher Fauleta3618372018-12-13 21:59:56 +01003243 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003244
Christopher Fauletb7f88902019-07-15 21:56:43 +02003245 struct htx *htx = htxbuf(&s->req.buf);
3246 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003247
Christopher Fauletb7f88902019-07-15 21:56:43 +02003248 /* we need more data */
3249 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3250 /* check if we can receive more */
3251 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3252 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3253 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003254 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003255 goto wait;
3256 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003257
Christopher Fauletb7f88902019-07-15 21:56:43 +02003258 /* The request was fully received. Copy data */
3259 blk = htx_get_head_blk(htx);
3260 while (blk) {
3261 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003262
Christopher Fauletb7f88902019-07-15 21:56:43 +02003263 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3264 break;
3265 if (type == HTX_BLK_DATA) {
3266 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003267
Christopher Fauletb7f88902019-07-15 21:56:43 +02003268 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003269 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3270 goto out;
3271 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003272 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003273 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003274 }
3275
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003276 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003277 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003278 cur_param = next_param = end_params;
3279 *end_params = '\0';
3280
3281 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3282
3283 /*
3284 * Parse the parameters in reverse order to only store the last value.
3285 * From the html form, the backend and the action are at the end.
3286 */
3287 while (cur_param > first_param) {
3288 char *value;
3289 int poffset, plen;
3290
3291 cur_param--;
3292
3293 if ((*cur_param == '&') || (cur_param == first_param)) {
3294 reprocess_servers:
3295 /* Parse the key */
3296 poffset = (cur_param != first_param ? 1 : 0);
3297 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3298 if ((plen > 0) && (plen <= sizeof(key))) {
3299 strncpy(key, cur_param + poffset, plen);
3300 key[plen - 1] = '\0';
3301 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003302 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003303 goto out;
3304 }
3305
3306 /* Parse the value */
3307 value = key;
3308 while (*value != '\0' && *value != '=') {
3309 value++;
3310 }
3311 if (*value == '=') {
3312 /* Ok, a value is found, we can mark the end of the key */
3313 *value++ = '\0';
3314 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003315 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003316 break;
3317
3318 /* Now we can check the key to see what to do */
3319 if (!px && (strcmp(key, "b") == 0)) {
3320 if ((px = proxy_be_by_name(value)) == NULL) {
3321 /* the backend name is unknown or ambiguous (duplicate names) */
3322 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3323 goto out;
3324 }
3325 }
3326 else if (!action && (strcmp(key, "action") == 0)) {
3327 if (strcmp(value, "ready") == 0) {
3328 action = ST_ADM_ACTION_READY;
3329 }
3330 else if (strcmp(value, "drain") == 0) {
3331 action = ST_ADM_ACTION_DRAIN;
3332 }
3333 else if (strcmp(value, "maint") == 0) {
3334 action = ST_ADM_ACTION_MAINT;
3335 }
3336 else if (strcmp(value, "shutdown") == 0) {
3337 action = ST_ADM_ACTION_SHUTDOWN;
3338 }
3339 else if (strcmp(value, "dhlth") == 0) {
3340 action = ST_ADM_ACTION_DHLTH;
3341 }
3342 else if (strcmp(value, "ehlth") == 0) {
3343 action = ST_ADM_ACTION_EHLTH;
3344 }
3345 else if (strcmp(value, "hrunn") == 0) {
3346 action = ST_ADM_ACTION_HRUNN;
3347 }
3348 else if (strcmp(value, "hnolb") == 0) {
3349 action = ST_ADM_ACTION_HNOLB;
3350 }
3351 else if (strcmp(value, "hdown") == 0) {
3352 action = ST_ADM_ACTION_HDOWN;
3353 }
3354 else if (strcmp(value, "dagent") == 0) {
3355 action = ST_ADM_ACTION_DAGENT;
3356 }
3357 else if (strcmp(value, "eagent") == 0) {
3358 action = ST_ADM_ACTION_EAGENT;
3359 }
3360 else if (strcmp(value, "arunn") == 0) {
3361 action = ST_ADM_ACTION_ARUNN;
3362 }
3363 else if (strcmp(value, "adown") == 0) {
3364 action = ST_ADM_ACTION_ADOWN;
3365 }
3366 /* else these are the old supported methods */
3367 else if (strcmp(value, "disable") == 0) {
3368 action = ST_ADM_ACTION_DISABLE;
3369 }
3370 else if (strcmp(value, "enable") == 0) {
3371 action = ST_ADM_ACTION_ENABLE;
3372 }
3373 else if (strcmp(value, "stop") == 0) {
3374 action = ST_ADM_ACTION_STOP;
3375 }
3376 else if (strcmp(value, "start") == 0) {
3377 action = ST_ADM_ACTION_START;
3378 }
3379 else {
3380 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3381 goto out;
3382 }
3383 }
3384 else if (strcmp(key, "s") == 0) {
3385 if (!(px && action)) {
3386 /*
3387 * Indicates that we'll need to reprocess the parameters
3388 * as soon as backend and action are known
3389 */
3390 if (!reprocess) {
3391 st_cur_param = cur_param;
3392 st_next_param = next_param;
3393 }
3394 reprocess = 1;
3395 }
3396 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003397 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003398 switch (action) {
3399 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003400 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003401 altered_servers++;
3402 total_servers++;
3403 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3404 }
3405 break;
3406 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003407 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003408 altered_servers++;
3409 total_servers++;
3410 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3411 }
3412 break;
3413 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003414 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003415 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3416 altered_servers++;
3417 total_servers++;
3418 }
3419 break;
3420 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003421 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003422 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3423 altered_servers++;
3424 total_servers++;
3425 }
3426 break;
3427 case ST_ADM_ACTION_DHLTH:
3428 if (sv->check.state & CHK_ST_CONFIGURED) {
3429 sv->check.state &= ~CHK_ST_ENABLED;
3430 altered_servers++;
3431 total_servers++;
3432 }
3433 break;
3434 case ST_ADM_ACTION_EHLTH:
3435 if (sv->check.state & CHK_ST_CONFIGURED) {
3436 sv->check.state |= CHK_ST_ENABLED;
3437 altered_servers++;
3438 total_servers++;
3439 }
3440 break;
3441 case ST_ADM_ACTION_HRUNN:
3442 if (!(sv->track)) {
3443 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003444 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003445 altered_servers++;
3446 total_servers++;
3447 }
3448 break;
3449 case ST_ADM_ACTION_HNOLB:
3450 if (!(sv->track)) {
3451 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003452 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003453 altered_servers++;
3454 total_servers++;
3455 }
3456 break;
3457 case ST_ADM_ACTION_HDOWN:
3458 if (!(sv->track)) {
3459 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003460 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003461 altered_servers++;
3462 total_servers++;
3463 }
3464 break;
3465 case ST_ADM_ACTION_DAGENT:
3466 if (sv->agent.state & CHK_ST_CONFIGURED) {
3467 sv->agent.state &= ~CHK_ST_ENABLED;
3468 altered_servers++;
3469 total_servers++;
3470 }
3471 break;
3472 case ST_ADM_ACTION_EAGENT:
3473 if (sv->agent.state & CHK_ST_CONFIGURED) {
3474 sv->agent.state |= CHK_ST_ENABLED;
3475 altered_servers++;
3476 total_servers++;
3477 }
3478 break;
3479 case ST_ADM_ACTION_ARUNN:
3480 if (sv->agent.state & CHK_ST_ENABLED) {
3481 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003482 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003483 altered_servers++;
3484 total_servers++;
3485 }
3486 break;
3487 case ST_ADM_ACTION_ADOWN:
3488 if (sv->agent.state & CHK_ST_ENABLED) {
3489 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003490 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003491 altered_servers++;
3492 total_servers++;
3493 }
3494 break;
3495 case ST_ADM_ACTION_READY:
3496 srv_adm_set_ready(sv);
3497 altered_servers++;
3498 total_servers++;
3499 break;
3500 case ST_ADM_ACTION_DRAIN:
3501 srv_adm_set_drain(sv);
3502 altered_servers++;
3503 total_servers++;
3504 break;
3505 case ST_ADM_ACTION_MAINT:
3506 srv_adm_set_maint(sv);
3507 altered_servers++;
3508 total_servers++;
3509 break;
3510 case ST_ADM_ACTION_SHUTDOWN:
Willy Tarreauc3914d42020-09-24 08:39:22 +02003511 if (!px->disabled) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003512 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003513 altered_servers++;
3514 total_servers++;
3515 }
3516 break;
3517 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003518 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003519 } else {
3520 /* the server name is unknown or ambiguous (duplicate names) */
3521 total_servers++;
3522 }
3523 }
3524 if (reprocess && px && action) {
3525 /* Now, we know the backend and the action chosen by the user.
3526 * We can safely restart from the first server parameter
3527 * to reprocess them
3528 */
3529 cur_param = st_cur_param;
3530 next_param = st_next_param;
3531 reprocess = 0;
3532 goto reprocess_servers;
3533 }
3534
3535 next_param = cur_param;
3536 }
3537 }
3538
3539 if (total_servers == 0) {
3540 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3541 }
3542 else if (altered_servers == 0) {
3543 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3544 }
3545 else if (altered_servers == total_servers) {
3546 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3547 }
3548 else {
3549 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3550 }
3551 out:
3552 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003553 wait:
3554 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3555 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003556}
3557
3558
Christopher Fauletb7f88902019-07-15 21:56:43 +02003559static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003560{
3561 struct stream *s = si_strm(si);
3562 struct uri_auth *uri = s->be->uri_auth;
3563 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003564 struct htx_sl *sl;
3565 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003566
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003567 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);
3568 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3569 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003570 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003571 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003572
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003573 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003574 goto full;
3575 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3576 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3577 goto full;
3578 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003579 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3580 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3581 goto full;
3582 }
Christopher Fauletef779222018-10-31 08:47:01 +01003583 else {
3584 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3585 goto full;
3586 }
3587
3588 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3589 const char *refresh = U2A(uri->refresh);
3590 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3591 goto full;
3592 }
3593
3594 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3595 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3596 goto full;
3597 }
3598
3599 if (!htx_add_endof(htx, HTX_BLK_EOH))
3600 goto full;
3601
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003602 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003603 return 1;
3604
3605 full:
3606 htx_reset(htx);
3607 si_rx_room_blk(si);
3608 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003609}
3610
Christopher Fauletef779222018-10-31 08:47:01 +01003611
Christopher Fauletb7f88902019-07-15 21:56:43 +02003612static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003613{
3614 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3615 struct stream *s = si_strm(si);
3616 struct uri_auth *uri = s->be->uri_auth;
3617 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003618 struct htx_sl *sl;
3619 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003620
3621 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3622 scope_txt[0] = 0;
3623 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003624 const char *scope_ptr = stats_scope_ptr(appctx, si);
3625
Christopher Fauletef779222018-10-31 08:47:01 +01003626 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003627 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003628 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3629 }
3630
3631 /* We don't want to land on the posted stats page because a refresh will
3632 * repost the data. We don't want this to happen on accident so we redirect
3633 * the browse to the stats page with a GET.
3634 */
3635 chunk_printf(&trash, "%s;st=%s%s%s%s",
3636 uri->uri_prefix,
3637 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3638 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3639 stat_status_codes[appctx->ctx.stats.st_code]) ?
3640 stat_status_codes[appctx->ctx.stats.st_code] :
3641 stat_status_codes[STAT_STATUS_UNKN],
3642 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3643 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3644 scope_txt);
3645
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003646 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3647 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3648 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003649 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003650 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003651
3652 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003653 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3654 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3655 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3656 goto full;
3657
3658 if (!htx_add_endof(htx, HTX_BLK_EOH))
3659 goto full;
3660
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003661 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003662 return 1;
3663
3664full:
3665 htx_reset(htx);
3666 si_rx_room_blk(si);
3667 return 0;
3668}
William Lallemand74c24fb2016-11-21 17:18:36 +01003669
Simon Horman05ee2132017-01-04 09:37:25 +01003670
William Lallemand74c24fb2016-11-21 17:18:36 +01003671/* This I/O handler runs as an applet embedded in a stream interface. It is
3672 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3673 * appctx->st0 contains the operation in progress (dump, done). The handler
3674 * automatically unregisters itself once transfer is complete.
3675 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003676static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003677{
3678 struct stream_interface *si = appctx->owner;
3679 struct stream *s = si_strm(si);
3680 struct channel *req = si_oc(si);
3681 struct channel *res = si_ic(si);
3682 struct htx *req_htx, *res_htx;
3683
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003684 /* only proxy stats are available via http */
3685 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3686
Christopher Fauletef779222018-10-31 08:47:01 +01003687 res_htx = htx_from_buf(&res->buf);
3688
3689 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3690 goto out;
3691
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003692 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003693 if (!b_size(&res->buf)) {
3694 si_rx_room_blk(si);
3695 goto out;
3696 }
3697
3698 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003699 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3700 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003701
3702 /* all states are processed in sequence */
3703 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003704 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003705 if (s->txn->meth == HTTP_METH_HEAD)
3706 appctx->st0 = STAT_HTTP_DONE;
3707 else
3708 appctx->st0 = STAT_HTTP_DUMP;
3709 }
3710 }
3711
3712 if (appctx->st0 == STAT_HTTP_DUMP) {
3713 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3714 appctx->st0 = STAT_HTTP_DONE;
3715 }
3716
3717 if (appctx->st0 == STAT_HTTP_POST) {
3718 if (stats_process_http_post(si))
3719 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003720 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003721 appctx->st0 = STAT_HTTP_DONE;
3722 }
3723
3724 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003725 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003726 appctx->st0 = STAT_HTTP_DONE;
3727 }
3728
3729 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003730 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003731 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003732 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3733 si_rx_room_blk(si);
3734 goto out;
3735 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003736 channel_add_input(&s->res, 1);
3737 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003738 }
3739
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003740 if (appctx->st0 == STAT_HTTP_END) {
3741 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003742 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003743 si_shutr(si);
3744 }
3745
3746 /* eat the whole request */
3747 if (co_data(req)) {
3748 req_htx = htx_from_buf(&req->buf);
3749 co_htx_skip(req, req_htx, co_data(req));
3750 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003751 }
3752 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003753
Christopher Fauletef779222018-10-31 08:47:01 +01003754 out:
3755 /* we have left the request in the buffer for the case where we
3756 * process a POST, and this automatically re-enables activity on
3757 * read. It's better to indicate that we want to stop reading when
3758 * we're sending, so that we know there's at most one direction
3759 * deciding to wake the applet up. It saves it from looping when
3760 * emitting large blocks into small TCP windows.
3761 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003762 htx_to_buf(res_htx, &res->buf);
3763 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003764 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003765}
3766
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003767/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003768static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003769 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003770{
3771 int field;
3772
3773 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3774 if (!field_format(info, field))
3775 continue;
3776
Willy Tarreaueaa55372019-10-09 07:39:11 +02003777 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003778 return 0;
3779 if (!stats_emit_raw_data_field(out, &info[field]))
3780 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003781 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3782 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003783 if (!chunk_strcat(out, "\n"))
3784 return 0;
3785 }
3786 return 1;
3787}
3788
3789/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003790static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003791 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003792{
3793 int field;
3794
3795 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3796 if (!field_format(info, field))
3797 continue;
3798
Willy Tarreaueaa55372019-10-09 07:39:11 +02003799 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 +01003800 return 0;
3801 if (!stats_emit_field_tags(out, &info[field], ':'))
3802 return 0;
3803 if (!stats_emit_typed_data_field(out, &info[field]))
3804 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003805 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3806 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003807 if (!chunk_strcat(out, "\n"))
3808 return 0;
3809 }
3810 return 1;
3811}
3812
3813/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003814 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003815 * INF_TOTAL_FIELDS. If this length is less then this value, the
3816 * function returns 0, otherwise, it returns 1.
3817 */
3818int stats_fill_info(struct field *info, int len)
3819{
3820 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003821 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003822
3823#ifdef USE_OPENSSL
3824 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3825 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3826 int ssl_reuse = 0;
3827
3828 if (ssl_key_rate < ssl_sess_rate) {
3829 /* count the ssl reuse ratio and avoid overflows in both directions */
3830 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3831 }
3832#endif
3833
3834 if (len < INF_TOTAL_FIELDS)
3835 return 0;
3836
3837 chunk_reset(out);
3838 memset(info, 0, sizeof(*info) * len);
3839
3840 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003841 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01003842 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003843 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003844
Yves Lafon95317282018-02-26 11:10:37 +01003845 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003846 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3847 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3848 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3849
3850 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3851 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3852
3853 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3854 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01003855 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003856 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01003857 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003858 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01003859 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003860 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3861 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3862 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3863 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3864 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3865 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3866 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3867 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3868#ifdef USE_OPENSSL
3869 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3870 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3871 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3872#endif
3873 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3874 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3875 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3876 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3877 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3878 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3879 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3880 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3881 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3882
3883#ifdef USE_OPENSSL
3884 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3885 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3886 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3887 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3888 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3889 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3890 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3891 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3892 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3893 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3894#endif
3895 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3896 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3897 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3898#ifdef USE_ZLIB
3899 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3900 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3901#endif
3902 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003903 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003904 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003905 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3906 if (global.desc)
3907 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003908 info[INF_STOPPING] = mkf_u32(0, stopping);
3909 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003910 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003911 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003912 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003913 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003914 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003915 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003916 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003917 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003918 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003919 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 +02003920 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003921 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003922 return 1;
3923}
3924
3925/* This function dumps information onto the stream interface's read buffer.
3926 * It returns 0 as long as it does not complete, non-zero upon completion.
3927 * No state is used.
3928 */
3929static int stats_dump_info_to_buffer(struct stream_interface *si)
3930{
3931 struct appctx *appctx = __objt_appctx(si->end);
3932
3933 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3934 return 0;
3935
3936 chunk_reset(&trash);
3937
3938 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003939 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003940 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003941 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003942 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003943 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003944
Willy Tarreau06d80a92017-10-19 14:32:15 +02003945 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003946 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003947 return 0;
3948 }
3949
3950 return 1;
3951}
3952
Simon Horman6f6bb382017-01-04 09:37:26 +01003953/* This function dumps the schema onto the stream interface's read buffer.
3954 * It returns 0 as long as it does not complete, non-zero upon completion.
3955 * No state is used.
3956 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003957 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01003958 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3959 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003960static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003961{
3962
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003963 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003964
3965 chunk_strcat(out,
3966 "{"
3967 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3968 "\"oneOf\":["
3969 "{"
3970 "\"title\":\"Info\","
3971 "\"type\":\"array\","
3972 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003973 "\"title\":\"InfoItem\","
3974 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003975 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003976 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3977 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3978 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3979 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3980 "},"
3981 "\"required\":[\"field\",\"processNum\",\"tags\","
3982 "\"value\"]"
3983 "}"
3984 "},"
3985 "{"
3986 "\"title\":\"Stat\","
3987 "\"type\":\"array\","
3988 "\"items\":{"
3989 "\"title\":\"InfoItem\","
3990 "\"type\":\"object\","
3991 "\"properties\":{"
3992 "\"objType\":{"
3993 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3994 "\"Server\",\"Unknown\"]"
3995 "},"
3996 "\"proxyId\":{"
3997 "\"type\":\"integer\","
3998 "\"minimum\":0"
3999 "},"
4000 "\"id\":{"
4001 "\"type\":\"integer\","
4002 "\"minimum\":0"
4003 "},"
4004 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4005 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4006 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4007 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4008 "},"
4009 "\"required\":[\"objType\",\"proxyId\",\"id\","
4010 "\"field\",\"processNum\",\"tags\","
4011 "\"value\"]"
4012 "}"
4013 "},"
4014 "{"
4015 "\"title\":\"Error\","
4016 "\"type\":\"object\","
4017 "\"properties\":{"
4018 "\"errorStr\":{"
4019 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004020 "}"
4021 "},"
4022 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004023 "}"
4024 "],"
4025 "\"definitions\":{"
4026 "\"field\":{"
4027 "\"type\":\"object\","
4028 "\"pos\":{"
4029 "\"type\":\"integer\","
4030 "\"minimum\":0"
4031 "},"
4032 "\"name\":{"
4033 "\"type\":\"string\""
4034 "},"
4035 "\"required\":[\"pos\",\"name\"]"
4036 "},"
4037 "\"processNum\":{"
4038 "\"type\":\"integer\","
4039 "\"minimum\":1"
4040 "},"
4041 "\"tags\":{"
4042 "\"type\":\"object\","
4043 "\"origin\":{"
4044 "\"type\":\"string\","
4045 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4046 "\"Config\",\"Product\",\"Unknown\"]"
4047 "},"
4048 "\"nature\":{"
4049 "\"type\":\"string\","
4050 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4051 "\"Rate\",\"Counter\",\"Duration\","
4052 "\"Age\",\"Time\",\"Name\",\"Output\","
4053 "\"Avg\", \"Unknown\"]"
4054 "},"
4055 "\"scope\":{"
4056 "\"type\":\"string\","
4057 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4058 "\"System\",\"Unknown\"]"
4059 "},"
4060 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4061 "},"
4062 "\"typedValue\":{"
4063 "\"type\":\"object\","
4064 "\"oneOf\":["
4065 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4066 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4067 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4068 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4069 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4070 "],"
4071 "\"definitions\":{"
4072 "\"s32Value\":{"
4073 "\"properties\":{"
4074 "\"type\":{"
4075 "\"type\":\"string\","
4076 "\"enum\":[\"s32\"]"
4077 "},"
4078 "\"value\":{"
4079 "\"type\":\"integer\","
4080 "\"minimum\":-2147483648,"
4081 "\"maximum\":2147483647"
4082 "}"
4083 "},"
4084 "\"required\":[\"type\",\"value\"]"
4085 "},"
4086 "\"s64Value\":{"
4087 "\"properties\":{"
4088 "\"type\":{"
4089 "\"type\":\"string\","
4090 "\"enum\":[\"s64\"]"
4091 "},"
4092 "\"value\":{"
4093 "\"type\":\"integer\","
4094 "\"minimum\":-9007199254740991,"
4095 "\"maximum\":9007199254740991"
4096 "}"
4097 "},"
4098 "\"required\":[\"type\",\"value\"]"
4099 "},"
4100 "\"u32Value\":{"
4101 "\"properties\":{"
4102 "\"type\":{"
4103 "\"type\":\"string\","
4104 "\"enum\":[\"u32\"]"
4105 "},"
4106 "\"value\":{"
4107 "\"type\":\"integer\","
4108 "\"minimum\":0,"
4109 "\"maximum\":4294967295"
4110 "}"
4111 "},"
4112 "\"required\":[\"type\",\"value\"]"
4113 "},"
4114 "\"u64Value\":{"
4115 "\"properties\":{"
4116 "\"type\":{"
4117 "\"type\":\"string\","
4118 "\"enum\":[\"u64\"]"
4119 "},"
4120 "\"value\":{"
4121 "\"type\":\"integer\","
4122 "\"minimum\":0,"
4123 "\"maximum\":9007199254740991"
4124 "}"
4125 "},"
4126 "\"required\":[\"type\",\"value\"]"
4127 "},"
4128 "\"strValue\":{"
4129 "\"properties\":{"
4130 "\"type\":{"
4131 "\"type\":\"string\","
4132 "\"enum\":[\"str\"]"
4133 "},"
4134 "\"value\":{\"type\":\"string\"}"
4135 "},"
4136 "\"required\":[\"type\",\"value\"]"
4137 "},"
4138 "\"unknownValue\":{"
4139 "\"properties\":{"
4140 "\"type\":{"
4141 "\"type\":\"integer\","
4142 "\"minimum\":0"
4143 "},"
4144 "\"value\":{"
4145 "\"type\":\"string\","
4146 "\"enum\":[\"unknown\"]"
4147 "}"
4148 "},"
4149 "\"required\":[\"type\",\"value\"]"
4150 "}"
4151 "}"
4152 "}"
4153 "}"
4154 "}");
4155
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004156 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004157 chunk_reset(out);
4158 chunk_appendf(out,
4159 "{\"errorStr\":\"output buffer too short\"}");
4160 }
4161}
4162
4163/* This function dumps the schema onto the stream interface's read buffer.
4164 * It returns 0 as long as it does not complete, non-zero upon completion.
4165 * No state is used.
4166 */
4167static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
4168{
4169 chunk_reset(&trash);
4170
4171 stats_dump_json_schema(&trash);
4172
Willy Tarreau06d80a92017-10-19 14:32:15 +02004173 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004174 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01004175 return 0;
4176 }
4177
4178 return 1;
4179}
4180
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004181static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004182{
4183 struct proxy *px;
4184 struct server *sv;
4185 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004186 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004187 int clrall = 0;
4188
4189 if (strcmp(args[2], "all") == 0)
4190 clrall = 1;
4191
4192 /* check permissions */
4193 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4194 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4195 return 1;
4196
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004197 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004198 if (clrall) {
4199 memset(&px->be_counters, 0, sizeof(px->be_counters));
4200 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4201 }
4202 else {
4203 px->be_counters.conn_max = 0;
4204 px->be_counters.p.http.rps_max = 0;
4205 px->be_counters.sps_max = 0;
4206 px->be_counters.cps_max = 0;
4207 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004208 px->be_counters.qtime_max = 0;
4209 px->be_counters.ctime_max = 0;
4210 px->be_counters.dtime_max = 0;
4211 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004212
4213 px->fe_counters.conn_max = 0;
4214 px->fe_counters.p.http.rps_max = 0;
4215 px->fe_counters.sps_max = 0;
4216 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004217 }
4218
4219 for (sv = px->srv; sv; sv = sv->next)
4220 if (clrall)
4221 memset(&sv->counters, 0, sizeof(sv->counters));
4222 else {
4223 sv->counters.cur_sess_max = 0;
4224 sv->counters.nbpend_max = 0;
4225 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004226 sv->counters.qtime_max = 0;
4227 sv->counters.ctime_max = 0;
4228 sv->counters.dtime_max = 0;
4229 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004230 }
4231
4232 list_for_each_entry(li, &px->conf.listeners, by_fe)
4233 if (li->counters) {
4234 if (clrall)
4235 memset(li->counters, 0, sizeof(*li->counters));
4236 else
4237 li->counters->conn_max = 0;
4238 }
4239 }
4240
4241 global.cps_max = 0;
4242 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004243 global.ssl_max = 0;
4244 global.ssl_fe_keys_max = 0;
4245 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004246
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004247 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4248 if (!mod->clearable && !clrall)
4249 continue;
4250
4251 for (px = proxies_list; px; px = px->next) {
4252 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4253
4254 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4255 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4256 mod,
4257 mod->counters,
4258 mod->counters_size);
4259 }
4260
4261 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4262 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4263 mod,
4264 mod->counters,
4265 mod->counters_size);
4266 }
4267
4268 if (mod_cap & STATS_PX_CAP_SRV) {
4269 for (sv = px->srv; sv; sv = sv->next) {
4270 EXTRA_COUNTERS_INIT(sv->extra_counters,
4271 mod,
4272 mod->counters,
4273 mod->counters_size);
4274 }
4275 }
4276
4277 if (mod_cap & STATS_PX_CAP_LI) {
4278 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4279 EXTRA_COUNTERS_INIT(li->extra_counters,
4280 mod,
4281 mod->counters,
4282 mod->counters_size);
4283 }
4284 }
4285 }
4286 }
4287
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004288 dns_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_DNS]);
4289
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004290 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004291 return 1;
4292}
4293
4294
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004295static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004296{
Willy Tarreau2f397382019-10-09 11:43:59 +02004297 int arg = 2;
4298
Willy Tarreaud25fc792016-12-16 12:33:47 +01004299 appctx->ctx.stats.scope_str = 0;
4300 appctx->ctx.stats.scope_len = 0;
4301 appctx->ctx.stats.flags = 0;
4302
Willy Tarreau2f397382019-10-09 11:43:59 +02004303 while (*args[arg]) {
4304 if (strcmp(args[arg], "typed") == 0)
4305 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4306 else if (strcmp(args[arg], "json") == 0)
4307 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4308 else if (strcmp(args[arg], "desc") == 0)
4309 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4310 arg++;
4311 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004312 return 0;
4313}
4314
4315
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004316static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004317{
Willy Tarreau2f397382019-10-09 11:43:59 +02004318 int arg = 2;
4319
Willy Tarreaud25fc792016-12-16 12:33:47 +01004320 appctx->ctx.stats.scope_str = 0;
4321 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004322 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4323
4324 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
4325 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004326
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004327 /* proxy is the default domain */
4328 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004329 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004330 ++args;
4331
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004332 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004333 ++args;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004334 } else if (strcmp(args[arg], "dns") == 0) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004335 appctx->ctx.stats.domain = STATS_DOMAIN_DNS;
4336 ++args;
4337 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004338 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004339 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004340 }
4341
4342 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4343 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004344 struct proxy *px;
4345
Willy Tarreau2f397382019-10-09 11:43:59 +02004346 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004347 if (px)
4348 appctx->ctx.stats.iid = px->uuid;
4349 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004350 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004351
Willy Tarreau9d008692019-08-09 11:21:01 +02004352 if (!appctx->ctx.stats.iid)
4353 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004354
Willy Tarreau2b812e22016-11-22 16:18:05 +01004355 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004356 appctx->ctx.stats.type = atoi(args[arg+1]);
4357 appctx->ctx.stats.sid = atoi(args[arg+2]);
4358 arg += 3;
4359 }
4360
4361 while (*args[arg]) {
4362 if (strcmp(args[arg], "typed") == 0)
4363 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4364 else if (strcmp(args[arg], "json") == 0)
4365 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4366 else if (strcmp(args[arg], "desc") == 0)
4367 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02004368 else if (strcmp(args[arg], "no-maint") == 0)
4369 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02004370 else if (strcmp(args[arg], "up") == 0)
4371 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02004372 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004373 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004374
Willy Tarreau2b812e22016-11-22 16:18:05 +01004375 return 0;
4376}
4377
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004378static int cli_io_handler_dump_info(struct appctx *appctx)
4379{
4380 return stats_dump_info_to_buffer(appctx->owner);
4381}
4382
Willy Tarreau2b812e22016-11-22 16:18:05 +01004383/* This I/O handler runs as an applet embedded in a stream interface. It is
4384 * used to send raw stats over a socket.
4385 */
4386static int cli_io_handler_dump_stat(struct appctx *appctx)
4387{
Christopher Fauletef779222018-10-31 08:47:01 +01004388 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004389}
4390
Simon Horman6f6bb382017-01-04 09:37:26 +01004391static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4392{
4393 return stats_dump_json_schema_to_buffer(appctx->owner);
4394}
4395
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004396static int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
4397 int type, int px_cap)
4398{
4399 struct stats_module *mod;
4400
4401 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
4402
4403 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4404 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4405 continue;
4406
4407 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
4408 }
4409
4410 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
4411
4412 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4413 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4414 continue;
4415
4416 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
4417 }
4418
4419 return 1;
4420
4421 alloc_failed:
4422 return 0;
4423}
4424
4425/* Initialize and allocate all extra counters for a proxy and its attached
4426 * servers/listeners with all already registered stats module
4427 */
4428int stats_allocate_proxy_counters(struct proxy *px)
4429{
4430 struct server *sv;
4431 struct listener *li;
4432
4433 if (px->cap & PR_CAP_FE) {
4434 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
4435 COUNTERS_FE,
4436 STATS_PX_CAP_FE)) {
4437 return 0;
4438 }
4439 }
4440
4441 if (px->cap & PR_CAP_BE) {
4442 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
4443 COUNTERS_BE,
4444 STATS_PX_CAP_BE)) {
4445 return 0;
4446 }
4447 }
4448
4449 for (sv = px->srv; sv; sv = sv->next) {
4450 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
4451 COUNTERS_SV,
4452 STATS_PX_CAP_SRV)) {
4453 return 0;
4454 }
4455 }
4456
4457 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4458 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
4459 COUNTERS_LI,
4460 STATS_PX_CAP_LI)) {
4461 return 0;
4462 }
4463 }
4464
4465 return 1;
4466}
4467
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004468void stats_register_module(struct stats_module *m)
4469{
4470 const uint8_t domain = stats_get_domain(m->domain_flags);
4471
4472 LIST_ADDQ(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004473 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004474}
4475
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004476static int allocate_stats_px_postcheck(void)
4477{
4478 struct stats_module *mod;
4479 size_t i = ST_F_TOTAL_FIELDS;
4480 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004481 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004482
4483 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
4484
4485 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
4486 if (!stat_f[STATS_DOMAIN_PROXY]) {
4487 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
4488 err_code |= ERR_ALERT | ERR_FATAL;
4489 return err_code;
4490 }
4491
4492 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
4493 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
4494
4495 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4496 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
4497 mod->stats,
4498 mod->stats_count * sizeof(struct name_desc));
4499 i += mod->stats_count;
4500 }
4501
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004502 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004503 if (!stats_allocate_proxy_counters(px)) {
4504 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
4505 err_code |= ERR_ALERT | ERR_FATAL;
4506 return err_code;
4507 }
4508 }
4509
Christopher Fauletde79cd22021-01-06 07:41:56 +01004510 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004511
4512 return err_code;
4513}
4514
4515REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
4516
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004517static int allocate_stats_dns_postcheck(void)
4518{
4519 struct stats_module *mod;
4520 size_t i = 0;
4521 int err_code = 0;
4522
4523 stat_f[STATS_DOMAIN_DNS] = malloc(stat_count[STATS_DOMAIN_DNS] * sizeof(struct name_desc));
4524 if (!stat_f[STATS_DOMAIN_DNS]) {
4525 ha_alert("stats: cannot allocate all fields for dns statistics\n");
4526 err_code |= ERR_ALERT | ERR_FATAL;
4527 return err_code;
4528 }
4529
4530 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_DNS], list) {
4531 memcpy(stat_f[STATS_DOMAIN_DNS] + i,
4532 mod->stats,
4533 mod->stats_count * sizeof(struct name_desc));
4534 i += mod->stats_count;
4535 }
4536
4537 if (!dns_allocate_counters(&stats_module_list[STATS_DOMAIN_DNS])) {
4538 ha_alert("stats: cannot allocate all counters for dns statistics\n");
4539 err_code |= ERR_ALERT | ERR_FATAL;
4540 return err_code;
4541 }
4542
Christopher Fauletde79cd22021-01-06 07:41:56 +01004543 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004544
4545 return err_code;
4546}
4547
4548REGISTER_CONFIG_POSTPARSER("allocate-stats-dns", allocate_stats_dns_postcheck);
4549
Christopher Fauletde79cd22021-01-06 07:41:56 +01004550static int allocate_stat_lines_per_thread(void)
4551{
4552 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
4553
4554 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
4555 const int domain = domains[i];
4556
4557 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
4558 if (!stat_l[domain])
4559 return 0;
4560 }
4561 return 1;
4562}
4563
4564REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
4565
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01004566static int allocate_trash_counters(void)
4567{
4568 struct stats_module *mod;
4569 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
4570 size_t max_counters_size = 0;
4571
4572 /* calculate the greatest counters used by any stats modules */
4573 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
4574 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
4575 max_counters_size = mod->counters_size > max_counters_size ?
4576 mod->counters_size : max_counters_size;
4577 }
4578 }
4579
4580 /* allocate the trash with the size of the greatest counters */
4581 if (max_counters_size) {
4582 trash_counters = malloc(max_counters_size);
4583 if (!trash_counters) {
4584 ha_alert("stats: cannot allocate trash counters for statistics\n");
4585 return 0;
4586 }
4587 }
4588
4589 return 1;
4590}
4591
4592REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
4593
Christopher Fauletde79cd22021-01-06 07:41:56 +01004594static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01004595{
4596 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
4597
4598 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
4599 const int domain = domains[i];
4600
Christopher Fauletde79cd22021-01-06 07:41:56 +01004601 free(stat_l[domain]);
4602 stat_l[domain] = NULL;
4603 }
4604}
4605
4606
4607REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
4608
4609static void deinit_stats(void)
4610{
4611 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
4612
4613 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
4614 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01004615
4616 if (stat_f[domain])
4617 free(stat_f[domain]);
4618 }
4619}
4620
4621REGISTER_POST_DEINIT(deinit_stats);
4622
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01004623static void free_trash_counters(void)
4624{
4625 if (trash_counters)
4626 free(trash_counters);
4627}
4628
4629REGISTER_PER_THREAD_FREE(free_trash_counters);
4630
Willy Tarreau2b812e22016-11-22 16:18:05 +01004631/* register cli keywords */
4632static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004633 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreaueaa55372019-10-09 07:39:11 +02004634 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreau3e320362020-10-23 17:28:57 +02004635 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server [desc|json|no-maint|typed|up]*", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01004636 { { "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 +01004637 {{},}
4638}};
4639
Willy Tarreau0108d902018-11-25 19:14:37 +01004640INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4641
William Lallemand74c24fb2016-11-21 17:18:36 +01004642struct applet http_stats_applet = {
4643 .obj_type = OBJ_TYPE_APPLET,
4644 .name = "<STATS>", /* used for logging */
4645 .fct = http_stats_io_handler,
4646 .release = NULL,
4647};
4648
William Lallemand74c24fb2016-11-21 17:18:36 +01004649/*
4650 * Local variables:
4651 * c-indent-level: 8
4652 * c-basic-offset: 8
4653 * End:
4654 */