blob: 2042687f0475a51857bdac7c6a33e0ebaa646773 [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)" },
95 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
96 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
97 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
98 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
99 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
100 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
101 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
102 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
103 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
104 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
105 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
106 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
107 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
108 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
109 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
110 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
111 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
112 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
113 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
114 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
115 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
116 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
117 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
118 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
119 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
120 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
121 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
122 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
123 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
124 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
125 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
126 [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" },
127 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
128 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
129 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
130 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
131 [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)" },
132 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
133 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
134 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
135 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
136 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
137 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
138 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
139 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
140 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
141 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
142 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
143 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
144 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
145 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
146 [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 +0200147 [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 +0200148 [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 +0200149 [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 +0200150 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100151};
152
Willy Tarreaueaa55372019-10-09 07:39:11 +0200153const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
155 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
156 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
157 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
158 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
159 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
160 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
161 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
162 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
163 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
164 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
165 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
166 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
167 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
168 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
169 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
170 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
171 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
172 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
173 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
174 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
175 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
176 [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" },
177 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
178 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
179 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
180 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
181 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
182 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
183 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
184 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
185 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
186 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
187 [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)" },
188 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
189 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
190 [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" },
191 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
192 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
193 [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" },
194 [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" },
195 [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" },
196 [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" },
197 [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" },
198 [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)" },
199 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
200 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
201 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
202 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
203 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
204 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
205 [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" },
206 [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" },
207 [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)" },
208 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
209 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
210 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
211 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
212 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
213 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
214 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
215 [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)" },
216 [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" },
217 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
218 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
219 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
220 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
221 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
222 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
223 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
224 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
225 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
226 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
227 [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" },
228 [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" },
229 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
230 [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" },
231 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
232 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
233 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
234 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
235 [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" },
236 [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" },
237 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
238 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
239 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
240 [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" },
241 [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" },
242 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
243 [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 +0100244 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
245 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
246 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
247 [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 +0100248 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200249 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
250 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
251 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200252 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100253};
254
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100255/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100256static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200257
258/* description of statistics (static and dynamic) */
259static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
260static size_t stat_count[STATS_DOMAIN_COUNT];
261
262/* one line for stats */
263static struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100264
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200265/* list of all registered stats module */
266static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
267 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
268};
269
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200270static inline uint8_t stats_get_domain(uint32_t domain)
271{
272 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
273}
274
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200275static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
276{
277 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
278}
279
Christopher Faulet6338a082019-09-09 15:50:54 +0200280static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100281
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200282int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100283{
284 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100285 if (chk->data >= channel_htx_recv_max(chn, htx))
286 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200287 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100288 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100289 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100290 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100291 }
292 else {
293 if (ci_putchk(chn, chk) == -1)
294 return 0;
295 }
296 return 1;
297}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100298
Christopher Fauleted7a0662019-01-14 11:07:34 +0100299static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
300{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200301 struct channel *req = si_oc(si);
302 struct htx *htx = htxbuf(&req->buf);
303 struct htx_blk *blk;
304 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100305
Christopher Fauletb7f88902019-07-15 21:56:43 +0200306 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100307 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200308 ALREADY_CHECKED(blk);
309 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
310 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100311}
312
William Lallemand74c24fb2016-11-21 17:18:36 +0100313/*
314 * http_stats_io_handler()
315 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
316 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100317 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100318 * -> stats_dump_html_head() // emits the HTML headers
319 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
320 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
321 * -> stats_dump_html_px_hdr()
322 * -> stats_dump_fe_stats()
323 * -> stats_dump_li_stats()
324 * -> stats_dump_sv_stats()
325 * -> stats_dump_be_stats()
326 * -> stats_dump_html_px_end()
327 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100328 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100329 */
330
331
William Lallemand74c24fb2016-11-21 17:18:36 +0100332/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
333 * for clearing it if needed.
334 * NOTE: Some tools happen to rely on the field position instead of its name,
335 * so please only append new fields at the end, never in the middle.
336 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200337static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100338{
339 int field;
340
341 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200342 if (stat_f[domain]) {
343 for (field = 0; field < stat_count[domain]; ++field) {
344 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100345
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200346 /* print special delimiter on proxy stats to mark end of
347 static fields */
348 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
349 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200350 }
351 }
352
William Lallemand74c24fb2016-11-21 17:18:36 +0100353 chunk_appendf(&trash, "\n");
354}
355
William Lallemand74c24fb2016-11-21 17:18:36 +0100356/* Emits a stats field without any surrounding element and properly encoded to
357 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
358 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200359int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100360{
361 switch (field_format(f, 0)) {
362 case FF_EMPTY: return 1;
363 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
364 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
365 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
366 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200367 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100368 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
369 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
370 }
371}
372
373/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
374 * output is supposed to be used on its own line. Returns non-zero on success, 0
375 * if the buffer is full.
376 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200377int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100378{
379 switch (field_format(f, 0)) {
380 case FF_EMPTY: return 1;
381 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
382 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
383 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
384 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200385 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100386 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
387 default: return chunk_appendf(out, "%08x:?", f->type);
388 }
389}
390
Simon Horman05ee2132017-01-04 09:37:25 +0100391/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
392 * the recommendation for interoperable integers in section 6 of RFC 7159.
393 */
394#define JSON_INT_MAX ((1ULL << 53) - 1)
395#define JSON_INT_MIN (0 - JSON_INT_MAX)
396
397/* Emits a stats field value and its type in JSON.
398 * Returns non-zero on success, 0 on error.
399 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200400int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100401{
402 int old_len;
403 char buf[20];
404 const char *type, *value = buf, *quote = "";
405
406 switch (field_format(f, 0)) {
407 case FF_EMPTY: return 1;
408 case FF_S32: type = "\"s32\"";
409 snprintf(buf, sizeof(buf), "%d", f->u.s32);
410 break;
411 case FF_U32: type = "\"u32\"";
412 snprintf(buf, sizeof(buf), "%u", f->u.u32);
413 break;
414 case FF_S64: type = "\"s64\"";
415 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
416 return 0;
417 type = "\"s64\"";
418 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
419 break;
420 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
421 return 0;
422 type = "\"u64\"";
423 snprintf(buf, sizeof(buf), "%llu",
424 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200425 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200426 case FF_FLT: type = "\"flt\"";
427 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100428 break;
429 case FF_STR: type = "\"str\"";
430 value = field_str(f, 0);
431 quote = "\"";
432 break;
433 default: snprintf(buf, sizeof(buf), "%u", f->type);
434 type = buf;
435 value = "unknown";
436 quote = "\"";
437 break;
438 }
439
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200440 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100441 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
442 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200443 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100444}
445
William Lallemand74c24fb2016-11-21 17:18:36 +0100446/* Emits an encoding of the field type on 3 characters followed by a delimiter.
447 * Returns non-zero on success, 0 if the buffer is full.
448 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200449int stats_emit_field_tags(struct buffer *out, const struct field *f,
450 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100451{
452 char origin, nature, scope;
453
454 switch (field_origin(f, 0)) {
455 case FO_METRIC: origin = 'M'; break;
456 case FO_STATUS: origin = 'S'; break;
457 case FO_KEY: origin = 'K'; break;
458 case FO_CONFIG: origin = 'C'; break;
459 case FO_PRODUCT: origin = 'P'; break;
460 default: origin = '?'; break;
461 }
462
463 switch (field_nature(f, 0)) {
464 case FN_GAUGE: nature = 'G'; break;
465 case FN_LIMIT: nature = 'L'; break;
466 case FN_MIN: nature = 'm'; break;
467 case FN_MAX: nature = 'M'; break;
468 case FN_RATE: nature = 'R'; break;
469 case FN_COUNTER: nature = 'C'; break;
470 case FN_DURATION: nature = 'D'; break;
471 case FN_AGE: nature = 'A'; break;
472 case FN_TIME: nature = 'T'; break;
473 case FN_NAME: nature = 'N'; break;
474 case FN_OUTPUT: nature = 'O'; break;
475 case FN_AVG: nature = 'a'; break;
476 default: nature = '?'; break;
477 }
478
479 switch (field_scope(f, 0)) {
480 case FS_PROCESS: scope = 'P'; break;
481 case FS_SERVICE: scope = 'S'; break;
482 case FS_SYSTEM: scope = 's'; break;
483 case FS_CLUSTER: scope = 'C'; break;
484 default: scope = '?'; break;
485 }
486
487 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
488}
489
Simon Horman05ee2132017-01-04 09:37:25 +0100490/* Emits an encoding of the field type as JSON.
491 * Returns non-zero on success, 0 if the buffer is full.
492 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200493int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100494{
495 const char *origin, *nature, *scope;
496 int old_len;
497
498 switch (field_origin(f, 0)) {
499 case FO_METRIC: origin = "Metric"; break;
500 case FO_STATUS: origin = "Status"; break;
501 case FO_KEY: origin = "Key"; break;
502 case FO_CONFIG: origin = "Config"; break;
503 case FO_PRODUCT: origin = "Product"; break;
504 default: origin = "Unknown"; break;
505 }
506
507 switch (field_nature(f, 0)) {
508 case FN_GAUGE: nature = "Gauge"; break;
509 case FN_LIMIT: nature = "Limit"; break;
510 case FN_MIN: nature = "Min"; break;
511 case FN_MAX: nature = "Max"; break;
512 case FN_RATE: nature = "Rate"; break;
513 case FN_COUNTER: nature = "Counter"; break;
514 case FN_DURATION: nature = "Duration"; break;
515 case FN_AGE: nature = "Age"; break;
516 case FN_TIME: nature = "Time"; break;
517 case FN_NAME: nature = "Name"; break;
518 case FN_OUTPUT: nature = "Output"; break;
519 case FN_AVG: nature = "Avg"; break;
520 default: nature = "Unknown"; break;
521 }
522
523 switch (field_scope(f, 0)) {
524 case FS_PROCESS: scope = "Process"; break;
525 case FS_SERVICE: scope = "Service"; break;
526 case FS_SYSTEM: scope = "System"; break;
527 case FS_CLUSTER: scope = "Cluster"; break;
528 default: scope = "Unknown"; break;
529 }
530
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200531 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100532 chunk_appendf(out, "\"tags\":{"
533 "\"origin\":\"%s\","
534 "\"nature\":\"%s\","
535 "\"scope\":\"%s\""
536 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200537 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100538}
William Lallemand74c24fb2016-11-21 17:18:36 +0100539
540/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200541static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200542 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200543 unsigned int flags,
544 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100545{
546 int field;
547
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200548 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100549 if (!stats_emit_raw_data_field(out, &stats[field]))
550 return 0;
551 if (!chunk_strcat(out, ","))
552 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200553
554 /* print special delimiter on proxy stats to mark end of
555 static fields */
556 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
557 if (!chunk_strcat(out, "-,"))
558 return 0;
559 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100560 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200561
William Lallemand74c24fb2016-11-21 17:18:36 +0100562 chunk_strcat(out, "\n");
563 return 1;
564}
565
566/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200567static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200568 const struct field *stats,
569 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200570 unsigned int flags,
571 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100572{
573 int field;
574
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200575 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100576 if (!stats[field].type)
577 continue;
578
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200579 switch (domain) {
580 case STATS_DOMAIN_PROXY:
581 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
582 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
583 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
584 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
585 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
586 '?',
587 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
588 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200589 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200590 stats[ST_F_PID].u.u32);
591 break;
592
593 default:
594 break;
595 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100596
597 if (!stats_emit_field_tags(out, &stats[field], ':'))
598 return 0;
599 if (!stats_emit_typed_data_field(out, &stats[field]))
600 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200601
602 if (flags & STAT_SHOW_FDESC
603 && !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].name)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200604 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200605 }
606
William Lallemand74c24fb2016-11-21 17:18:36 +0100607 if (!chunk_strcat(out, "\n"))
608 return 0;
609 }
610 return 1;
611}
612
Simon Horman05ee2132017-01-04 09:37:25 +0100613/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200614static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200615 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100616{
617 int field;
618 int started = 0;
619
620 if (!chunk_strcat(out, "["))
621 return 0;
622
623 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
624 int old_len;
625
626 if (!field_format(info, field))
627 continue;
628
629 if (started && !chunk_strcat(out, ","))
630 goto err;
631 started = 1;
632
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200633 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100634 chunk_appendf(out,
635 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
636 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200637 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100638 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200639 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100640 goto err;
641
642 if (!stats_emit_json_field_tags(out, &info[field]))
643 goto err;
644
645 if (!stats_emit_json_data_field(out, &info[field]))
646 goto err;
647
648 if (!chunk_strcat(out, "}"))
649 goto err;
650 }
651
652 if (!chunk_strcat(out, "]"))
653 goto err;
654 return 1;
655
656err:
657 chunk_reset(out);
658 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
659 return 0;
660}
661
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200662static void stats_print_proxy_field_json(struct buffer *out,
663 const struct field *stat,
664 const char *name,
665 int pos,
666 uint32_t field_type,
667 uint32_t iid,
668 uint32_t sid,
669 uint32_t pid)
670{
671 const char *obj_type;
672 switch (field_type) {
673 case STATS_TYPE_FE: obj_type = "Frontend"; break;
674 case STATS_TYPE_BE: obj_type = "Backend"; break;
675 case STATS_TYPE_SO: obj_type = "Listener"; break;
676 case STATS_TYPE_SV: obj_type = "Server"; break;
677 default: obj_type = "Unknown"; break;
678 }
679
680 chunk_appendf(out,
681 "{"
682 "\"objType\":\"%s\","
683 "\"proxyId\":%u,"
684 "\"id\":%u,"
685 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
686 "\"processNum\":%u,",
687 obj_type, iid, sid, pos, name, pid);
688}
689
Simon Horman05ee2132017-01-04 09:37:25 +0100690/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200691static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200692 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200693 unsigned int flags,
694 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100695{
696 int field;
697 int started = 0;
698
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200699 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100700 return 0;
701 if (!chunk_strcat(out, "["))
702 return 0;
703
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200704 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100705 int old_len;
706
707 if (!stats[field].type)
708 continue;
709
710 if (started && !chunk_strcat(out, ","))
711 goto err;
712 started = 1;
713
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200714 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200715 if (domain == STATS_DOMAIN_PROXY) {
716 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200717 stat_f[domain][field].name,
718 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200719 stats[ST_F_TYPE].u.u32,
720 stats[ST_F_IID].u.u32,
721 stats[ST_F_SID].u.u32,
722 stats[ST_F_PID].u.u32);
723 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200724
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100726 goto err;
727
728 if (!stats_emit_json_field_tags(out, &stats[field]))
729 goto err;
730
731 if (!stats_emit_json_data_field(out, &stats[field]))
732 goto err;
733
734 if (!chunk_strcat(out, "}"))
735 goto err;
736 }
737
738 if (!chunk_strcat(out, "]"))
739 goto err;
740
741 return 1;
742
743err:
744 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200745 if (flags & STAT_STARTED)
746 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100747 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
748 return 0;
749}
750
William Lallemand74c24fb2016-11-21 17:18:36 +0100751/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200752 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200753 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100754 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200755static int stats_dump_fields_html(struct buffer *out,
756 const struct field *stats,
757 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100758{
Willy Tarreau83061a82018-07-13 11:56:34 +0200759 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100760
761 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
762 chunk_appendf(out,
763 /* name, queue */
764 "<tr class=\"frontend\">");
765
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200766 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100767 /* Column sub-heading for Enable or Disable server */
768 chunk_appendf(out, "<td></td>");
769 }
770
771 chunk_appendf(out,
772 "<td class=ac>"
773 "<a name=\"%s/Frontend\"></a>"
774 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
775 "<td colspan=3></td>"
776 "",
777 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
778
779 chunk_appendf(out,
780 /* sessions rate : current */
781 "<td><u>%s<div class=tips><table class=det>"
782 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
783 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
784 "",
785 U2H(stats[ST_F_RATE].u.u32),
786 U2H(stats[ST_F_CONN_RATE].u.u32),
787 U2H(stats[ST_F_RATE].u.u32));
788
789 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
790 chunk_appendf(out,
791 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
792 U2H(stats[ST_F_REQ_RATE].u.u32));
793
794 chunk_appendf(out,
795 "</table></div></u></td>"
796 /* sessions rate : max */
797 "<td><u>%s<div class=tips><table class=det>"
798 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
799 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
800 "",
801 U2H(stats[ST_F_RATE_MAX].u.u32),
802 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
803 U2H(stats[ST_F_RATE_MAX].u.u32));
804
805 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
806 chunk_appendf(out,
807 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
808 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
809
810 chunk_appendf(out,
811 "</table></div></u></td>"
812 /* sessions rate : limit */
813 "<td>%s</td>",
814 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
815
816 chunk_appendf(out,
817 /* sessions: current, max, limit, total */
818 "<td>%s</td><td>%s</td><td>%s</td>"
819 "<td><u>%s<div class=tips><table class=det>"
820 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
821 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
822 "",
823 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
824 U2H(stats[ST_F_STOT].u.u64),
825 U2H(stats[ST_F_CONN_TOT].u.u64),
826 U2H(stats[ST_F_STOT].u.u64));
827
828 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
829 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
830 chunk_appendf(out,
831 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
832 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
833 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
834 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
835 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
836 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
837 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
838 "<tr><th>- other responses:</th><td>%s</td></tr>"
839 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100840 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
841 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200842 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100843 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100844 "",
845 U2H(stats[ST_F_REQ_TOT].u.u64),
846 U2H(stats[ST_F_HRSP_1XX].u.u64),
847 U2H(stats[ST_F_HRSP_2XX].u.u64),
848 U2H(stats[ST_F_COMP_RSP].u.u64),
849 stats[ST_F_HRSP_2XX].u.u64 ?
850 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
851 U2H(stats[ST_F_HRSP_3XX].u.u64),
852 U2H(stats[ST_F_HRSP_4XX].u.u64),
853 U2H(stats[ST_F_HRSP_5XX].u.u64),
854 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200855 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100856 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
857 U2H(stats[ST_F_CACHE_HITS].u.u64),
858 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
859 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100860 U2H(stats[ST_F_WREW].u.u64),
861 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100862 }
863
864 chunk_appendf(out,
865 "</table></div></u></td>"
866 /* sessions: lbtot, lastsess */
867 "<td></td><td></td>"
868 /* bytes : in */
869 "<td>%s</td>"
870 "",
871 U2H(stats[ST_F_BIN].u.u64));
872
873 chunk_appendf(out,
874 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
875 "<td>%s%s<div class=tips><table class=det>"
876 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
877 "<tr><th>Compression in:</th><td>%s</td></tr>"
878 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
879 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
880 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
881 "</table></div>%s</td>",
882 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
883 U2H(stats[ST_F_BOUT].u.u64),
884 U2H(stats[ST_F_BOUT].u.u64),
885 U2H(stats[ST_F_COMP_IN].u.u64),
886 U2H(stats[ST_F_COMP_OUT].u.u64),
887 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
888 U2H(stats[ST_F_COMP_BYP].u.u64),
889 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
890 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,
891 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
892
893 chunk_appendf(out,
894 /* denied: req, resp */
895 "<td>%s</td><td>%s</td>"
896 /* errors : request, connect, response */
897 "<td>%s</td><td></td><td></td>"
898 /* warnings: retries, redispatches */
899 "<td></td><td></td>"
900 /* server status : reflect frontend status */
901 "<td class=ac>%s</td>"
902 /* rest of server: nothing */
903 "<td class=ac colspan=8></td></tr>"
904 "",
905 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
906 U2H(stats[ST_F_EREQ].u.u64),
907 field_str(stats, ST_F_STATUS));
908 }
909 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
910 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200911 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100912 /* Column sub-heading for Enable or Disable server */
913 chunk_appendf(out, "<td></td>");
914 }
915
916 chunk_appendf(out,
917 /* frontend name, listener name */
918 "<td class=ac><a name=\"%s/+%s\"></a>%s"
919 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
920 "",
921 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200922 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100923 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
924
Willy Tarreau708c4162019-10-09 10:19:16 +0200925 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100926 chunk_appendf(out, "<div class=tips>");
927
Willy Tarreau90807112020-02-25 08:16:33 +0100928 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100929 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
930 else if (*field_str(stats, ST_F_ADDR) == '[')
931 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
932 else if (*field_str(stats, ST_F_ADDR))
933 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
934
935 /* id */
936 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
937 }
938
939 chunk_appendf(out,
940 /* queue */
941 "%s</td><td colspan=3></td>"
942 /* sessions rate: current, max, limit */
943 "<td colspan=3>&nbsp;</td>"
944 /* sessions: current, max, limit, total, lbtot, lastsess */
945 "<td>%s</td><td>%s</td><td>%s</td>"
946 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
947 /* bytes: in, out */
948 "<td>%s</td><td>%s</td>"
949 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200950 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100951 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
952 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
953
954 chunk_appendf(out,
955 /* denied: req, resp */
956 "<td>%s</td><td>%s</td>"
957 /* errors: request, connect, response */
958 "<td>%s</td><td></td><td></td>"
959 /* warnings: retries, redispatches */
960 "<td></td><td></td>"
961 /* server status: reflect listener status */
962 "<td class=ac>%s</td>"
963 /* rest of server: nothing */
964 "<td class=ac colspan=8></td></tr>"
965 "",
966 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
967 U2H(stats[ST_F_EREQ].u.u64),
968 field_str(stats, ST_F_STATUS));
969 }
970 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
971 const char *style;
972
973 /* determine the style to use depending on the server's state,
974 * its health and weight. There isn't a 1-to-1 mapping between
975 * state and styles for the cases where the server is (still)
976 * up. The reason is that we don't want to report nolb and
977 * drain with the same color.
978 */
979
980 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
981 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
982 style = "down";
983 }
984 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
985 style = "going_up";
986 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400987 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
988 style = "draining";
989 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100990 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
991 style = "going_down";
992 }
993 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
994 style = "nolb";
995 }
996 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
997 style = "no_check";
998 }
999 else if (!stats[ST_F_CHKFAIL].type ||
1000 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1001 /* no check or max health = UP */
1002 if (stats[ST_F_WEIGHT].u.u32)
1003 style = "up";
1004 else
1005 style = "draining";
1006 }
1007 else {
1008 style = "going_down";
1009 }
1010
Willy Tarreau7b524852020-08-11 10:26:36 +02001011 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001012 chunk_appendf(out, "<tr class=\"maintain\">");
1013 else
1014 chunk_appendf(out,
1015 "<tr class=\"%s_%s\">",
1016 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1017
1018
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001019 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001020 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001021 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1022 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001023 field_str(stats, ST_F_SVNAME));
1024
1025 chunk_appendf(out,
1026 "<td class=ac><a name=\"%s/%s\"></a>%s"
1027 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1028 "",
1029 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001030 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001031 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1032
Willy Tarreau708c4162019-10-09 10:19:16 +02001033 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001034 chunk_appendf(out, "<div class=tips>");
1035
Willy Tarreau90807112020-02-25 08:16:33 +01001036 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001037 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1038 else if (*field_str(stats, ST_F_ADDR) == '[')
1039 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1040 else if (*field_str(stats, ST_F_ADDR))
1041 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1042
1043 /* id */
1044 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1045
1046 /* cookie */
1047 if (stats[ST_F_COOKIE].type) {
1048 chunk_appendf(out, ", cookie: '");
1049 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1050 chunk_htmlencode(out, &src);
1051 chunk_appendf(out, "'");
1052 }
1053
1054 chunk_appendf(out, "</div>");
1055 }
1056
1057 chunk_appendf(out,
1058 /* queue : current, max, limit */
1059 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1060 /* sessions rate : current, max, limit */
1061 "<td>%s</td><td>%s</td><td></td>"
1062 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001063 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001064 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1065 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1066
1067 chunk_appendf(out,
1068 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001069 "<td><u>%s<div class=tips>"
1070 "<table class=det>"
1071 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001072 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001073 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001074 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1075 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001076 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001077 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1078 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1079 "</table></div></u>"
1080 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001081 "<td><u>%s<div class=tips><table class=det>"
1082 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1083 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001084 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001085 U2H(stats[ST_F_SCUR].u.u32),
1086 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1087 U2H(stats[ST_F_SRV_ICUR].u.u32),
1088 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1089 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001090 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001091
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001092 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1093 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1094 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001095 U2H(stats[ST_F_STOT].u.u64),
1096 U2H(stats[ST_F_STOT].u.u64));
1097
1098 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1099 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001100 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001101 "<tr><th>New connections:</th><td>%s</td></tr>"
1102 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001103 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001104 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1105 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1106 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1107 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1108 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1109 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001110 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001111 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001112 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001113 U2H(stats[ST_F_CONNECT].u.u64),
1114 U2H(stats[ST_F_REUSE].u.u64),
1115 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1116 (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 +02001117 U2H(stats[ST_F_REQ_TOT].u.u64),
1118 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1119 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1120 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1121 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1122 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1123 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1124 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1125 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1126 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1127 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1128 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1129 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001130 U2H(stats[ST_F_WREW].u.u64),
1131 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001132 }
1133
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001134 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1135 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1136 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1137 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1138 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001139 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001140 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1141 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1142 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1143 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001144
1145 chunk_appendf(out,
1146 "</table></div></u></td>"
1147 /* sessions: lbtot, last */
1148 "<td>%s</td><td>%s</td>",
1149 U2H(stats[ST_F_LBTOT].u.u64),
1150 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1151
1152 chunk_appendf(out,
1153 /* bytes : in, out */
1154 "<td>%s</td><td>%s</td>"
1155 /* denied: req, resp */
1156 "<td></td><td>%s</td>"
1157 /* errors : request, connect */
1158 "<td></td><td>%s</td>"
1159 /* errors : response */
1160 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1161 /* warnings: retries, redispatches */
1162 "<td>%lld</td><td>%lld</td>"
1163 "",
1164 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1165 U2H(stats[ST_F_DRESP].u.u64),
1166 U2H(stats[ST_F_ECON].u.u64),
1167 U2H(stats[ST_F_ERESP].u.u64),
1168 (long long)stats[ST_F_CLI_ABRT].u.u64,
1169 (long long)stats[ST_F_SRV_ABRT].u.u64,
1170 (long long)stats[ST_F_WRETR].u.u64,
1171 (long long)stats[ST_F_WREDIS].u.u64);
1172
1173 /* status, last change */
1174 chunk_appendf(out, "<td class=ac>");
1175
1176 /* FIXME!!!!
1177 * LASTCHG should contain the last change for *this* server and must be computed
1178 * properly above, as was done below, ie: this server if maint, otherwise ref server
1179 * if tracking. Note that ref is either local or remote depending on tracking.
1180 */
1181
1182
Willy Tarreau7b524852020-08-11 10:26:36 +02001183 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001184 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1185 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001186 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001187 chunk_strcat(out, "<i>no check</i>");
1188 }
1189 else {
1190 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 +02001191 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001192 if (stats[ST_F_CHECK_HEALTH].u.u32)
1193 chunk_strcat(out, " &uarr;");
1194 }
1195 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1196 chunk_strcat(out, " &darr;");
1197 }
1198
Willy Tarreau7b524852020-08-11 10:26:36 +02001199 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001200 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1201 chunk_appendf(out,
1202 "</td><td class=ac><u> %s",
1203 field_str(stats, ST_F_AGENT_STATUS));
1204
1205 if (stats[ST_F_AGENT_CODE].type)
1206 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1207
1208 if (stats[ST_F_AGENT_DURATION].type)
1209 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1210
1211 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1212
1213 if (*field_str(stats, ST_F_LAST_AGT)) {
1214 chunk_appendf(out, ": ");
1215 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1216 chunk_htmlencode(out, &src);
1217 }
1218 chunk_appendf(out, "</div></u>");
1219 }
1220 else if (stats[ST_F_CHECK_STATUS].type) {
1221 chunk_appendf(out,
1222 "</td><td class=ac><u> %s",
1223 field_str(stats, ST_F_CHECK_STATUS));
1224
1225 if (stats[ST_F_CHECK_CODE].type)
1226 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1227
1228 if (stats[ST_F_CHECK_DURATION].type)
1229 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1230
1231 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1232
1233 if (*field_str(stats, ST_F_LAST_CHK)) {
1234 chunk_appendf(out, ": ");
1235 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1236 chunk_htmlencode(out, &src);
1237 }
1238 chunk_appendf(out, "</div></u>");
1239 }
1240 else
1241 chunk_appendf(out, "</td><td>");
1242
1243 chunk_appendf(out,
1244 /* weight */
1245 "</td><td class=ac>%d</td>"
1246 /* act, bck */
1247 "<td class=ac>%s</td><td class=ac>%s</td>"
1248 "",
1249 stats[ST_F_WEIGHT].u.u32,
1250 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1251 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1252
1253 /* check failures: unique, fatal, down time */
1254 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1255 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1256 }
1257 else if (stats[ST_F_CHKFAIL].type) {
1258 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1259
1260 if (stats[ST_F_HANAFAIL].type)
1261 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1262
1263 chunk_appendf(out,
1264 "<div class=tips>Failed Health Checks%s</div></u></td>"
1265 "<td>%lld</td><td>%s</td>"
1266 "",
1267 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1268 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1269 }
1270 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1271 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1272 chunk_appendf(out,
1273 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1274 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1275 }
1276 else
1277 chunk_appendf(out, "<td colspan=3></td>");
1278
1279 /* throttle */
1280 if (stats[ST_F_THROTTLE].type)
1281 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1282 else
1283 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1284 }
1285 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1286 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001287 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001288 /* Column sub-heading for Enable or Disable server */
1289 chunk_appendf(out, "<td></td>");
1290 }
1291 chunk_appendf(out,
1292 "<td class=ac>"
1293 /* name */
1294 "%s<a name=\"%s/Backend\"></a>"
1295 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1296 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001297 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001298 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1299
Willy Tarreau708c4162019-10-09 10:19:16 +02001300 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001301 /* balancing */
1302 chunk_appendf(out, "<div class=tips>balancing: %s",
1303 field_str(stats, ST_F_ALGO));
1304
1305 /* cookie */
1306 if (stats[ST_F_COOKIE].type) {
1307 chunk_appendf(out, ", cookie: '");
1308 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1309 chunk_htmlencode(out, &src);
1310 chunk_appendf(out, "'");
1311 }
1312 chunk_appendf(out, "</div>");
1313 }
1314
1315 chunk_appendf(out,
1316 "%s</td>"
1317 /* queue : current, max */
1318 "<td>%s</td><td>%s</td><td></td>"
1319 /* sessions rate : current, max, limit */
1320 "<td>%s</td><td>%s</td><td></td>"
1321 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001322 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001323 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1324 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1325
1326 chunk_appendf(out,
1327 /* sessions: current, max, limit, total */
1328 "<td>%s</td><td>%s</td><td>%s</td>"
1329 "<td><u>%s<div class=tips><table class=det>"
1330 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1331 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001332 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 +01001333 U2H(stats[ST_F_STOT].u.u64),
1334 U2H(stats[ST_F_STOT].u.u64));
1335
1336 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1337 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1338 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001339 "<tr><th>New connections:</th><td>%s</td></tr>"
1340 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001341 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1342 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1343 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1344 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1345 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1346 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1347 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1348 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001349 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1350 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001351 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001352 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001353 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001354 U2H(stats[ST_F_CONNECT].u.u64),
1355 U2H(stats[ST_F_REUSE].u.u64),
1356 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1357 (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 +01001358 U2H(stats[ST_F_REQ_TOT].u.u64),
1359 U2H(stats[ST_F_HRSP_1XX].u.u64),
1360 U2H(stats[ST_F_HRSP_2XX].u.u64),
1361 U2H(stats[ST_F_COMP_RSP].u.u64),
1362 stats[ST_F_HRSP_2XX].u.u64 ?
1363 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1364 U2H(stats[ST_F_HRSP_3XX].u.u64),
1365 U2H(stats[ST_F_HRSP_4XX].u.u64),
1366 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001367 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001368 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1369 U2H(stats[ST_F_CACHE_HITS].u.u64),
1370 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1371 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001372 U2H(stats[ST_F_WREW].u.u64),
1373 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001374 }
1375
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001376 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1377 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1378 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1379 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1380 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001381 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001382 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1383 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1384 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1385 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001386
1387 chunk_appendf(out,
1388 "</table></div></u></td>"
1389 /* sessions: lbtot, last */
1390 "<td>%s</td><td>%s</td>"
1391 /* bytes: in */
1392 "<td>%s</td>"
1393 "",
1394 U2H(stats[ST_F_LBTOT].u.u64),
1395 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1396 U2H(stats[ST_F_BIN].u.u64));
1397
1398 chunk_appendf(out,
1399 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1400 "<td>%s%s<div class=tips><table class=det>"
1401 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1402 "<tr><th>Compression in:</th><td>%s</td></tr>"
1403 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1404 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1405 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1406 "</table></div>%s</td>",
1407 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1408 U2H(stats[ST_F_BOUT].u.u64),
1409 U2H(stats[ST_F_BOUT].u.u64),
1410 U2H(stats[ST_F_COMP_IN].u.u64),
1411 U2H(stats[ST_F_COMP_OUT].u.u64),
1412 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1413 U2H(stats[ST_F_COMP_BYP].u.u64),
1414 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1415 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,
1416 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1417
1418 chunk_appendf(out,
1419 /* denied: req, resp */
1420 "<td>%s</td><td>%s</td>"
1421 /* errors : request, connect */
1422 "<td></td><td>%s</td>"
1423 /* errors : response */
1424 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1425 /* warnings: retries, redispatches */
1426 "<td>%lld</td><td>%lld</td>"
1427 /* backend status: reflect backend status (up/down): we display UP
1428 * if the backend has known working servers or if it has no server at
1429 * all (eg: for stats). Then we display the total weight, number of
1430 * active and backups. */
1431 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1432 "<td class=ac>%d</td><td class=ac>%d</td>"
1433 "",
1434 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1435 U2H(stats[ST_F_ECON].u.u64),
1436 U2H(stats[ST_F_ERESP].u.u64),
1437 (long long)stats[ST_F_CLI_ABRT].u.u64,
1438 (long long)stats[ST_F_SRV_ABRT].u.u64,
1439 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1440 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1441 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1442 stats[ST_F_WEIGHT].u.u32,
1443 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1444
1445 chunk_appendf(out,
1446 /* rest of backend: nothing, down transitions, total downtime, throttle */
1447 "<td class=ac>&nbsp;</td><td>%d</td>"
1448 "<td>%s</td>"
1449 "<td></td>"
1450 "</tr>",
1451 stats[ST_F_CHKDOWN].u.u32,
1452 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1453 }
1454 return 1;
1455}
1456
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001457int stats_dump_one_line(const struct field *stats, size_t stats_count,
1458 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001459{
Simon Horman05ee2132017-01-04 09:37:25 +01001460 int ret;
1461
William Lallemand74c24fb2016-11-21 17:18:36 +01001462 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001463 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001464 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001465 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001466 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001467 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001468 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001469 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001470
1471 if (ret)
1472 appctx->ctx.stats.flags |= STAT_STARTED;
1473
1474 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001475}
1476
1477/* Fill <stats> with the frontend statistics. <stats> is
1478 * preallocated array of length <len>. The length of the array
1479 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1480 * this value, the function returns 0, otherwise, it returns 1.
1481 */
1482int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1483{
1484 if (len < ST_F_TOTAL_FIELDS)
1485 return 0;
1486
William Lallemand74c24fb2016-11-21 17:18:36 +01001487 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1488 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1489 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1490 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1491 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1492 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1493 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1494 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1495 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1496 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1497 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1498 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1499 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1500 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1501 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1502 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1503 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1504 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1505 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1506 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1507 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1508 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001509 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001510 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001511
1512 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1513 if (px->mode == PR_MODE_HTTP) {
1514 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1515 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1516 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1517 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1518 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1519 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1520 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001521 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1522 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001523 }
1524
1525 /* requests : req_rate, req_rate_max, req_tot, */
1526 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1527 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1528 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1529
1530 /* compression: in, out, bypassed, responses */
1531 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1532 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1533 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1534 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1535
1536 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1537 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1538 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1539 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1540
1541 return 1;
1542}
1543
1544/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1545 * the state from stream interface <si>. The caller is responsible for clearing
1546 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1547 */
1548static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1549{
1550 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001551 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1552 struct stats_module *mod;
1553 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001554
1555 if (!(px->cap & PR_CAP_FE))
1556 return 0;
1557
1558 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1559 return 0;
1560
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001561 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1562
William Lallemand74c24fb2016-11-21 17:18:36 +01001563 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1564 return 0;
1565
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001566 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1567 void *counters;
1568
1569 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1570 stats_count += mod->stats_count;
1571 continue;
1572 }
1573
1574 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1575 mod->fill_stats(counters, stats + stats_count);
1576 stats_count += mod->stats_count;
1577 }
1578
1579 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001580}
1581
1582/* Fill <stats> with the listener statistics. <stats> is
1583 * preallocated array of length <len>. The length of the array
1584 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1585 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001586 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001587 */
1588int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1589 struct field *stats, int len)
1590{
Willy Tarreau83061a82018-07-13 11:56:34 +02001591 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001592
1593 if (len < ST_F_TOTAL_FIELDS)
1594 return 0;
1595
1596 if (!l->counters)
1597 return 0;
1598
1599 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001600
1601 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1602 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1603 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1604 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1605 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1606 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1607 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1608 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1609 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1610 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1611 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1612 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1613 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1614 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001615 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 +01001616 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1617 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1618 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1619 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001620 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001621 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001622
Willy Tarreau708c4162019-10-09 10:19:16 +02001623 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001624 char str[INET6_ADDRSTRLEN];
1625 int port;
1626
Willy Tarreau37159062020-08-27 07:48:42 +02001627 port = get_host_port(&l->rx.addr);
1628 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001629 case AF_INET:
1630 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1631 chunk_appendf(out, "%s:%d", str, port);
1632 break;
1633 case AF_INET6:
1634 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1635 chunk_appendf(out, "[%s]:%d", str, port);
1636 break;
1637 case AF_UNIX:
1638 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1639 break;
1640 case -1:
1641 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1642 chunk_strcat(out, strerror(errno));
1643 break;
1644 default: /* address family not supported */
1645 break;
1646 }
1647 }
1648
1649 return 1;
1650}
1651
1652/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001653 * from stream interface <si>. The caller is responsible for clearing the trash
1654 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001655 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001656static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001657{
1658 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001659 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1660 struct stats_module *mod;
1661 size_t stats_count = ST_F_TOTAL_FIELDS;
1662
1663 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001664
Willy Tarreau43241ff2019-10-09 11:27:51 +02001665 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001666 return 0;
1667
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001668 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1669 void *counters;
1670
1671 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1672 stats_count += mod->stats_count;
1673 continue;
1674 }
1675
1676 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
1677 mod->fill_stats(counters, stats + stats_count);
1678 stats_count += mod->stats_count;
1679 }
1680
1681 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001682}
1683
1684enum srv_stats_state {
1685 SRV_STATS_STATE_DOWN = 0,
1686 SRV_STATS_STATE_DOWN_AGENT,
1687 SRV_STATS_STATE_GOING_UP,
1688 SRV_STATS_STATE_UP_GOING_DOWN,
1689 SRV_STATS_STATE_UP,
1690 SRV_STATS_STATE_NOLB_GOING_DOWN,
1691 SRV_STATS_STATE_NOLB,
1692 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1693 SRV_STATS_STATE_DRAIN,
1694 SRV_STATS_STATE_DRAIN_AGENT,
1695 SRV_STATS_STATE_NO_CHECK,
1696
1697 SRV_STATS_STATE_COUNT, /* Must be last */
1698};
1699
1700static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1701 [SRV_STATS_STATE_DOWN] = "DOWN",
1702 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1703 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1704 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1705 [SRV_STATS_STATE_UP] = "UP",
1706 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1707 [SRV_STATS_STATE_NOLB] = "NOLB",
1708 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1709 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1710 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1711 [SRV_STATS_STATE_NO_CHECK] = "no check"
1712};
1713
1714/* Fill <stats> with the server statistics. <stats> is
1715 * preallocated array of length <len>. The length of the array
1716 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1717 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001718 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001719 */
1720int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1721 struct field *stats, int len)
1722{
1723 struct server *via, *ref;
1724 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001725 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001726 enum srv_stats_state state;
1727 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001728 long long srv_samples_counter;
1729 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001730
1731 if (len < ST_F_TOTAL_FIELDS)
1732 return 0;
1733
William Lallemand74c24fb2016-11-21 17:18:36 +01001734 /* we have "via" which is the tracked server as described in the configuration,
1735 * and "ref" which is the checked server and the end of the chain.
1736 */
1737 via = sv->track ? sv->track : sv;
1738 ref = via;
1739 while (ref->track)
1740 ref = ref->track;
1741
Emeric Brun52a91d32017-08-31 14:41:55 +02001742 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001743 if ((ref->check.state & CHK_ST_ENABLED) &&
1744 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1745 state = SRV_STATS_STATE_UP_GOING_DOWN;
1746 } else {
1747 state = SRV_STATS_STATE_UP;
1748 }
1749
Emeric Brun52a91d32017-08-31 14:41:55 +02001750 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001751 if (ref->agent.state & CHK_ST_ENABLED)
1752 state = SRV_STATS_STATE_DRAIN_AGENT;
1753 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1754 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1755 else
1756 state = SRV_STATS_STATE_DRAIN;
1757 }
1758
1759 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1760 state = SRV_STATS_STATE_NO_CHECK;
1761 }
1762 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001763 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001764 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1765 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1766 state = SRV_STATS_STATE_NOLB;
1767 } else {
1768 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1769 }
1770 }
1771 else { /* stopped */
1772 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1773 state = SRV_STATS_STATE_DOWN_AGENT;
1774 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1775 state = SRV_STATS_STATE_DOWN; /* DOWN */
1776 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1777 state = SRV_STATS_STATE_GOING_UP;
1778 } else {
1779 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1780 }
1781 }
1782
1783 chunk_reset(out);
1784
1785 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1786 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1787 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1788 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1789 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1790 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1791 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1792
1793 if (sv->maxconn)
1794 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1795
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001796 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1797 if (sv->max_idle_conns != -1)
1798 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1799
William Lallemand74c24fb2016-11-21 17:18:36 +01001800 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1801 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1802 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001803 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001804 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1805 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1806 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1807 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001808 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001809 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001810 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1811 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001812
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001813 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1814 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1815 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001816 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001817
William Lallemand74c24fb2016-11-21 17:18:36 +01001818 /* status */
1819 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001820 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001821 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001822 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001823 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001824 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001825 chunk_appendf(out, "MAINT");
1826 else
1827 chunk_appendf(out,
1828 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001829 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1830 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001831
1832 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1833 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001834 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
William Lallemand74c24fb2016-11-21 17:18:36 +01001835 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1836 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1837
1838 /* check failures: unique, fatal; last change, total downtime */
1839 if (sv->check.state & CHK_ST_ENABLED) {
1840 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1841 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1842 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1843 }
1844
1845 if (sv->maxqueue)
1846 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1847
1848 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1849 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1850 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1851
Emeric Brun52a91d32017-08-31 14:41:55 +02001852 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001853 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1854
1855 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1856
1857 if (sv->track) {
1858 char *fld_track = chunk_newstr(out);
1859
1860 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1861 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1862 }
1863
1864 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1865 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1866 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1867
1868 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1869 const char *fld_chksts;
1870
1871 fld_chksts = chunk_newstr(out);
1872 chunk_strcat(out, "* "); // for check in progress
1873 chunk_strcat(out, get_check_status_info(sv->check.status));
1874 if (!(sv->check.state & CHK_ST_INPROGRESS))
1875 fld_chksts += 2; // skip "* "
1876 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1877
1878 if (sv->check.status >= HCHK_STATUS_L57DATA)
1879 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1880
1881 if (sv->check.status >= HCHK_STATUS_CHECKED)
1882 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1883
1884 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1885 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1886 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1887 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1888 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1889 }
1890
1891 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1892 const char *fld_chksts;
1893
1894 fld_chksts = chunk_newstr(out);
1895 chunk_strcat(out, "* "); // for check in progress
1896 chunk_strcat(out, get_check_status_info(sv->agent.status));
1897 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1898 fld_chksts += 2; // skip "* "
1899 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1900
1901 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1902 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1903
1904 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1905 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1906
1907 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1908 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1909 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1910 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1911 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1912 }
1913
1914 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1915 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001916 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001917 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1918 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1919 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1920 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1921 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1922 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1923 }
1924
1925 if (ref->observe)
1926 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1927
1928 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1929 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1930 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1931
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001932 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1933 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1934 srv_samples_window = srv_samples_counter;
1935
1936 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1937 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1938 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1939 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001940
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001941 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1942 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1943 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1944 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1945
Willy Tarreau708c4162019-10-09 10:19:16 +02001946 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001947 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1948 case AF_INET:
1949 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001950 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001951 break;
1952 case AF_INET6:
1953 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001954 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001955 break;
1956 case AF_UNIX:
1957 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1958 break;
1959 case -1:
1960 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1961 chunk_strcat(out, strerror(errno));
1962 break;
1963 default: /* address family not supported */
1964 break;
1965 }
1966
1967 if (sv->cookie)
1968 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1969 }
1970
1971 return 1;
1972}
1973
1974/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001975 * from stream interface <si>, and server state <state>. The caller is
1976 * responsible for clearing the trash if needed. Returns non-zero if it emits
1977 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001978 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001979static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001980{
1981 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001982 struct stats_module *mod;
1983 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1984 size_t stats_count = ST_F_TOTAL_FIELDS;
1985
1986 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001987
Willy Tarreau43241ff2019-10-09 11:27:51 +02001988 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001989 return 0;
1990
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001991 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1992 void *counters;
1993
1994 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
1995 continue;
1996
1997 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
1998 stats_count += mod->stats_count;
1999 continue;
2000 }
2001
2002 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2003 mod->fill_stats(counters, stats + stats_count);
2004 stats_count += mod->stats_count;
2005 }
2006
2007 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002008}
2009
2010/* Fill <stats> with the backend statistics. <stats> is
2011 * preallocated array of length <len>. The length of the array
2012 * must be at least ST_F_TOTAL_FIELDS. If this length is less
2013 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02002014 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01002015 */
2016int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
2017{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002018 long long be_samples_counter;
2019 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2020
William Lallemand74c24fb2016-11-21 17:18:36 +01002021 if (len < ST_F_TOTAL_FIELDS)
2022 return 0;
2023
William Lallemand74c24fb2016-11-21 17:18:36 +01002024 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2025 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2026 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2027 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
2028 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01002029 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01002030 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
2031 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2032 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2033 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2034 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2035 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2036 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2037 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2038 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2039 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
2040 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002041 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01002042 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01002043 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
2044 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01002045 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2046 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2047 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
2048 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
2049 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
2050 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2051 if (px->srv)
2052 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
2053
2054 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
2055 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2056 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
2057 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2058 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2059 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2060 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
2061
Willy Tarreau708c4162019-10-09 10:19:16 +02002062 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002063 if (px->cookie_name)
2064 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2065 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2066 }
2067
2068 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2069 if (px->mode == PR_MODE_HTTP) {
2070 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2071 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2072 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2073 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2074 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2075 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2076 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01002077 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2078 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01002079 }
2080
2081 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2082 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2083
2084 /* compression: in, out, bypassed, responses */
2085 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2086 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2087 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2088 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2089 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
2090
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002091 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2092 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2093 be_samples_window = be_samples_counter;
2094
2095 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2096 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2097 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2098 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 +01002099
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002100 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2101 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2102 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2103 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2104
William Lallemand74c24fb2016-11-21 17:18:36 +01002105 return 1;
2106}
2107
2108/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002109 * interface <si>. The caller is responsible for clearing the trash if needed.
2110 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002111 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002112static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002113{
2114 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002115 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2116 struct stats_module *mod;
2117 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002118
2119 if (!(px->cap & PR_CAP_BE))
2120 return 0;
2121
2122 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2123 return 0;
2124
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002125 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2126
Willy Tarreau43241ff2019-10-09 11:27:51 +02002127 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002128 return 0;
2129
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002130 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2131 struct extra_counters *counters;
2132
2133 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2134 continue;
2135
2136 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2137 stats_count += mod->stats_count;
2138 continue;
2139 }
2140
2141 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2142 mod->fill_stats(counters, stats + stats_count);
2143 stats_count += mod->stats_count;
2144 }
2145
2146 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002147}
2148
2149/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2150 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2151 * for clearing the trash if needed.
2152 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002153static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002154{
2155 struct appctx *appctx = __objt_appctx(si->end);
2156 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2157
2158 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2159 /* A form to enable/disable this proxy servers */
2160
2161 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2162 scope_txt[0] = 0;
2163 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002164 const char *scope_ptr = stats_scope_ptr(appctx, si);
2165
William Lallemand74c24fb2016-11-21 17:18:36 +01002166 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002167 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002168 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2169 }
2170
2171 chunk_appendf(&trash,
2172 "<form method=\"post\">");
2173 }
2174
2175 /* print a new table */
2176 chunk_appendf(&trash,
2177 "<table class=\"tbl\" width=\"100%%\">\n"
2178 "<tr class=\"titre\">"
2179 "<th class=\"pxname\" width=\"10%%\">");
2180
2181 chunk_appendf(&trash,
2182 "<a name=\"%s\"></a>%s"
2183 "<a class=px href=\"#%s\">%s</a>",
2184 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002185 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002186 px->id, px->id);
2187
Willy Tarreau676c29e2019-10-09 10:50:01 +02002188 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002189 /* cap, mode, id */
2190 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2191 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2192 px->uuid);
2193 chunk_appendf(&trash, "</div>");
2194 }
2195
2196 chunk_appendf(&trash,
2197 "%s</th>"
2198 "<th class=\"%s\" width=\"90%%\">%s</th>"
2199 "</tr>\n"
2200 "</table>\n"
2201 "<table class=\"tbl\" width=\"100%%\">\n"
2202 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002203 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002204 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2205
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002206 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002207 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002208 if ((px->cap & PR_CAP_BE) && px->srv)
2209 chunk_appendf(&trash,
2210 "<th rowspan=2 width=1><input type=\"checkbox\" "
2211 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2212 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2213 px->id,
2214 px->id);
2215 else
2216 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002217 }
2218
2219 chunk_appendf(&trash,
2220 "<th rowspan=2></th>"
2221 "<th colspan=3>Queue</th>"
2222 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2223 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2224 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2225 "<th colspan=9>Server</th>"
2226 "</tr>\n"
2227 "<tr class=\"titre\">"
2228 "<th>Cur</th><th>Max</th><th>Limit</th>"
2229 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2230 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2231 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2232 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2233 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2234 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2235 "<th>Thrtle</th>\n"
2236 "</tr>");
2237}
2238
2239/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2240 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2241 */
2242static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2243{
2244 struct appctx *appctx = __objt_appctx(si->end);
2245 chunk_appendf(&trash, "</table>");
2246
2247 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2248 /* close the form used to enable/disable this proxy servers */
2249 chunk_appendf(&trash,
2250 "Choose the action to perform on the checked servers : "
2251 "<select name=action>"
2252 "<option value=\"\"></option>"
2253 "<option value=\"ready\">Set state to READY</option>"
2254 "<option value=\"drain\">Set state to DRAIN</option>"
2255 "<option value=\"maint\">Set state to MAINT</option>"
2256 "<option value=\"dhlth\">Health: disable checks</option>"
2257 "<option value=\"ehlth\">Health: enable checks</option>"
2258 "<option value=\"hrunn\">Health: force UP</option>"
2259 "<option value=\"hnolb\">Health: force NOLB</option>"
2260 "<option value=\"hdown\">Health: force DOWN</option>"
2261 "<option value=\"dagent\">Agent: disable checks</option>"
2262 "<option value=\"eagent\">Agent: enable checks</option>"
2263 "<option value=\"arunn\">Agent: force UP</option>"
2264 "<option value=\"adown\">Agent: force DOWN</option>"
2265 "<option value=\"shutdown\">Kill Sessions</option>"
2266 "</select>"
2267 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2268 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2269 "</form>",
2270 px->uuid);
2271 }
2272
2273 chunk_appendf(&trash, "<p>\n");
2274}
2275
2276/*
2277 * Dumps statistics for a proxy. The output is sent to the stream interface's
2278 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2279 * buffer space, or non-zero if everything completed. This function is used
2280 * both by the CLI and the HTTP entry points, and is able to dump the output
2281 * in HTML or CSV formats. If the later, <uri> must be NULL.
2282 */
Christopher Fauletef779222018-10-31 08:47:01 +01002283int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2284 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002285{
2286 struct appctx *appctx = __objt_appctx(si->end);
2287 struct stream *s = si_strm(si);
2288 struct channel *rep = si_ic(si);
2289 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2290 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002291
2292 chunk_reset(&trash);
2293
2294 switch (appctx->ctx.stats.px_st) {
2295 case STAT_PX_ST_INIT:
2296 /* we are on a new proxy */
2297 if (uri && uri->scope) {
2298 /* we have a limited scope, we have to check the proxy name */
2299 struct stat_scope *scope;
2300 int len;
2301
2302 len = strlen(px->id);
2303 scope = uri->scope;
2304
2305 while (scope) {
2306 /* match exact proxy name */
2307 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2308 break;
2309
2310 /* match '.' which means 'self' proxy */
2311 if (!strcmp(scope->px_id, ".") && px == s->be)
2312 break;
2313 scope = scope->next;
2314 }
2315
2316 /* proxy name not found : don't dump anything */
2317 if (scope == NULL)
2318 return 1;
2319 }
2320
2321 /* if the user has requested a limited output and the proxy
2322 * name does not match, skip it.
2323 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002324 if (appctx->ctx.stats.scope_len) {
2325 const char *scope_ptr = stats_scope_ptr(appctx, si);
2326
2327 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2328 return 1;
2329 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002330
2331 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2332 (appctx->ctx.stats.iid != -1) &&
2333 (px->uuid != appctx->ctx.stats.iid))
2334 return 1;
2335
2336 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2337 /* fall through */
2338
2339 case STAT_PX_ST_TH:
2340 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002341 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002342 if (!stats_putchk(rep, htx, &trash))
2343 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002344 }
2345
2346 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2347 /* fall through */
2348
2349 case STAT_PX_ST_FE:
2350 /* print the frontend */
2351 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002352 if (!stats_putchk(rep, htx, &trash))
2353 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002354 }
2355
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002356 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01002357 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2358 /* fall through */
2359
2360 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002361 /* obj2 points to listeners list as initialized above */
2362 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002363 if (htx) {
2364 if (htx_almost_full(htx))
2365 goto full;
2366 }
2367 else {
2368 if (buffer_almost_full(&rep->buf))
2369 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002370 }
2371
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002372 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01002373 if (!l->counters)
2374 continue;
2375
2376 if (appctx->ctx.stats.flags & STAT_BOUND) {
2377 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2378 break;
2379
2380 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2381 continue;
2382 }
2383
2384 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002385 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002386 if (!stats_putchk(rep, htx, &trash))
2387 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002388 }
2389 }
2390
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002391 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01002392 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2393 /* fall through */
2394
2395 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002396 /* obj2 points to servers list as initialized above */
2397 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002398 if (htx) {
2399 if (htx_almost_full(htx))
2400 goto full;
2401 }
2402 else {
2403 if (buffer_almost_full(&rep->buf))
2404 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002405 }
2406
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002407 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01002408
2409 if (appctx->ctx.stats.flags & STAT_BOUND) {
2410 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2411 break;
2412
2413 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2414 continue;
2415 }
2416
2417 svs = sv;
2418 while (svs->track)
2419 svs = svs->track;
2420
2421 /* do not report servers which are DOWN and not changing state */
2422 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002423 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2424 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002425 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2426 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2427 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2428 continue;
2429 }
2430
Willy Tarreau43241ff2019-10-09 11:27:51 +02002431 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002432 if (!stats_putchk(rep, htx, &trash))
2433 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002434 }
2435 } /* for sv */
2436
2437 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2438 /* fall through */
2439
2440 case STAT_PX_ST_BE:
2441 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002442 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002443 if (!stats_putchk(rep, htx, &trash))
2444 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002445 }
2446
2447 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2448 /* fall through */
2449
2450 case STAT_PX_ST_END:
2451 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2452 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002453 if (!stats_putchk(rep, htx, &trash))
2454 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002455 }
2456
2457 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2458 /* fall through */
2459
2460 case STAT_PX_ST_FIN:
2461 return 1;
2462
2463 default:
2464 /* unknown state, we should put an abort() here ! */
2465 return 1;
2466 }
Christopher Fauletef779222018-10-31 08:47:01 +01002467
2468 full:
2469 si_rx_room_blk(si);
2470 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002471}
2472
2473/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2474 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2475 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002476static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002477{
2478 /* WARNING! This must fit in the first buffer !!! */
2479 chunk_appendf(&trash,
2480 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2481 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2482 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03002483 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002484 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2485 "<style type=\"text/css\"><!--\n"
2486 "body {"
2487 " font-family: arial, helvetica, sans-serif;"
2488 " font-size: 12px;"
2489 " font-weight: normal;"
2490 " color: black;"
2491 " background: white;"
2492 "}\n"
2493 "th,td {"
2494 " font-size: 10px;"
2495 "}\n"
2496 "h1 {"
2497 " font-size: x-large;"
2498 " margin-bottom: 0.5em;"
2499 "}\n"
2500 "h2 {"
2501 " font-family: helvetica, arial;"
2502 " font-size: x-large;"
2503 " font-weight: bold;"
2504 " font-style: italic;"
2505 " color: #6020a0;"
2506 " margin-top: 0em;"
2507 " margin-bottom: 0em;"
2508 "}\n"
2509 "h3 {"
2510 " font-family: helvetica, arial;"
2511 " font-size: 16px;"
2512 " font-weight: bold;"
2513 " color: #b00040;"
2514 " background: #e8e8d0;"
2515 " margin-top: 0em;"
2516 " margin-bottom: 0em;"
2517 "}\n"
2518 "li {"
2519 " margin-top: 0.25em;"
2520 " margin-right: 2em;"
2521 "}\n"
2522 ".hr {margin-top: 0.25em;"
2523 " border-color: black;"
2524 " border-bottom-style: solid;"
2525 "}\n"
2526 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2527 ".total {background: #20D0D0;color: #ffff80;}\n"
2528 ".frontend {background: #e8e8d0;}\n"
2529 ".socket {background: #d0d0d0;}\n"
2530 ".backend {background: #e8e8d0;}\n"
2531 ".active_down {background: #ff9090;}\n"
2532 ".active_going_up {background: #ffd020;}\n"
2533 ".active_going_down {background: #ffffa0;}\n"
2534 ".active_up {background: #c0ffc0;}\n"
2535 ".active_nolb {background: #20a0ff;}\n"
2536 ".active_draining {background: #20a0FF;}\n"
2537 ".active_no_check {background: #e0e0e0;}\n"
2538 ".backup_down {background: #ff9090;}\n"
2539 ".backup_going_up {background: #ff80ff;}\n"
2540 ".backup_going_down {background: #c060ff;}\n"
2541 ".backup_up {background: #b0d0ff;}\n"
2542 ".backup_nolb {background: #90b0e0;}\n"
2543 ".backup_draining {background: #cc9900;}\n"
2544 ".backup_no_check {background: #e0e0e0;}\n"
2545 ".maintain {background: #c07820;}\n"
2546 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2547 "\n"
2548 "a.px:link {color: #ffff40; text-decoration: none;}"
2549 "a.px:visited {color: #ffff40; text-decoration: none;}"
2550 "a.px:hover {color: #ffffff; text-decoration: none;}"
2551 "a.lfsb:link {color: #000000; text-decoration: none;}"
2552 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2553 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2554 "\n"
2555 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2556 "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"
2557 "table.tbl td.ac { text-align: center;}\n"
2558 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2559 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2560 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2561 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2562 "\n"
2563 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2564 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2565 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2566 "table.det { border-collapse: collapse; border-style: none; }\n"
2567 "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"
2568 "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"
2569 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2570 "div.tips {\n"
2571 " display:block;\n"
2572 " visibility:hidden;\n"
2573 " z-index:2147483647;\n"
2574 " position:absolute;\n"
2575 " padding:2px 4px 3px;\n"
2576 " background:#f0f060; color:#000000;\n"
2577 " border:1px solid #7040c0;\n"
2578 " white-space:nowrap;\n"
2579 " font-style:normal;font-size:11px;font-weight:normal;\n"
2580 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2581 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2582 "}\n"
2583 "u:hover div.tips {visibility:visible;}\n"
2584 "-->\n"
2585 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002586 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002587 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002588 );
2589}
2590
2591/* Dumps the HTML stats information block to the trash for and uses the state from
2592 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2593 * for clearing the trash if needed.
2594 */
2595static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2596{
2597 struct appctx *appctx = __objt_appctx(si->end);
2598 unsigned int up = (now.tv_sec - start_date.tv_sec);
2599 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002600 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002601 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2602
2603 /* Turn the bytes per second to bits per second and take care of the
2604 * usual ethernet overhead in order to help figure how far we are from
2605 * interface saturation since it's the only case which usually matters.
2606 * For this we count the total size of an Ethernet frame on the wire
2607 * including preamble and IFG (1538) for the largest TCP segment it
2608 * transports (1448 with TCP timestamps). This is not valid for smaller
2609 * packets (under-estimated), but it gives a reasonably accurate
2610 * estimation of how far we are from uplink saturation.
2611 */
2612 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002613
2614 /* WARNING! this has to fit the first packet too.
2615 * We are around 3.5 kB, add adding entries will
2616 * become tricky if we want to support 4kB buffers !
2617 */
2618 chunk_appendf(&trash,
2619 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2620 PRODUCT_NAME "%s</a></h1>\n"
2621 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2622 "<hr width=\"100%%\" class=\"hr\">\n"
2623 "<h3>&gt; General process information</h3>\n"
2624 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002625 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002626 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2627 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2628 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002629 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002630 "Running tasks: %d/%d; idle = %d %%<br>\n"
2631 "</td><td align=\"center\" nowrap>\n"
2632 "<table class=\"lgd\"><tr>\n"
2633 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2634 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2635 "</tr><tr>\n"
2636 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2637 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2638 "</tr><tr>\n"
2639 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2640 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2641 "</tr><tr>\n"
2642 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2643 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2644 "</tr><tr>\n"
2645 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2646 "</tr><tr>\n"
2647 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2648 "</tr></table>\n"
2649 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2650 "</td>"
2651 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2652 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2653 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002654 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2655 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2656 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2657 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2658 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002659 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002660 up / 86400, (up % 86400) / 3600,
2661 (up % 3600) / 60, (up % 60),
2662 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2663 global.rlimit_memmax ? " MB" : "",
2664 global.rlimit_nofile,
2665 global.maxsock, global.maxconn, global.maxpipes,
2666 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002667 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2668 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002669 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002670 );
2671
2672 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002673 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002674 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2675
2676 chunk_appendf(&trash,
2677 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2678 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2679 STAT_SCOPE_TXT_MAXLEN);
2680
2681 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2682 scope_txt[0] = 0;
2683 if (appctx->ctx.stats.scope_len) {
2684 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002685 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002686 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2687 }
2688
2689 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2690 chunk_appendf(&trash,
2691 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2692 uri->uri_prefix,
2693 "",
2694 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2695 scope_txt);
2696 else
2697 chunk_appendf(&trash,
2698 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2699 uri->uri_prefix,
2700 ";up",
2701 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2702 scope_txt);
2703
2704 if (uri->refresh > 0) {
2705 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2706 chunk_appendf(&trash,
2707 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2708 uri->uri_prefix,
2709 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2710 "",
2711 scope_txt);
2712 else
2713 chunk_appendf(&trash,
2714 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2715 uri->uri_prefix,
2716 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2717 ";norefresh",
2718 scope_txt);
2719 }
2720
2721 chunk_appendf(&trash,
2722 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2723 uri->uri_prefix,
2724 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2725 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2726 scope_txt);
2727
2728 chunk_appendf(&trash,
2729 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2730 uri->uri_prefix,
2731 (uri->refresh > 0) ? ";norefresh" : "",
2732 scope_txt);
2733
2734 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002735 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2736 uri->uri_prefix,
2737 (uri->refresh > 0) ? ";norefresh" : "",
2738 scope_txt, uri->uri_prefix);
2739
2740 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002741 "</ul></td>"
2742 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2743 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2744 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2745 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2746 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2747 "</ul>"
2748 "</td>"
2749 "</tr></table>\n"
2750 ""
2751 );
2752
2753 if (appctx->ctx.stats.st_code) {
2754 switch (appctx->ctx.stats.st_code) {
2755 case STAT_STATUS_DONE:
2756 chunk_appendf(&trash,
2757 "<p><div class=active_up>"
2758 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2759 "Action processed successfully."
2760 "</div>\n", uri->uri_prefix,
2761 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2762 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2763 scope_txt);
2764 break;
2765 case STAT_STATUS_NONE:
2766 chunk_appendf(&trash,
2767 "<p><div class=active_going_down>"
2768 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2769 "Nothing has changed."
2770 "</div>\n", uri->uri_prefix,
2771 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2772 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2773 scope_txt);
2774 break;
2775 case STAT_STATUS_PART:
2776 chunk_appendf(&trash,
2777 "<p><div class=active_going_down>"
2778 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2779 "Action partially processed.<br>"
2780 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2781 "</div>\n", uri->uri_prefix,
2782 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2783 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2784 scope_txt);
2785 break;
2786 case STAT_STATUS_ERRP:
2787 chunk_appendf(&trash,
2788 "<p><div class=active_down>"
2789 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2790 "Action not processed because of invalid parameters."
2791 "<ul>"
2792 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002793 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002794 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2795 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2796 "</ul>"
2797 "</div>\n", uri->uri_prefix,
2798 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2799 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2800 scope_txt);
2801 break;
2802 case STAT_STATUS_EXCD:
2803 chunk_appendf(&trash,
2804 "<p><div class=active_down>"
2805 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2806 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2807 "You should retry with less servers at a time.</b>"
2808 "</div>\n", uri->uri_prefix,
2809 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2810 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2811 scope_txt);
2812 break;
2813 case STAT_STATUS_DENY:
2814 chunk_appendf(&trash,
2815 "<p><div class=active_down>"
2816 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2817 "<b>Action denied.</b>"
2818 "</div>\n", uri->uri_prefix,
2819 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2820 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2821 scope_txt);
2822 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002823 case STAT_STATUS_IVAL:
2824 chunk_appendf(&trash,
2825 "<p><div class=active_down>"
2826 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2827 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2828 "</div>\n", uri->uri_prefix,
2829 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2830 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2831 scope_txt);
2832 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002833 default:
2834 chunk_appendf(&trash,
2835 "<p><div class=active_no_check>"
2836 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2837 "Unexpected result."
2838 "</div>\n", uri->uri_prefix,
2839 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2840 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2841 scope_txt);
2842 }
2843 chunk_appendf(&trash, "<p>\n");
2844 }
2845}
2846
2847/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2848 * for clearing the trash if needed.
2849 */
2850static void stats_dump_html_end()
2851{
2852 chunk_appendf(&trash, "</body></html>\n");
2853}
2854
Simon Horman05ee2132017-01-04 09:37:25 +01002855/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2856 * for clearing it if needed.
2857 */
2858static void stats_dump_json_header()
2859{
2860 chunk_strcat(&trash, "[");
2861}
2862
2863
2864/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2865 * for clearing the trash if needed.
2866 */
2867static void stats_dump_json_end()
2868{
2869 chunk_strcat(&trash, "]");
2870}
2871
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002872/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
2873 * a pointer to the current server/listener.
2874 */
2875static int stats_dump_proxies(struct stream_interface *si,
2876 struct htx *htx,
2877 struct uri_auth *uri)
2878{
2879 struct appctx *appctx = __objt_appctx(si->end);
2880 struct channel *rep = si_ic(si);
2881 struct proxy *px;
2882
2883 /* dump proxies */
2884 while (appctx->ctx.stats.obj1) {
2885 if (htx) {
2886 if (htx_almost_full(htx))
2887 goto full;
2888 }
2889 else {
2890 if (buffer_almost_full(&rep->buf))
2891 goto full;
2892 }
2893
2894 px = appctx->ctx.stats.obj1;
2895 /* skip the disabled proxies, global frontend and non-networked ones */
2896 if (px->state != PR_STSTOPPED && px->uuid > 0
2897 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
2898
2899 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
2900 return 0;
2901 }
2902
2903 appctx->ctx.stats.obj1 = px->next;
2904 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2905 }
2906
2907 return 1;
2908
2909 full:
2910 si_rx_room_blk(si);
2911 return 0;
2912}
2913
William Lallemand74c24fb2016-11-21 17:18:36 +01002914/* This function dumps statistics onto the stream interface's read buffer in
2915 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2916 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2917 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2918 * and the stream must be closed, or -1 in case of any error. This function is
2919 * used by both the CLI and the HTTP handlers.
2920 */
Christopher Fauletef779222018-10-31 08:47:01 +01002921static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2922 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002923{
2924 struct appctx *appctx = __objt_appctx(si->end);
2925 struct channel *rep = si_ic(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002926
2927 chunk_reset(&trash);
2928
2929 switch (appctx->st2) {
2930 case STAT_ST_INIT:
2931 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2932 /* fall through */
2933
2934 case STAT_ST_HEAD:
2935 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002936 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002937 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2938 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002939 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002940 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002941 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02002942 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01002943
Christopher Fauletef779222018-10-31 08:47:01 +01002944 if (!stats_putchk(rep, htx, &trash))
2945 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002946
Christopher Faulet6338a082019-09-09 15:50:54 +02002947 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2948 appctx->st2 = STAT_ST_FIN;
2949 return 1;
2950 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002951 appctx->st2 = STAT_ST_INFO;
2952 /* fall through */
2953
2954 case STAT_ST_INFO:
2955 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2956 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002957 if (!stats_putchk(rep, htx, &trash))
2958 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002959 }
2960
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002961 appctx->ctx.stats.obj1 = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002962 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2963 appctx->st2 = STAT_ST_LIST;
2964 /* fall through */
2965
2966 case STAT_ST_LIST:
2967 /* dump proxies */
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002968 if (!stats_dump_proxies(si, htx, uri))
2969 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002970
2971 appctx->st2 = STAT_ST_END;
2972 /* fall through */
2973
2974 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002975 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2976 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2977 stats_dump_html_end();
2978 else
2979 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002980 if (!stats_putchk(rep, htx, &trash))
2981 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002982 }
2983
2984 appctx->st2 = STAT_ST_FIN;
2985 /* fall through */
2986
2987 case STAT_ST_FIN:
2988 return 1;
2989
2990 default:
2991 /* unknown state ! */
2992 appctx->st2 = STAT_ST_FIN;
2993 return -1;
2994 }
Christopher Fauletef779222018-10-31 08:47:01 +01002995
2996 full:
2997 si_rx_room_blk(si);
2998 return 0;
2999
William Lallemand74c24fb2016-11-21 17:18:36 +01003000}
3001
3002/* We reached the stats page through a POST request. The appctx is
3003 * expected to have already been allocated by the caller.
3004 * Parse the posted data and enable/disable servers if necessary.
3005 * Returns 1 if request was parsed or zero if it needs more data.
3006 */
3007static int stats_process_http_post(struct stream_interface *si)
3008{
3009 struct stream *s = si_strm(si);
3010 struct appctx *appctx = objt_appctx(si->end);
3011
3012 struct proxy *px = NULL;
3013 struct server *sv = NULL;
3014
3015 char key[LINESIZE];
3016 int action = ST_ADM_ACTION_NONE;
3017 int reprocess = 0;
3018
3019 int total_servers = 0;
3020 int altered_servers = 0;
3021
3022 char *first_param, *cur_param, *next_param, *end_params;
3023 char *st_cur_param = NULL;
3024 char *st_next_param = NULL;
3025
Christopher Fauleta3618372018-12-13 21:59:56 +01003026 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003027
Christopher Fauletb7f88902019-07-15 21:56:43 +02003028 struct htx *htx = htxbuf(&s->req.buf);
3029 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003030
Christopher Fauletb7f88902019-07-15 21:56:43 +02003031 /* we need more data */
3032 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3033 /* check if we can receive more */
3034 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3035 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3036 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003037 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003038 goto wait;
3039 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003040
Christopher Fauletb7f88902019-07-15 21:56:43 +02003041 /* The request was fully received. Copy data */
3042 blk = htx_get_head_blk(htx);
3043 while (blk) {
3044 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003045
Christopher Fauletb7f88902019-07-15 21:56:43 +02003046 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3047 break;
3048 if (type == HTX_BLK_DATA) {
3049 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003050
Christopher Fauletb7f88902019-07-15 21:56:43 +02003051 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003052 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3053 goto out;
3054 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003055 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003056 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003057 }
3058
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003059 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003060 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003061 cur_param = next_param = end_params;
3062 *end_params = '\0';
3063
3064 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3065
3066 /*
3067 * Parse the parameters in reverse order to only store the last value.
3068 * From the html form, the backend and the action are at the end.
3069 */
3070 while (cur_param > first_param) {
3071 char *value;
3072 int poffset, plen;
3073
3074 cur_param--;
3075
3076 if ((*cur_param == '&') || (cur_param == first_param)) {
3077 reprocess_servers:
3078 /* Parse the key */
3079 poffset = (cur_param != first_param ? 1 : 0);
3080 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3081 if ((plen > 0) && (plen <= sizeof(key))) {
3082 strncpy(key, cur_param + poffset, plen);
3083 key[plen - 1] = '\0';
3084 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003085 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003086 goto out;
3087 }
3088
3089 /* Parse the value */
3090 value = key;
3091 while (*value != '\0' && *value != '=') {
3092 value++;
3093 }
3094 if (*value == '=') {
3095 /* Ok, a value is found, we can mark the end of the key */
3096 *value++ = '\0';
3097 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003098 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003099 break;
3100
3101 /* Now we can check the key to see what to do */
3102 if (!px && (strcmp(key, "b") == 0)) {
3103 if ((px = proxy_be_by_name(value)) == NULL) {
3104 /* the backend name is unknown or ambiguous (duplicate names) */
3105 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3106 goto out;
3107 }
3108 }
3109 else if (!action && (strcmp(key, "action") == 0)) {
3110 if (strcmp(value, "ready") == 0) {
3111 action = ST_ADM_ACTION_READY;
3112 }
3113 else if (strcmp(value, "drain") == 0) {
3114 action = ST_ADM_ACTION_DRAIN;
3115 }
3116 else if (strcmp(value, "maint") == 0) {
3117 action = ST_ADM_ACTION_MAINT;
3118 }
3119 else if (strcmp(value, "shutdown") == 0) {
3120 action = ST_ADM_ACTION_SHUTDOWN;
3121 }
3122 else if (strcmp(value, "dhlth") == 0) {
3123 action = ST_ADM_ACTION_DHLTH;
3124 }
3125 else if (strcmp(value, "ehlth") == 0) {
3126 action = ST_ADM_ACTION_EHLTH;
3127 }
3128 else if (strcmp(value, "hrunn") == 0) {
3129 action = ST_ADM_ACTION_HRUNN;
3130 }
3131 else if (strcmp(value, "hnolb") == 0) {
3132 action = ST_ADM_ACTION_HNOLB;
3133 }
3134 else if (strcmp(value, "hdown") == 0) {
3135 action = ST_ADM_ACTION_HDOWN;
3136 }
3137 else if (strcmp(value, "dagent") == 0) {
3138 action = ST_ADM_ACTION_DAGENT;
3139 }
3140 else if (strcmp(value, "eagent") == 0) {
3141 action = ST_ADM_ACTION_EAGENT;
3142 }
3143 else if (strcmp(value, "arunn") == 0) {
3144 action = ST_ADM_ACTION_ARUNN;
3145 }
3146 else if (strcmp(value, "adown") == 0) {
3147 action = ST_ADM_ACTION_ADOWN;
3148 }
3149 /* else these are the old supported methods */
3150 else if (strcmp(value, "disable") == 0) {
3151 action = ST_ADM_ACTION_DISABLE;
3152 }
3153 else if (strcmp(value, "enable") == 0) {
3154 action = ST_ADM_ACTION_ENABLE;
3155 }
3156 else if (strcmp(value, "stop") == 0) {
3157 action = ST_ADM_ACTION_STOP;
3158 }
3159 else if (strcmp(value, "start") == 0) {
3160 action = ST_ADM_ACTION_START;
3161 }
3162 else {
3163 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3164 goto out;
3165 }
3166 }
3167 else if (strcmp(key, "s") == 0) {
3168 if (!(px && action)) {
3169 /*
3170 * Indicates that we'll need to reprocess the parameters
3171 * as soon as backend and action are known
3172 */
3173 if (!reprocess) {
3174 st_cur_param = cur_param;
3175 st_next_param = next_param;
3176 }
3177 reprocess = 1;
3178 }
3179 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003180 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003181 switch (action) {
3182 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003183 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003184 altered_servers++;
3185 total_servers++;
3186 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3187 }
3188 break;
3189 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003190 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003191 altered_servers++;
3192 total_servers++;
3193 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3194 }
3195 break;
3196 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003197 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003198 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3199 altered_servers++;
3200 total_servers++;
3201 }
3202 break;
3203 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003204 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003205 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3206 altered_servers++;
3207 total_servers++;
3208 }
3209 break;
3210 case ST_ADM_ACTION_DHLTH:
3211 if (sv->check.state & CHK_ST_CONFIGURED) {
3212 sv->check.state &= ~CHK_ST_ENABLED;
3213 altered_servers++;
3214 total_servers++;
3215 }
3216 break;
3217 case ST_ADM_ACTION_EHLTH:
3218 if (sv->check.state & CHK_ST_CONFIGURED) {
3219 sv->check.state |= CHK_ST_ENABLED;
3220 altered_servers++;
3221 total_servers++;
3222 }
3223 break;
3224 case ST_ADM_ACTION_HRUNN:
3225 if (!(sv->track)) {
3226 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003227 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003228 altered_servers++;
3229 total_servers++;
3230 }
3231 break;
3232 case ST_ADM_ACTION_HNOLB:
3233 if (!(sv->track)) {
3234 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003235 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003236 altered_servers++;
3237 total_servers++;
3238 }
3239 break;
3240 case ST_ADM_ACTION_HDOWN:
3241 if (!(sv->track)) {
3242 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003243 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003244 altered_servers++;
3245 total_servers++;
3246 }
3247 break;
3248 case ST_ADM_ACTION_DAGENT:
3249 if (sv->agent.state & CHK_ST_CONFIGURED) {
3250 sv->agent.state &= ~CHK_ST_ENABLED;
3251 altered_servers++;
3252 total_servers++;
3253 }
3254 break;
3255 case ST_ADM_ACTION_EAGENT:
3256 if (sv->agent.state & CHK_ST_CONFIGURED) {
3257 sv->agent.state |= CHK_ST_ENABLED;
3258 altered_servers++;
3259 total_servers++;
3260 }
3261 break;
3262 case ST_ADM_ACTION_ARUNN:
3263 if (sv->agent.state & CHK_ST_ENABLED) {
3264 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003265 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003266 altered_servers++;
3267 total_servers++;
3268 }
3269 break;
3270 case ST_ADM_ACTION_ADOWN:
3271 if (sv->agent.state & CHK_ST_ENABLED) {
3272 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003273 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003274 altered_servers++;
3275 total_servers++;
3276 }
3277 break;
3278 case ST_ADM_ACTION_READY:
3279 srv_adm_set_ready(sv);
3280 altered_servers++;
3281 total_servers++;
3282 break;
3283 case ST_ADM_ACTION_DRAIN:
3284 srv_adm_set_drain(sv);
3285 altered_servers++;
3286 total_servers++;
3287 break;
3288 case ST_ADM_ACTION_MAINT:
3289 srv_adm_set_maint(sv);
3290 altered_servers++;
3291 total_servers++;
3292 break;
3293 case ST_ADM_ACTION_SHUTDOWN:
3294 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003295 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003296 altered_servers++;
3297 total_servers++;
3298 }
3299 break;
3300 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003301 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003302 } else {
3303 /* the server name is unknown or ambiguous (duplicate names) */
3304 total_servers++;
3305 }
3306 }
3307 if (reprocess && px && action) {
3308 /* Now, we know the backend and the action chosen by the user.
3309 * We can safely restart from the first server parameter
3310 * to reprocess them
3311 */
3312 cur_param = st_cur_param;
3313 next_param = st_next_param;
3314 reprocess = 0;
3315 goto reprocess_servers;
3316 }
3317
3318 next_param = cur_param;
3319 }
3320 }
3321
3322 if (total_servers == 0) {
3323 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3324 }
3325 else if (altered_servers == 0) {
3326 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3327 }
3328 else if (altered_servers == total_servers) {
3329 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3330 }
3331 else {
3332 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3333 }
3334 out:
3335 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003336 wait:
3337 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3338 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003339}
3340
3341
Christopher Fauletb7f88902019-07-15 21:56:43 +02003342static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003343{
3344 struct stream *s = si_strm(si);
3345 struct uri_auth *uri = s->be->uri_auth;
3346 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003347 struct htx_sl *sl;
3348 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003349
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003350 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);
3351 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3352 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003353 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003354 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003355
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003356 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003357 goto full;
3358 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3359 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3360 goto full;
3361 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003362 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3363 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3364 goto full;
3365 }
Christopher Fauletef779222018-10-31 08:47:01 +01003366 else {
3367 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3368 goto full;
3369 }
3370
3371 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3372 const char *refresh = U2A(uri->refresh);
3373 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3374 goto full;
3375 }
3376
3377 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3378 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3379 goto full;
3380 }
3381
3382 if (!htx_add_endof(htx, HTX_BLK_EOH))
3383 goto full;
3384
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003385 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003386 return 1;
3387
3388 full:
3389 htx_reset(htx);
3390 si_rx_room_blk(si);
3391 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003392}
3393
Christopher Fauletef779222018-10-31 08:47:01 +01003394
Christopher Fauletb7f88902019-07-15 21:56:43 +02003395static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003396{
3397 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3398 struct stream *s = si_strm(si);
3399 struct uri_auth *uri = s->be->uri_auth;
3400 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003401 struct htx_sl *sl;
3402 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003403
3404 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3405 scope_txt[0] = 0;
3406 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003407 const char *scope_ptr = stats_scope_ptr(appctx, si);
3408
Christopher Fauletef779222018-10-31 08:47:01 +01003409 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003410 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003411 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3412 }
3413
3414 /* We don't want to land on the posted stats page because a refresh will
3415 * repost the data. We don't want this to happen on accident so we redirect
3416 * the browse to the stats page with a GET.
3417 */
3418 chunk_printf(&trash, "%s;st=%s%s%s%s",
3419 uri->uri_prefix,
3420 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3421 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3422 stat_status_codes[appctx->ctx.stats.st_code]) ?
3423 stat_status_codes[appctx->ctx.stats.st_code] :
3424 stat_status_codes[STAT_STATUS_UNKN],
3425 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3426 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3427 scope_txt);
3428
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003429 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3430 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3431 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003432 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003433 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003434
3435 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003436 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3437 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3438 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3439 goto full;
3440
3441 if (!htx_add_endof(htx, HTX_BLK_EOH))
3442 goto full;
3443
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003444 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003445 return 1;
3446
3447full:
3448 htx_reset(htx);
3449 si_rx_room_blk(si);
3450 return 0;
3451}
William Lallemand74c24fb2016-11-21 17:18:36 +01003452
Simon Horman05ee2132017-01-04 09:37:25 +01003453
William Lallemand74c24fb2016-11-21 17:18:36 +01003454/* This I/O handler runs as an applet embedded in a stream interface. It is
3455 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3456 * appctx->st0 contains the operation in progress (dump, done). The handler
3457 * automatically unregisters itself once transfer is complete.
3458 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003459static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003460{
3461 struct stream_interface *si = appctx->owner;
3462 struct stream *s = si_strm(si);
3463 struct channel *req = si_oc(si);
3464 struct channel *res = si_ic(si);
3465 struct htx *req_htx, *res_htx;
3466
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003467 /* only proxy stats are available via http */
3468 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3469
Christopher Fauletef779222018-10-31 08:47:01 +01003470 res_htx = htx_from_buf(&res->buf);
3471
3472 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3473 goto out;
3474
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003475 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003476 if (!b_size(&res->buf)) {
3477 si_rx_room_blk(si);
3478 goto out;
3479 }
3480
3481 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003482 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3483 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003484
3485 /* all states are processed in sequence */
3486 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003487 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003488 if (s->txn->meth == HTTP_METH_HEAD)
3489 appctx->st0 = STAT_HTTP_DONE;
3490 else
3491 appctx->st0 = STAT_HTTP_DUMP;
3492 }
3493 }
3494
3495 if (appctx->st0 == STAT_HTTP_DUMP) {
3496 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3497 appctx->st0 = STAT_HTTP_DONE;
3498 }
3499
3500 if (appctx->st0 == STAT_HTTP_POST) {
3501 if (stats_process_http_post(si))
3502 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003503 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003504 appctx->st0 = STAT_HTTP_DONE;
3505 }
3506
3507 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003508 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003509 appctx->st0 = STAT_HTTP_DONE;
3510 }
3511
3512 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003513 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003514 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003515 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3516 si_rx_room_blk(si);
3517 goto out;
3518 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003519 channel_add_input(&s->res, 1);
3520 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003521 }
3522
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003523 if (appctx->st0 == STAT_HTTP_END) {
3524 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003525 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003526 si_shutr(si);
3527 }
3528
3529 /* eat the whole request */
3530 if (co_data(req)) {
3531 req_htx = htx_from_buf(&req->buf);
3532 co_htx_skip(req, req_htx, co_data(req));
3533 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003534 }
3535 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003536
Christopher Fauletef779222018-10-31 08:47:01 +01003537 out:
3538 /* we have left the request in the buffer for the case where we
3539 * process a POST, and this automatically re-enables activity on
3540 * read. It's better to indicate that we want to stop reading when
3541 * we're sending, so that we know there's at most one direction
3542 * deciding to wake the applet up. It saves it from looping when
3543 * emitting large blocks into small TCP windows.
3544 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003545 htx_to_buf(res_htx, &res->buf);
3546 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003547 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003548}
3549
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003550/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003551static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003552 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003553{
3554 int field;
3555
3556 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3557 if (!field_format(info, field))
3558 continue;
3559
Willy Tarreaueaa55372019-10-09 07:39:11 +02003560 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003561 return 0;
3562 if (!stats_emit_raw_data_field(out, &info[field]))
3563 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003564 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3565 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003566 if (!chunk_strcat(out, "\n"))
3567 return 0;
3568 }
3569 return 1;
3570}
3571
3572/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003573static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003574 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003575{
3576 int field;
3577
3578 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3579 if (!field_format(info, field))
3580 continue;
3581
Willy Tarreaueaa55372019-10-09 07:39:11 +02003582 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 +01003583 return 0;
3584 if (!stats_emit_field_tags(out, &info[field], ':'))
3585 return 0;
3586 if (!stats_emit_typed_data_field(out, &info[field]))
3587 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003588 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3589 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003590 if (!chunk_strcat(out, "\n"))
3591 return 0;
3592 }
3593 return 1;
3594}
3595
3596/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003597 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003598 * INF_TOTAL_FIELDS. If this length is less then this value, the
3599 * function returns 0, otherwise, it returns 1.
3600 */
3601int stats_fill_info(struct field *info, int len)
3602{
3603 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003604 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003605
3606#ifdef USE_OPENSSL
3607 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3608 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3609 int ssl_reuse = 0;
3610
3611 if (ssl_key_rate < ssl_sess_rate) {
3612 /* count the ssl reuse ratio and avoid overflows in both directions */
3613 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3614 }
3615#endif
3616
3617 if (len < INF_TOTAL_FIELDS)
3618 return 0;
3619
3620 chunk_reset(out);
3621 memset(info, 0, sizeof(*info) * len);
3622
3623 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003624 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3625 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003626
Yves Lafon95317282018-02-26 11:10:37 +01003627 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003628 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3629 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3630 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3631
3632 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3633 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3634
3635 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3636 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3637 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3638 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3639 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3640 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3641 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3642 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3643 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3644 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3645 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3646 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3647#ifdef USE_OPENSSL
3648 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3649 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3650 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3651#endif
3652 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3653 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3654 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3655 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3656 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3657 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3658 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3659 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3660 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3661
3662#ifdef USE_OPENSSL
3663 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3664 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3665 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3666 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3667 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3668 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3669 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3670 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3671 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3672 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3673#endif
3674 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3675 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3676 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3677#ifdef USE_ZLIB
3678 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3679 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3680#endif
3681 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003682 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003683 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003684 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3685 if (global.desc)
3686 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003687 info[INF_STOPPING] = mkf_u32(0, stopping);
3688 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003689 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003690 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003691 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003692 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003693 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003694 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003695 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003696 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003697 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003698 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 +02003699 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003700 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003701
3702 return 1;
3703}
3704
3705/* This function dumps information onto the stream interface's read buffer.
3706 * It returns 0 as long as it does not complete, non-zero upon completion.
3707 * No state is used.
3708 */
3709static int stats_dump_info_to_buffer(struct stream_interface *si)
3710{
3711 struct appctx *appctx = __objt_appctx(si->end);
3712
3713 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3714 return 0;
3715
3716 chunk_reset(&trash);
3717
3718 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003719 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003720 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003721 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003722 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003723 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003724
Willy Tarreau06d80a92017-10-19 14:32:15 +02003725 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003726 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003727 return 0;
3728 }
3729
3730 return 1;
3731}
3732
Simon Horman6f6bb382017-01-04 09:37:26 +01003733/* This function dumps the schema onto the stream interface's read buffer.
3734 * It returns 0 as long as it does not complete, non-zero upon completion.
3735 * No state is used.
3736 *
3737 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3738 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3739 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003740static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003741{
3742
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003743 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003744
3745 chunk_strcat(out,
3746 "{"
3747 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3748 "\"oneOf\":["
3749 "{"
3750 "\"title\":\"Info\","
3751 "\"type\":\"array\","
3752 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003753 "\"title\":\"InfoItem\","
3754 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003755 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003756 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3757 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3758 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3759 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3760 "},"
3761 "\"required\":[\"field\",\"processNum\",\"tags\","
3762 "\"value\"]"
3763 "}"
3764 "},"
3765 "{"
3766 "\"title\":\"Stat\","
3767 "\"type\":\"array\","
3768 "\"items\":{"
3769 "\"title\":\"InfoItem\","
3770 "\"type\":\"object\","
3771 "\"properties\":{"
3772 "\"objType\":{"
3773 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3774 "\"Server\",\"Unknown\"]"
3775 "},"
3776 "\"proxyId\":{"
3777 "\"type\":\"integer\","
3778 "\"minimum\":0"
3779 "},"
3780 "\"id\":{"
3781 "\"type\":\"integer\","
3782 "\"minimum\":0"
3783 "},"
3784 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3785 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3786 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3787 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3788 "},"
3789 "\"required\":[\"objType\",\"proxyId\",\"id\","
3790 "\"field\",\"processNum\",\"tags\","
3791 "\"value\"]"
3792 "}"
3793 "},"
3794 "{"
3795 "\"title\":\"Error\","
3796 "\"type\":\"object\","
3797 "\"properties\":{"
3798 "\"errorStr\":{"
3799 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003800 "}"
3801 "},"
3802 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01003803 "}"
3804 "],"
3805 "\"definitions\":{"
3806 "\"field\":{"
3807 "\"type\":\"object\","
3808 "\"pos\":{"
3809 "\"type\":\"integer\","
3810 "\"minimum\":0"
3811 "},"
3812 "\"name\":{"
3813 "\"type\":\"string\""
3814 "},"
3815 "\"required\":[\"pos\",\"name\"]"
3816 "},"
3817 "\"processNum\":{"
3818 "\"type\":\"integer\","
3819 "\"minimum\":1"
3820 "},"
3821 "\"tags\":{"
3822 "\"type\":\"object\","
3823 "\"origin\":{"
3824 "\"type\":\"string\","
3825 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3826 "\"Config\",\"Product\",\"Unknown\"]"
3827 "},"
3828 "\"nature\":{"
3829 "\"type\":\"string\","
3830 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3831 "\"Rate\",\"Counter\",\"Duration\","
3832 "\"Age\",\"Time\",\"Name\",\"Output\","
3833 "\"Avg\", \"Unknown\"]"
3834 "},"
3835 "\"scope\":{"
3836 "\"type\":\"string\","
3837 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3838 "\"System\",\"Unknown\"]"
3839 "},"
3840 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3841 "},"
3842 "\"typedValue\":{"
3843 "\"type\":\"object\","
3844 "\"oneOf\":["
3845 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3846 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3847 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3848 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3849 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3850 "],"
3851 "\"definitions\":{"
3852 "\"s32Value\":{"
3853 "\"properties\":{"
3854 "\"type\":{"
3855 "\"type\":\"string\","
3856 "\"enum\":[\"s32\"]"
3857 "},"
3858 "\"value\":{"
3859 "\"type\":\"integer\","
3860 "\"minimum\":-2147483648,"
3861 "\"maximum\":2147483647"
3862 "}"
3863 "},"
3864 "\"required\":[\"type\",\"value\"]"
3865 "},"
3866 "\"s64Value\":{"
3867 "\"properties\":{"
3868 "\"type\":{"
3869 "\"type\":\"string\","
3870 "\"enum\":[\"s64\"]"
3871 "},"
3872 "\"value\":{"
3873 "\"type\":\"integer\","
3874 "\"minimum\":-9007199254740991,"
3875 "\"maximum\":9007199254740991"
3876 "}"
3877 "},"
3878 "\"required\":[\"type\",\"value\"]"
3879 "},"
3880 "\"u32Value\":{"
3881 "\"properties\":{"
3882 "\"type\":{"
3883 "\"type\":\"string\","
3884 "\"enum\":[\"u32\"]"
3885 "},"
3886 "\"value\":{"
3887 "\"type\":\"integer\","
3888 "\"minimum\":0,"
3889 "\"maximum\":4294967295"
3890 "}"
3891 "},"
3892 "\"required\":[\"type\",\"value\"]"
3893 "},"
3894 "\"u64Value\":{"
3895 "\"properties\":{"
3896 "\"type\":{"
3897 "\"type\":\"string\","
3898 "\"enum\":[\"u64\"]"
3899 "},"
3900 "\"value\":{"
3901 "\"type\":\"integer\","
3902 "\"minimum\":0,"
3903 "\"maximum\":9007199254740991"
3904 "}"
3905 "},"
3906 "\"required\":[\"type\",\"value\"]"
3907 "},"
3908 "\"strValue\":{"
3909 "\"properties\":{"
3910 "\"type\":{"
3911 "\"type\":\"string\","
3912 "\"enum\":[\"str\"]"
3913 "},"
3914 "\"value\":{\"type\":\"string\"}"
3915 "},"
3916 "\"required\":[\"type\",\"value\"]"
3917 "},"
3918 "\"unknownValue\":{"
3919 "\"properties\":{"
3920 "\"type\":{"
3921 "\"type\":\"integer\","
3922 "\"minimum\":0"
3923 "},"
3924 "\"value\":{"
3925 "\"type\":\"string\","
3926 "\"enum\":[\"unknown\"]"
3927 "}"
3928 "},"
3929 "\"required\":[\"type\",\"value\"]"
3930 "}"
3931 "}"
3932 "}"
3933 "}"
3934 "}");
3935
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003936 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003937 chunk_reset(out);
3938 chunk_appendf(out,
3939 "{\"errorStr\":\"output buffer too short\"}");
3940 }
3941}
3942
3943/* This function dumps the schema onto the stream interface's read buffer.
3944 * It returns 0 as long as it does not complete, non-zero upon completion.
3945 * No state is used.
3946 */
3947static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3948{
3949 chunk_reset(&trash);
3950
3951 stats_dump_json_schema(&trash);
3952
Willy Tarreau06d80a92017-10-19 14:32:15 +02003953 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003954 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003955 return 0;
3956 }
3957
3958 return 1;
3959}
3960
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003961static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003962{
3963 struct proxy *px;
3964 struct server *sv;
3965 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02003966 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003967 int clrall = 0;
3968
3969 if (strcmp(args[2], "all") == 0)
3970 clrall = 1;
3971
3972 /* check permissions */
3973 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3974 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3975 return 1;
3976
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003977 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003978 if (clrall) {
3979 memset(&px->be_counters, 0, sizeof(px->be_counters));
3980 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3981 }
3982 else {
3983 px->be_counters.conn_max = 0;
3984 px->be_counters.p.http.rps_max = 0;
3985 px->be_counters.sps_max = 0;
3986 px->be_counters.cps_max = 0;
3987 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003988 px->be_counters.qtime_max = 0;
3989 px->be_counters.ctime_max = 0;
3990 px->be_counters.dtime_max = 0;
3991 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003992
3993 px->fe_counters.conn_max = 0;
3994 px->fe_counters.p.http.rps_max = 0;
3995 px->fe_counters.sps_max = 0;
3996 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003997 }
3998
3999 for (sv = px->srv; sv; sv = sv->next)
4000 if (clrall)
4001 memset(&sv->counters, 0, sizeof(sv->counters));
4002 else {
4003 sv->counters.cur_sess_max = 0;
4004 sv->counters.nbpend_max = 0;
4005 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004006 sv->counters.qtime_max = 0;
4007 sv->counters.ctime_max = 0;
4008 sv->counters.dtime_max = 0;
4009 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004010 }
4011
4012 list_for_each_entry(li, &px->conf.listeners, by_fe)
4013 if (li->counters) {
4014 if (clrall)
4015 memset(li->counters, 0, sizeof(*li->counters));
4016 else
4017 li->counters->conn_max = 0;
4018 }
4019 }
4020
4021 global.cps_max = 0;
4022 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004023 global.ssl_max = 0;
4024 global.ssl_fe_keys_max = 0;
4025 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004026
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004027 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4028 if (!mod->clearable && !clrall)
4029 continue;
4030
4031 for (px = proxies_list; px; px = px->next) {
4032 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4033
4034 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4035 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4036 mod,
4037 mod->counters,
4038 mod->counters_size);
4039 }
4040
4041 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4042 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4043 mod,
4044 mod->counters,
4045 mod->counters_size);
4046 }
4047
4048 if (mod_cap & STATS_PX_CAP_SRV) {
4049 for (sv = px->srv; sv; sv = sv->next) {
4050 EXTRA_COUNTERS_INIT(sv->extra_counters,
4051 mod,
4052 mod->counters,
4053 mod->counters_size);
4054 }
4055 }
4056
4057 if (mod_cap & STATS_PX_CAP_LI) {
4058 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4059 EXTRA_COUNTERS_INIT(li->extra_counters,
4060 mod,
4061 mod->counters,
4062 mod->counters_size);
4063 }
4064 }
4065 }
4066 }
4067
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004068 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004069 return 1;
4070}
4071
4072
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004073static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004074{
Willy Tarreau2f397382019-10-09 11:43:59 +02004075 int arg = 2;
4076
Willy Tarreaud25fc792016-12-16 12:33:47 +01004077 appctx->ctx.stats.scope_str = 0;
4078 appctx->ctx.stats.scope_len = 0;
4079 appctx->ctx.stats.flags = 0;
4080
Willy Tarreau2f397382019-10-09 11:43:59 +02004081 while (*args[arg]) {
4082 if (strcmp(args[arg], "typed") == 0)
4083 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4084 else if (strcmp(args[arg], "json") == 0)
4085 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4086 else if (strcmp(args[arg], "desc") == 0)
4087 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4088 arg++;
4089 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004090 return 0;
4091}
4092
4093
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004094static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004095{
Willy Tarreau2f397382019-10-09 11:43:59 +02004096 int arg = 2;
4097
Willy Tarreaud25fc792016-12-16 12:33:47 +01004098 appctx->ctx.stats.scope_str = 0;
4099 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004100 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4101
4102 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
4103 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004104
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004105 /* proxy is the default domain */
4106 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
4107 if (!strcmp(args[arg], "domain")) {
4108 ++args;
4109
4110 if (!strcmp(args[arg], "proxy"))
4111 ++args;
4112 else
4113 return cli_err(appctx, "Invalid statistics domain.\n");
4114 }
4115
4116 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4117 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004118 struct proxy *px;
4119
Willy Tarreau2f397382019-10-09 11:43:59 +02004120 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004121 if (px)
4122 appctx->ctx.stats.iid = px->uuid;
4123 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004124 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004125
Willy Tarreau9d008692019-08-09 11:21:01 +02004126 if (!appctx->ctx.stats.iid)
4127 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004128
Willy Tarreau2b812e22016-11-22 16:18:05 +01004129 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004130 appctx->ctx.stats.type = atoi(args[arg+1]);
4131 appctx->ctx.stats.sid = atoi(args[arg+2]);
4132 arg += 3;
4133 }
4134
4135 while (*args[arg]) {
4136 if (strcmp(args[arg], "typed") == 0)
4137 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4138 else if (strcmp(args[arg], "json") == 0)
4139 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4140 else if (strcmp(args[arg], "desc") == 0)
4141 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4142 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004143 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004144
Willy Tarreau2b812e22016-11-22 16:18:05 +01004145 return 0;
4146}
4147
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004148static int cli_io_handler_dump_info(struct appctx *appctx)
4149{
4150 return stats_dump_info_to_buffer(appctx->owner);
4151}
4152
Willy Tarreau2b812e22016-11-22 16:18:05 +01004153/* This I/O handler runs as an applet embedded in a stream interface. It is
4154 * used to send raw stats over a socket.
4155 */
4156static int cli_io_handler_dump_stat(struct appctx *appctx)
4157{
Christopher Fauletef779222018-10-31 08:47:01 +01004158 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004159}
4160
Simon Horman6f6bb382017-01-04 09:37:26 +01004161static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4162{
4163 return stats_dump_json_schema_to_buffer(appctx->owner);
4164}
4165
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004166static int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
4167 int type, int px_cap)
4168{
4169 struct stats_module *mod;
4170
4171 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
4172
4173 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4174 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4175 continue;
4176
4177 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
4178 }
4179
4180 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
4181
4182 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4183 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4184 continue;
4185
4186 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
4187 }
4188
4189 return 1;
4190
4191 alloc_failed:
4192 return 0;
4193}
4194
4195/* Initialize and allocate all extra counters for a proxy and its attached
4196 * servers/listeners with all already registered stats module
4197 */
4198int stats_allocate_proxy_counters(struct proxy *px)
4199{
4200 struct server *sv;
4201 struct listener *li;
4202
4203 if (px->cap & PR_CAP_FE) {
4204 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
4205 COUNTERS_FE,
4206 STATS_PX_CAP_FE)) {
4207 return 0;
4208 }
4209 }
4210
4211 if (px->cap & PR_CAP_BE) {
4212 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
4213 COUNTERS_BE,
4214 STATS_PX_CAP_BE)) {
4215 return 0;
4216 }
4217 }
4218
4219 for (sv = px->srv; sv; sv = sv->next) {
4220 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
4221 COUNTERS_SV,
4222 STATS_PX_CAP_SRV)) {
4223 return 0;
4224 }
4225 }
4226
4227 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4228 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
4229 COUNTERS_LI,
4230 STATS_PX_CAP_LI)) {
4231 return 0;
4232 }
4233 }
4234
4235 return 1;
4236}
4237
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004238void stats_register_module(struct stats_module *m)
4239{
4240 const uint8_t domain = stats_get_domain(m->domain_flags);
4241
4242 LIST_ADDQ(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004243 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004244}
4245
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004246static int allocate_stats_px_postcheck(void)
4247{
4248 struct stats_module *mod;
4249 size_t i = ST_F_TOTAL_FIELDS;
4250 int err_code = 0;
4251
4252 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
4253
4254 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
4255 if (!stat_f[STATS_DOMAIN_PROXY]) {
4256 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
4257 err_code |= ERR_ALERT | ERR_FATAL;
4258 return err_code;
4259 }
4260
4261 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
4262 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
4263
4264 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4265 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
4266 mod->stats,
4267 mod->stats_count * sizeof(struct name_desc));
4268 i += mod->stats_count;
4269 }
4270
4271 for (struct proxy *px = proxies_list; px; px = px->next) {
4272 if (!stats_allocate_proxy_counters(px)) {
4273 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
4274 err_code |= ERR_ALERT | ERR_FATAL;
4275 return err_code;
4276 }
4277 }
4278
4279 stat_l[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct field));
4280 if (!stat_l[STATS_DOMAIN_PROXY]) {
4281 ha_alert("stats: cannot allocate a line for proxy statistics\n");
4282 err_code |= ERR_ALERT | ERR_FATAL;
4283 return err_code;
4284 }
4285
4286 return err_code;
4287}
4288
4289REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
4290
Willy Tarreau2b812e22016-11-22 16:18:05 +01004291/* register cli keywords */
4292static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004293 { { "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 +02004294 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
4295 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server [desc|json|typed]*", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01004296 { { "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 +01004297 {{},}
4298}};
4299
Willy Tarreau0108d902018-11-25 19:14:37 +01004300INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4301
William Lallemand74c24fb2016-11-21 17:18:36 +01004302struct applet http_stats_applet = {
4303 .obj_type = OBJ_TYPE_APPLET,
4304 .name = "<STATS>", /* used for logging */
4305 .fct = http_stats_io_handler,
4306 .release = NULL,
4307};
4308
William Lallemand74c24fb2016-11-21 17:18:36 +01004309/*
4310 * Local variables:
4311 * c-indent-level: 8
4312 * c-basic-offset: 8
4313 * End:
4314 */