blob: 16e0c10fdffd597a8ec744b1b7528b4afff1e086 [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];
William Lallemand74c24fb2016-11-21 17:18:36 +0100257/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100258static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100259
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200260static inline uint8_t stats_get_domain(uint32_t domain)
261{
262 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
263}
264
Christopher Faulet6338a082019-09-09 15:50:54 +0200265static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100266
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200267int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100268{
269 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100270 if (chk->data >= channel_htx_recv_max(chn, htx))
271 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200272 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100273 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100274 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100275 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100276 }
277 else {
278 if (ci_putchk(chn, chk) == -1)
279 return 0;
280 }
281 return 1;
282}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100283
Christopher Fauleted7a0662019-01-14 11:07:34 +0100284static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
285{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200286 struct channel *req = si_oc(si);
287 struct htx *htx = htxbuf(&req->buf);
288 struct htx_blk *blk;
289 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100290
Christopher Fauletb7f88902019-07-15 21:56:43 +0200291 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100292 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200293 ALREADY_CHECKED(blk);
294 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
295 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100296}
297
William Lallemand74c24fb2016-11-21 17:18:36 +0100298/*
299 * http_stats_io_handler()
300 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
301 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100302 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100303 * -> stats_dump_html_head() // emits the HTML headers
304 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
305 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
306 * -> stats_dump_html_px_hdr()
307 * -> stats_dump_fe_stats()
308 * -> stats_dump_li_stats()
309 * -> stats_dump_sv_stats()
310 * -> stats_dump_be_stats()
311 * -> stats_dump_html_px_end()
312 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100313 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100314 */
315
316
William Lallemand74c24fb2016-11-21 17:18:36 +0100317/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
318 * for clearing it if needed.
319 * NOTE: Some tools happen to rely on the field position instead of its name,
320 * so please only append new fields at the end, never in the middle.
321 */
322static void stats_dump_csv_header()
323{
324 int field;
325
326 chunk_appendf(&trash, "# ");
327 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200328 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100329
330 chunk_appendf(&trash, "\n");
331}
332
333
334/* Emits a stats field without any surrounding element and properly encoded to
335 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
336 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200337int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100338{
339 switch (field_format(f, 0)) {
340 case FF_EMPTY: return 1;
341 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
342 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
343 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
344 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200345 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100346 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
347 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
348 }
349}
350
351/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
352 * output is supposed to be used on its own line. Returns non-zero on success, 0
353 * if the buffer is full.
354 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200355int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100356{
357 switch (field_format(f, 0)) {
358 case FF_EMPTY: return 1;
359 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
360 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
361 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
362 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200363 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100364 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
365 default: return chunk_appendf(out, "%08x:?", f->type);
366 }
367}
368
Simon Horman05ee2132017-01-04 09:37:25 +0100369/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
370 * the recommendation for interoperable integers in section 6 of RFC 7159.
371 */
372#define JSON_INT_MAX ((1ULL << 53) - 1)
373#define JSON_INT_MIN (0 - JSON_INT_MAX)
374
375/* Emits a stats field value and its type in JSON.
376 * Returns non-zero on success, 0 on error.
377 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200378int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100379{
380 int old_len;
381 char buf[20];
382 const char *type, *value = buf, *quote = "";
383
384 switch (field_format(f, 0)) {
385 case FF_EMPTY: return 1;
386 case FF_S32: type = "\"s32\"";
387 snprintf(buf, sizeof(buf), "%d", f->u.s32);
388 break;
389 case FF_U32: type = "\"u32\"";
390 snprintf(buf, sizeof(buf), "%u", f->u.u32);
391 break;
392 case FF_S64: type = "\"s64\"";
393 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
394 return 0;
395 type = "\"s64\"";
396 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
397 break;
398 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
399 return 0;
400 type = "\"u64\"";
401 snprintf(buf, sizeof(buf), "%llu",
402 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200403 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200404 case FF_FLT: type = "\"flt\"";
405 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100406 break;
407 case FF_STR: type = "\"str\"";
408 value = field_str(f, 0);
409 quote = "\"";
410 break;
411 default: snprintf(buf, sizeof(buf), "%u", f->type);
412 type = buf;
413 value = "unknown";
414 quote = "\"";
415 break;
416 }
417
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200418 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100419 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
420 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200421 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100422}
423
William Lallemand74c24fb2016-11-21 17:18:36 +0100424/* Emits an encoding of the field type on 3 characters followed by a delimiter.
425 * Returns non-zero on success, 0 if the buffer is full.
426 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200427int stats_emit_field_tags(struct buffer *out, const struct field *f,
428 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100429{
430 char origin, nature, scope;
431
432 switch (field_origin(f, 0)) {
433 case FO_METRIC: origin = 'M'; break;
434 case FO_STATUS: origin = 'S'; break;
435 case FO_KEY: origin = 'K'; break;
436 case FO_CONFIG: origin = 'C'; break;
437 case FO_PRODUCT: origin = 'P'; break;
438 default: origin = '?'; break;
439 }
440
441 switch (field_nature(f, 0)) {
442 case FN_GAUGE: nature = 'G'; break;
443 case FN_LIMIT: nature = 'L'; break;
444 case FN_MIN: nature = 'm'; break;
445 case FN_MAX: nature = 'M'; break;
446 case FN_RATE: nature = 'R'; break;
447 case FN_COUNTER: nature = 'C'; break;
448 case FN_DURATION: nature = 'D'; break;
449 case FN_AGE: nature = 'A'; break;
450 case FN_TIME: nature = 'T'; break;
451 case FN_NAME: nature = 'N'; break;
452 case FN_OUTPUT: nature = 'O'; break;
453 case FN_AVG: nature = 'a'; break;
454 default: nature = '?'; break;
455 }
456
457 switch (field_scope(f, 0)) {
458 case FS_PROCESS: scope = 'P'; break;
459 case FS_SERVICE: scope = 'S'; break;
460 case FS_SYSTEM: scope = 's'; break;
461 case FS_CLUSTER: scope = 'C'; break;
462 default: scope = '?'; break;
463 }
464
465 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
466}
467
Simon Horman05ee2132017-01-04 09:37:25 +0100468/* Emits an encoding of the field type as JSON.
469 * Returns non-zero on success, 0 if the buffer is full.
470 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200471int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100472{
473 const char *origin, *nature, *scope;
474 int old_len;
475
476 switch (field_origin(f, 0)) {
477 case FO_METRIC: origin = "Metric"; break;
478 case FO_STATUS: origin = "Status"; break;
479 case FO_KEY: origin = "Key"; break;
480 case FO_CONFIG: origin = "Config"; break;
481 case FO_PRODUCT: origin = "Product"; break;
482 default: origin = "Unknown"; break;
483 }
484
485 switch (field_nature(f, 0)) {
486 case FN_GAUGE: nature = "Gauge"; break;
487 case FN_LIMIT: nature = "Limit"; break;
488 case FN_MIN: nature = "Min"; break;
489 case FN_MAX: nature = "Max"; break;
490 case FN_RATE: nature = "Rate"; break;
491 case FN_COUNTER: nature = "Counter"; break;
492 case FN_DURATION: nature = "Duration"; break;
493 case FN_AGE: nature = "Age"; break;
494 case FN_TIME: nature = "Time"; break;
495 case FN_NAME: nature = "Name"; break;
496 case FN_OUTPUT: nature = "Output"; break;
497 case FN_AVG: nature = "Avg"; break;
498 default: nature = "Unknown"; break;
499 }
500
501 switch (field_scope(f, 0)) {
502 case FS_PROCESS: scope = "Process"; break;
503 case FS_SERVICE: scope = "Service"; break;
504 case FS_SYSTEM: scope = "System"; break;
505 case FS_CLUSTER: scope = "Cluster"; break;
506 default: scope = "Unknown"; break;
507 }
508
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100510 chunk_appendf(out, "\"tags\":{"
511 "\"origin\":\"%s\","
512 "\"nature\":\"%s\","
513 "\"scope\":\"%s\""
514 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200515 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100516}
William Lallemand74c24fb2016-11-21 17:18:36 +0100517
518/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200519static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200520 const struct field *stats, size_t stats_count,
521 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100522{
523 int field;
524
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200525 for (field = 0; field < stats_count; field++) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100526 if (!stats_emit_raw_data_field(out, &stats[field]))
527 return 0;
528 if (!chunk_strcat(out, ","))
529 return 0;
530 }
531 chunk_strcat(out, "\n");
532 return 1;
533}
534
535/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200536static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200537 const struct field *stats,
538 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200539 unsigned int flags,
540 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100541{
542 int field;
543
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200544 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100545 if (!stats[field].type)
546 continue;
547
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200548 switch (domain) {
549 case STATS_DOMAIN_PROXY:
550 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
551 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
552 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
553 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
554 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
555 '?',
556 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
557 field,
558 stat_fields[field].name,
559 stats[ST_F_PID].u.u32);
560 break;
561
562 default:
563 break;
564 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100565
566 if (!stats_emit_field_tags(out, &stats[field], ':'))
567 return 0;
568 if (!stats_emit_typed_data_field(out, &stats[field]))
569 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200570 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
571 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100572 if (!chunk_strcat(out, "\n"))
573 return 0;
574 }
575 return 1;
576}
577
Simon Horman05ee2132017-01-04 09:37:25 +0100578/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200579static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200580 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100581{
582 int field;
583 int started = 0;
584
585 if (!chunk_strcat(out, "["))
586 return 0;
587
588 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
589 int old_len;
590
591 if (!field_format(info, field))
592 continue;
593
594 if (started && !chunk_strcat(out, ","))
595 goto err;
596 started = 1;
597
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200598 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100599 chunk_appendf(out,
600 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
601 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200602 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100603 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200604 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100605 goto err;
606
607 if (!stats_emit_json_field_tags(out, &info[field]))
608 goto err;
609
610 if (!stats_emit_json_data_field(out, &info[field]))
611 goto err;
612
613 if (!chunk_strcat(out, "}"))
614 goto err;
615 }
616
617 if (!chunk_strcat(out, "]"))
618 goto err;
619 return 1;
620
621err:
622 chunk_reset(out);
623 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
624 return 0;
625}
626
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200627static void stats_print_proxy_field_json(struct buffer *out,
628 const struct field *stat,
629 const char *name,
630 int pos,
631 uint32_t field_type,
632 uint32_t iid,
633 uint32_t sid,
634 uint32_t pid)
635{
636 const char *obj_type;
637 switch (field_type) {
638 case STATS_TYPE_FE: obj_type = "Frontend"; break;
639 case STATS_TYPE_BE: obj_type = "Backend"; break;
640 case STATS_TYPE_SO: obj_type = "Listener"; break;
641 case STATS_TYPE_SV: obj_type = "Server"; break;
642 default: obj_type = "Unknown"; break;
643 }
644
645 chunk_appendf(out,
646 "{"
647 "\"objType\":\"%s\","
648 "\"proxyId\":%u,"
649 "\"id\":%u,"
650 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
651 "\"processNum\":%u,",
652 obj_type, iid, sid, pos, name, pid);
653}
654
Simon Horman05ee2132017-01-04 09:37:25 +0100655/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200656static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200657 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200658 unsigned int flags,
659 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100660{
661 int field;
662 int started = 0;
663
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200664 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100665 return 0;
666 if (!chunk_strcat(out, "["))
667 return 0;
668
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200669 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100670 int old_len;
671
672 if (!stats[field].type)
673 continue;
674
675 if (started && !chunk_strcat(out, ","))
676 goto err;
677 started = 1;
678
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200680 if (domain == STATS_DOMAIN_PROXY) {
681 stats_print_proxy_field_json(out, &stats[field],
682 stat_fields[field].name, field,
683 stats[ST_F_TYPE].u.u32,
684 stats[ST_F_IID].u.u32,
685 stats[ST_F_SID].u.u32,
686 stats[ST_F_PID].u.u32);
687 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200688
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100690 goto err;
691
692 if (!stats_emit_json_field_tags(out, &stats[field]))
693 goto err;
694
695 if (!stats_emit_json_data_field(out, &stats[field]))
696 goto err;
697
698 if (!chunk_strcat(out, "}"))
699 goto err;
700 }
701
702 if (!chunk_strcat(out, "]"))
703 goto err;
704
705 return 1;
706
707err:
708 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200709 if (flags & STAT_STARTED)
710 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100711 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
712 return 0;
713}
714
William Lallemand74c24fb2016-11-21 17:18:36 +0100715/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200716 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200717 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100718 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200719static int stats_dump_fields_html(struct buffer *out,
720 const struct field *stats,
721 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100722{
Willy Tarreau83061a82018-07-13 11:56:34 +0200723 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100724
725 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
726 chunk_appendf(out,
727 /* name, queue */
728 "<tr class=\"frontend\">");
729
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200730 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100731 /* Column sub-heading for Enable or Disable server */
732 chunk_appendf(out, "<td></td>");
733 }
734
735 chunk_appendf(out,
736 "<td class=ac>"
737 "<a name=\"%s/Frontend\"></a>"
738 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
739 "<td colspan=3></td>"
740 "",
741 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
742
743 chunk_appendf(out,
744 /* sessions rate : current */
745 "<td><u>%s<div class=tips><table class=det>"
746 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
747 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
748 "",
749 U2H(stats[ST_F_RATE].u.u32),
750 U2H(stats[ST_F_CONN_RATE].u.u32),
751 U2H(stats[ST_F_RATE].u.u32));
752
753 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
754 chunk_appendf(out,
755 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
756 U2H(stats[ST_F_REQ_RATE].u.u32));
757
758 chunk_appendf(out,
759 "</table></div></u></td>"
760 /* sessions rate : max */
761 "<td><u>%s<div class=tips><table class=det>"
762 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
763 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
764 "",
765 U2H(stats[ST_F_RATE_MAX].u.u32),
766 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
767 U2H(stats[ST_F_RATE_MAX].u.u32));
768
769 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
770 chunk_appendf(out,
771 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
772 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
773
774 chunk_appendf(out,
775 "</table></div></u></td>"
776 /* sessions rate : limit */
777 "<td>%s</td>",
778 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
779
780 chunk_appendf(out,
781 /* sessions: current, max, limit, total */
782 "<td>%s</td><td>%s</td><td>%s</td>"
783 "<td><u>%s<div class=tips><table class=det>"
784 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
785 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
786 "",
787 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
788 U2H(stats[ST_F_STOT].u.u64),
789 U2H(stats[ST_F_CONN_TOT].u.u64),
790 U2H(stats[ST_F_STOT].u.u64));
791
792 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
793 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
794 chunk_appendf(out,
795 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
796 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
797 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
798 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
799 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
800 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
801 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
802 "<tr><th>- other responses:</th><td>%s</td></tr>"
803 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100804 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
805 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200806 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100807 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100808 "",
809 U2H(stats[ST_F_REQ_TOT].u.u64),
810 U2H(stats[ST_F_HRSP_1XX].u.u64),
811 U2H(stats[ST_F_HRSP_2XX].u.u64),
812 U2H(stats[ST_F_COMP_RSP].u.u64),
813 stats[ST_F_HRSP_2XX].u.u64 ?
814 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
815 U2H(stats[ST_F_HRSP_3XX].u.u64),
816 U2H(stats[ST_F_HRSP_4XX].u.u64),
817 U2H(stats[ST_F_HRSP_5XX].u.u64),
818 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200819 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100820 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
821 U2H(stats[ST_F_CACHE_HITS].u.u64),
822 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
823 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100824 U2H(stats[ST_F_WREW].u.u64),
825 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100826 }
827
828 chunk_appendf(out,
829 "</table></div></u></td>"
830 /* sessions: lbtot, lastsess */
831 "<td></td><td></td>"
832 /* bytes : in */
833 "<td>%s</td>"
834 "",
835 U2H(stats[ST_F_BIN].u.u64));
836
837 chunk_appendf(out,
838 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
839 "<td>%s%s<div class=tips><table class=det>"
840 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
841 "<tr><th>Compression in:</th><td>%s</td></tr>"
842 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
843 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
844 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
845 "</table></div>%s</td>",
846 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
847 U2H(stats[ST_F_BOUT].u.u64),
848 U2H(stats[ST_F_BOUT].u.u64),
849 U2H(stats[ST_F_COMP_IN].u.u64),
850 U2H(stats[ST_F_COMP_OUT].u.u64),
851 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
852 U2H(stats[ST_F_COMP_BYP].u.u64),
853 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
854 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,
855 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
856
857 chunk_appendf(out,
858 /* denied: req, resp */
859 "<td>%s</td><td>%s</td>"
860 /* errors : request, connect, response */
861 "<td>%s</td><td></td><td></td>"
862 /* warnings: retries, redispatches */
863 "<td></td><td></td>"
864 /* server status : reflect frontend status */
865 "<td class=ac>%s</td>"
866 /* rest of server: nothing */
867 "<td class=ac colspan=8></td></tr>"
868 "",
869 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
870 U2H(stats[ST_F_EREQ].u.u64),
871 field_str(stats, ST_F_STATUS));
872 }
873 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
874 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200875 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100876 /* Column sub-heading for Enable or Disable server */
877 chunk_appendf(out, "<td></td>");
878 }
879
880 chunk_appendf(out,
881 /* frontend name, listener name */
882 "<td class=ac><a name=\"%s/+%s\"></a>%s"
883 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
884 "",
885 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200886 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100887 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
888
Willy Tarreau708c4162019-10-09 10:19:16 +0200889 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100890 chunk_appendf(out, "<div class=tips>");
891
Willy Tarreau90807112020-02-25 08:16:33 +0100892 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100893 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
894 else if (*field_str(stats, ST_F_ADDR) == '[')
895 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
896 else if (*field_str(stats, ST_F_ADDR))
897 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
898
899 /* id */
900 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
901 }
902
903 chunk_appendf(out,
904 /* queue */
905 "%s</td><td colspan=3></td>"
906 /* sessions rate: current, max, limit */
907 "<td colspan=3>&nbsp;</td>"
908 /* sessions: current, max, limit, total, lbtot, lastsess */
909 "<td>%s</td><td>%s</td><td>%s</td>"
910 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
911 /* bytes: in, out */
912 "<td>%s</td><td>%s</td>"
913 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200914 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100915 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
916 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
917
918 chunk_appendf(out,
919 /* denied: req, resp */
920 "<td>%s</td><td>%s</td>"
921 /* errors: request, connect, response */
922 "<td>%s</td><td></td><td></td>"
923 /* warnings: retries, redispatches */
924 "<td></td><td></td>"
925 /* server status: reflect listener status */
926 "<td class=ac>%s</td>"
927 /* rest of server: nothing */
928 "<td class=ac colspan=8></td></tr>"
929 "",
930 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
931 U2H(stats[ST_F_EREQ].u.u64),
932 field_str(stats, ST_F_STATUS));
933 }
934 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
935 const char *style;
936
937 /* determine the style to use depending on the server's state,
938 * its health and weight. There isn't a 1-to-1 mapping between
939 * state and styles for the cases where the server is (still)
940 * up. The reason is that we don't want to report nolb and
941 * drain with the same color.
942 */
943
944 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
945 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
946 style = "down";
947 }
948 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
949 style = "going_up";
950 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400951 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
952 style = "draining";
953 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100954 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
955 style = "going_down";
956 }
957 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
958 style = "nolb";
959 }
960 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
961 style = "no_check";
962 }
963 else if (!stats[ST_F_CHKFAIL].type ||
964 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
965 /* no check or max health = UP */
966 if (stats[ST_F_WEIGHT].u.u32)
967 style = "up";
968 else
969 style = "draining";
970 }
971 else {
972 style = "going_down";
973 }
974
Willy Tarreau7b524852020-08-11 10:26:36 +0200975 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +0100976 chunk_appendf(out, "<tr class=\"maintain\">");
977 else
978 chunk_appendf(out,
979 "<tr class=\"%s_%s\">",
980 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
981
982
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200983 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100984 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000985 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
986 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100987 field_str(stats, ST_F_SVNAME));
988
989 chunk_appendf(out,
990 "<td class=ac><a name=\"%s/%s\"></a>%s"
991 "<a class=lfsb href=\"#%s/%s\">%s</a>"
992 "",
993 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200994 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100995 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
996
Willy Tarreau708c4162019-10-09 10:19:16 +0200997 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100998 chunk_appendf(out, "<div class=tips>");
999
Willy Tarreau90807112020-02-25 08:16:33 +01001000 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001001 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1002 else if (*field_str(stats, ST_F_ADDR) == '[')
1003 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1004 else if (*field_str(stats, ST_F_ADDR))
1005 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1006
1007 /* id */
1008 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1009
1010 /* cookie */
1011 if (stats[ST_F_COOKIE].type) {
1012 chunk_appendf(out, ", cookie: '");
1013 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1014 chunk_htmlencode(out, &src);
1015 chunk_appendf(out, "'");
1016 }
1017
1018 chunk_appendf(out, "</div>");
1019 }
1020
1021 chunk_appendf(out,
1022 /* queue : current, max, limit */
1023 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1024 /* sessions rate : current, max, limit */
1025 "<td>%s</td><td>%s</td><td></td>"
1026 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001027 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001028 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1029 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1030
1031 chunk_appendf(out,
1032 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001033 "<td><u>%s<div class=tips>"
1034 "<table class=det>"
1035 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001036 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001037 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001038 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1039 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001040 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001041 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1042 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1043 "</table></div></u>"
1044 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001045 "<td><u>%s<div class=tips><table class=det>"
1046 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1047 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001048 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001049 U2H(stats[ST_F_SCUR].u.u32),
1050 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1051 U2H(stats[ST_F_SRV_ICUR].u.u32),
1052 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1053 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001054 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001055
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001056 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1057 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1058 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001059 U2H(stats[ST_F_STOT].u.u64),
1060 U2H(stats[ST_F_STOT].u.u64));
1061
1062 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1063 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001064 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001065 "<tr><th>New connections:</th><td>%s</td></tr>"
1066 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001067 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001068 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1069 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1070 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1071 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1072 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1073 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001074 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001075 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001076 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001077 U2H(stats[ST_F_CONNECT].u.u64),
1078 U2H(stats[ST_F_REUSE].u.u64),
1079 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1080 (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 +02001081 U2H(stats[ST_F_REQ_TOT].u.u64),
1082 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1083 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1084 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1085 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1086 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1087 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1088 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1089 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1090 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1091 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1092 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1093 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001094 U2H(stats[ST_F_WREW].u.u64),
1095 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001096 }
1097
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001098 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1099 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1100 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1101 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1102 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001103 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001104 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1105 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1106 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1107 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001108
1109 chunk_appendf(out,
1110 "</table></div></u></td>"
1111 /* sessions: lbtot, last */
1112 "<td>%s</td><td>%s</td>",
1113 U2H(stats[ST_F_LBTOT].u.u64),
1114 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1115
1116 chunk_appendf(out,
1117 /* bytes : in, out */
1118 "<td>%s</td><td>%s</td>"
1119 /* denied: req, resp */
1120 "<td></td><td>%s</td>"
1121 /* errors : request, connect */
1122 "<td></td><td>%s</td>"
1123 /* errors : response */
1124 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1125 /* warnings: retries, redispatches */
1126 "<td>%lld</td><td>%lld</td>"
1127 "",
1128 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1129 U2H(stats[ST_F_DRESP].u.u64),
1130 U2H(stats[ST_F_ECON].u.u64),
1131 U2H(stats[ST_F_ERESP].u.u64),
1132 (long long)stats[ST_F_CLI_ABRT].u.u64,
1133 (long long)stats[ST_F_SRV_ABRT].u.u64,
1134 (long long)stats[ST_F_WRETR].u.u64,
1135 (long long)stats[ST_F_WREDIS].u.u64);
1136
1137 /* status, last change */
1138 chunk_appendf(out, "<td class=ac>");
1139
1140 /* FIXME!!!!
1141 * LASTCHG should contain the last change for *this* server and must be computed
1142 * properly above, as was done below, ie: this server if maint, otherwise ref server
1143 * if tracking. Note that ref is either local or remote depending on tracking.
1144 */
1145
1146
Willy Tarreau7b524852020-08-11 10:26:36 +02001147 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001148 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1149 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001150 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001151 chunk_strcat(out, "<i>no check</i>");
1152 }
1153 else {
1154 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 +02001155 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001156 if (stats[ST_F_CHECK_HEALTH].u.u32)
1157 chunk_strcat(out, " &uarr;");
1158 }
1159 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1160 chunk_strcat(out, " &darr;");
1161 }
1162
Willy Tarreau7b524852020-08-11 10:26:36 +02001163 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001164 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1165 chunk_appendf(out,
1166 "</td><td class=ac><u> %s",
1167 field_str(stats, ST_F_AGENT_STATUS));
1168
1169 if (stats[ST_F_AGENT_CODE].type)
1170 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1171
1172 if (stats[ST_F_AGENT_DURATION].type)
1173 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1174
1175 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1176
1177 if (*field_str(stats, ST_F_LAST_AGT)) {
1178 chunk_appendf(out, ": ");
1179 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1180 chunk_htmlencode(out, &src);
1181 }
1182 chunk_appendf(out, "</div></u>");
1183 }
1184 else if (stats[ST_F_CHECK_STATUS].type) {
1185 chunk_appendf(out,
1186 "</td><td class=ac><u> %s",
1187 field_str(stats, ST_F_CHECK_STATUS));
1188
1189 if (stats[ST_F_CHECK_CODE].type)
1190 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1191
1192 if (stats[ST_F_CHECK_DURATION].type)
1193 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1194
1195 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1196
1197 if (*field_str(stats, ST_F_LAST_CHK)) {
1198 chunk_appendf(out, ": ");
1199 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1200 chunk_htmlencode(out, &src);
1201 }
1202 chunk_appendf(out, "</div></u>");
1203 }
1204 else
1205 chunk_appendf(out, "</td><td>");
1206
1207 chunk_appendf(out,
1208 /* weight */
1209 "</td><td class=ac>%d</td>"
1210 /* act, bck */
1211 "<td class=ac>%s</td><td class=ac>%s</td>"
1212 "",
1213 stats[ST_F_WEIGHT].u.u32,
1214 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1215 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1216
1217 /* check failures: unique, fatal, down time */
1218 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1219 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1220 }
1221 else if (stats[ST_F_CHKFAIL].type) {
1222 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1223
1224 if (stats[ST_F_HANAFAIL].type)
1225 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1226
1227 chunk_appendf(out,
1228 "<div class=tips>Failed Health Checks%s</div></u></td>"
1229 "<td>%lld</td><td>%s</td>"
1230 "",
1231 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1232 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1233 }
1234 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1235 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1236 chunk_appendf(out,
1237 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1238 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1239 }
1240 else
1241 chunk_appendf(out, "<td colspan=3></td>");
1242
1243 /* throttle */
1244 if (stats[ST_F_THROTTLE].type)
1245 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1246 else
1247 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1248 }
1249 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1250 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001251 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001252 /* Column sub-heading for Enable or Disable server */
1253 chunk_appendf(out, "<td></td>");
1254 }
1255 chunk_appendf(out,
1256 "<td class=ac>"
1257 /* name */
1258 "%s<a name=\"%s/Backend\"></a>"
1259 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1260 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001261 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001262 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1263
Willy Tarreau708c4162019-10-09 10:19:16 +02001264 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001265 /* balancing */
1266 chunk_appendf(out, "<div class=tips>balancing: %s",
1267 field_str(stats, ST_F_ALGO));
1268
1269 /* cookie */
1270 if (stats[ST_F_COOKIE].type) {
1271 chunk_appendf(out, ", cookie: '");
1272 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1273 chunk_htmlencode(out, &src);
1274 chunk_appendf(out, "'");
1275 }
1276 chunk_appendf(out, "</div>");
1277 }
1278
1279 chunk_appendf(out,
1280 "%s</td>"
1281 /* queue : current, max */
1282 "<td>%s</td><td>%s</td><td></td>"
1283 /* sessions rate : current, max, limit */
1284 "<td>%s</td><td>%s</td><td></td>"
1285 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001286 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001287 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1288 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1289
1290 chunk_appendf(out,
1291 /* sessions: current, max, limit, total */
1292 "<td>%s</td><td>%s</td><td>%s</td>"
1293 "<td><u>%s<div class=tips><table class=det>"
1294 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1295 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001296 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 +01001297 U2H(stats[ST_F_STOT].u.u64),
1298 U2H(stats[ST_F_STOT].u.u64));
1299
1300 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1301 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1302 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001303 "<tr><th>New connections:</th><td>%s</td></tr>"
1304 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001305 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1306 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1307 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1308 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1309 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1310 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1311 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1312 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001313 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1314 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001315 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001316 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001317 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001318 U2H(stats[ST_F_CONNECT].u.u64),
1319 U2H(stats[ST_F_REUSE].u.u64),
1320 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1321 (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 +01001322 U2H(stats[ST_F_REQ_TOT].u.u64),
1323 U2H(stats[ST_F_HRSP_1XX].u.u64),
1324 U2H(stats[ST_F_HRSP_2XX].u.u64),
1325 U2H(stats[ST_F_COMP_RSP].u.u64),
1326 stats[ST_F_HRSP_2XX].u.u64 ?
1327 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1328 U2H(stats[ST_F_HRSP_3XX].u.u64),
1329 U2H(stats[ST_F_HRSP_4XX].u.u64),
1330 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001331 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001332 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1333 U2H(stats[ST_F_CACHE_HITS].u.u64),
1334 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1335 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001336 U2H(stats[ST_F_WREW].u.u64),
1337 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001338 }
1339
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001340 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1341 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1342 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1343 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1344 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001345 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001346 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1347 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1348 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1349 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001350
1351 chunk_appendf(out,
1352 "</table></div></u></td>"
1353 /* sessions: lbtot, last */
1354 "<td>%s</td><td>%s</td>"
1355 /* bytes: in */
1356 "<td>%s</td>"
1357 "",
1358 U2H(stats[ST_F_LBTOT].u.u64),
1359 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1360 U2H(stats[ST_F_BIN].u.u64));
1361
1362 chunk_appendf(out,
1363 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1364 "<td>%s%s<div class=tips><table class=det>"
1365 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1366 "<tr><th>Compression in:</th><td>%s</td></tr>"
1367 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1368 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1369 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1370 "</table></div>%s</td>",
1371 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1372 U2H(stats[ST_F_BOUT].u.u64),
1373 U2H(stats[ST_F_BOUT].u.u64),
1374 U2H(stats[ST_F_COMP_IN].u.u64),
1375 U2H(stats[ST_F_COMP_OUT].u.u64),
1376 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1377 U2H(stats[ST_F_COMP_BYP].u.u64),
1378 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1379 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,
1380 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1381
1382 chunk_appendf(out,
1383 /* denied: req, resp */
1384 "<td>%s</td><td>%s</td>"
1385 /* errors : request, connect */
1386 "<td></td><td>%s</td>"
1387 /* errors : response */
1388 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1389 /* warnings: retries, redispatches */
1390 "<td>%lld</td><td>%lld</td>"
1391 /* backend status: reflect backend status (up/down): we display UP
1392 * if the backend has known working servers or if it has no server at
1393 * all (eg: for stats). Then we display the total weight, number of
1394 * active and backups. */
1395 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1396 "<td class=ac>%d</td><td class=ac>%d</td>"
1397 "",
1398 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1399 U2H(stats[ST_F_ECON].u.u64),
1400 U2H(stats[ST_F_ERESP].u.u64),
1401 (long long)stats[ST_F_CLI_ABRT].u.u64,
1402 (long long)stats[ST_F_SRV_ABRT].u.u64,
1403 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1404 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1405 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1406 stats[ST_F_WEIGHT].u.u32,
1407 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1408
1409 chunk_appendf(out,
1410 /* rest of backend: nothing, down transitions, total downtime, throttle */
1411 "<td class=ac>&nbsp;</td><td>%d</td>"
1412 "<td>%s</td>"
1413 "<td></td>"
1414 "</tr>",
1415 stats[ST_F_CHKDOWN].u.u32,
1416 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1417 }
1418 return 1;
1419}
1420
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001421int stats_dump_one_line(const struct field *stats, size_t stats_count,
1422 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001423{
Simon Horman05ee2132017-01-04 09:37:25 +01001424 int ret;
1425
William Lallemand74c24fb2016-11-21 17:18:36 +01001426 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001427 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001428 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001429 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001430 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001431 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001432 else
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001433 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001434
1435 if (ret)
1436 appctx->ctx.stats.flags |= STAT_STARTED;
1437
1438 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001439}
1440
1441/* Fill <stats> with the frontend statistics. <stats> is
1442 * preallocated array of length <len>. The length of the array
1443 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1444 * this value, the function returns 0, otherwise, it returns 1.
1445 */
1446int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1447{
1448 if (len < ST_F_TOTAL_FIELDS)
1449 return 0;
1450
1451 memset(stats, 0, sizeof(*stats) * len);
1452
1453 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1454 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1455 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1456 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1457 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1458 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1459 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1460 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1461 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1462 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1463 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1464 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1465 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1466 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1467 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1468 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1469 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1470 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1471 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1472 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1473 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1474 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001475 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001476 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001477
1478 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1479 if (px->mode == PR_MODE_HTTP) {
1480 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1481 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1482 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1483 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1484 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1485 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1486 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001487 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1488 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001489 }
1490
1491 /* requests : req_rate, req_rate_max, req_tot, */
1492 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1493 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1494 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1495
1496 /* compression: in, out, bypassed, responses */
1497 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1498 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1499 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1500 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1501
1502 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1503 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1504 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1505 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1506
1507 return 1;
1508}
1509
1510/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1511 * the state from stream interface <si>. The caller is responsible for clearing
1512 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1513 */
1514static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1515{
1516 struct appctx *appctx = __objt_appctx(si->end);
1517
1518 if (!(px->cap & PR_CAP_FE))
1519 return 0;
1520
1521 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1522 return 0;
1523
1524 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1525 return 0;
1526
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001527 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001528}
1529
1530/* Fill <stats> with the listener statistics. <stats> is
1531 * preallocated array of length <len>. The length of the array
1532 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1533 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001534 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001535 */
1536int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1537 struct field *stats, int len)
1538{
Willy Tarreau83061a82018-07-13 11:56:34 +02001539 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001540
1541 if (len < ST_F_TOTAL_FIELDS)
1542 return 0;
1543
1544 if (!l->counters)
1545 return 0;
1546
1547 chunk_reset(out);
1548 memset(stats, 0, sizeof(*stats) * len);
1549
1550 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1551 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1552 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1553 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1554 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1555 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1556 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1557 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1558 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1559 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1560 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1561 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1562 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1563 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001564 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 +01001565 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1566 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1567 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1568 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001569 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001570 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001571
Willy Tarreau708c4162019-10-09 10:19:16 +02001572 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001573 char str[INET6_ADDRSTRLEN];
1574 int port;
1575
Willy Tarreau37159062020-08-27 07:48:42 +02001576 port = get_host_port(&l->rx.addr);
1577 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001578 case AF_INET:
1579 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1580 chunk_appendf(out, "%s:%d", str, port);
1581 break;
1582 case AF_INET6:
1583 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1584 chunk_appendf(out, "[%s]:%d", str, port);
1585 break;
1586 case AF_UNIX:
1587 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1588 break;
1589 case -1:
1590 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1591 chunk_strcat(out, strerror(errno));
1592 break;
1593 default: /* address family not supported */
1594 break;
1595 }
1596 }
1597
1598 return 1;
1599}
1600
1601/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001602 * from stream interface <si>. The caller is responsible for clearing the trash
1603 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001604 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001605static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001606{
1607 struct appctx *appctx = __objt_appctx(si->end);
1608
Willy Tarreau43241ff2019-10-09 11:27:51 +02001609 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001610 return 0;
1611
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001612 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001613}
1614
1615enum srv_stats_state {
1616 SRV_STATS_STATE_DOWN = 0,
1617 SRV_STATS_STATE_DOWN_AGENT,
1618 SRV_STATS_STATE_GOING_UP,
1619 SRV_STATS_STATE_UP_GOING_DOWN,
1620 SRV_STATS_STATE_UP,
1621 SRV_STATS_STATE_NOLB_GOING_DOWN,
1622 SRV_STATS_STATE_NOLB,
1623 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1624 SRV_STATS_STATE_DRAIN,
1625 SRV_STATS_STATE_DRAIN_AGENT,
1626 SRV_STATS_STATE_NO_CHECK,
1627
1628 SRV_STATS_STATE_COUNT, /* Must be last */
1629};
1630
1631static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1632 [SRV_STATS_STATE_DOWN] = "DOWN",
1633 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1634 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1635 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1636 [SRV_STATS_STATE_UP] = "UP",
1637 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1638 [SRV_STATS_STATE_NOLB] = "NOLB",
1639 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1640 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1641 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1642 [SRV_STATS_STATE_NO_CHECK] = "no check"
1643};
1644
1645/* Fill <stats> with the server statistics. <stats> is
1646 * preallocated array of length <len>. The length of the array
1647 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1648 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001649 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001650 */
1651int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1652 struct field *stats, int len)
1653{
1654 struct server *via, *ref;
1655 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001656 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001657 enum srv_stats_state state;
1658 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001659 long long srv_samples_counter;
1660 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001661
1662 if (len < ST_F_TOTAL_FIELDS)
1663 return 0;
1664
1665 memset(stats, 0, sizeof(*stats) * len);
1666
1667 /* we have "via" which is the tracked server as described in the configuration,
1668 * and "ref" which is the checked server and the end of the chain.
1669 */
1670 via = sv->track ? sv->track : sv;
1671 ref = via;
1672 while (ref->track)
1673 ref = ref->track;
1674
Emeric Brun52a91d32017-08-31 14:41:55 +02001675 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001676 if ((ref->check.state & CHK_ST_ENABLED) &&
1677 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1678 state = SRV_STATS_STATE_UP_GOING_DOWN;
1679 } else {
1680 state = SRV_STATS_STATE_UP;
1681 }
1682
Emeric Brun52a91d32017-08-31 14:41:55 +02001683 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001684 if (ref->agent.state & CHK_ST_ENABLED)
1685 state = SRV_STATS_STATE_DRAIN_AGENT;
1686 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1687 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1688 else
1689 state = SRV_STATS_STATE_DRAIN;
1690 }
1691
1692 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1693 state = SRV_STATS_STATE_NO_CHECK;
1694 }
1695 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001696 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001697 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1698 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1699 state = SRV_STATS_STATE_NOLB;
1700 } else {
1701 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1702 }
1703 }
1704 else { /* stopped */
1705 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1706 state = SRV_STATS_STATE_DOWN_AGENT;
1707 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1708 state = SRV_STATS_STATE_DOWN; /* DOWN */
1709 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1710 state = SRV_STATS_STATE_GOING_UP;
1711 } else {
1712 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1713 }
1714 }
1715
1716 chunk_reset(out);
1717
1718 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1719 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1720 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1721 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1722 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1723 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1724 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1725
1726 if (sv->maxconn)
1727 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1728
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001729 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1730 if (sv->max_idle_conns != -1)
1731 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1732
William Lallemand74c24fb2016-11-21 17:18:36 +01001733 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1734 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1735 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001736 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001737 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1738 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1739 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1740 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001741 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001742 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001743 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1744 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001745
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001746 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1747 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1748 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001749 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001750
William Lallemand74c24fb2016-11-21 17:18:36 +01001751 /* status */
1752 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001753 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001754 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001755 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001756 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001757 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001758 chunk_appendf(out, "MAINT");
1759 else
1760 chunk_appendf(out,
1761 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001762 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1763 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001764
1765 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1766 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001767 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 +01001768 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1769 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1770
1771 /* check failures: unique, fatal; last change, total downtime */
1772 if (sv->check.state & CHK_ST_ENABLED) {
1773 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1774 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1775 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1776 }
1777
1778 if (sv->maxqueue)
1779 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1780
1781 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1782 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1783 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1784
Emeric Brun52a91d32017-08-31 14:41:55 +02001785 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001786 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1787
1788 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1789
1790 if (sv->track) {
1791 char *fld_track = chunk_newstr(out);
1792
1793 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1794 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1795 }
1796
1797 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1798 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1799 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1800
1801 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1802 const char *fld_chksts;
1803
1804 fld_chksts = chunk_newstr(out);
1805 chunk_strcat(out, "* "); // for check in progress
1806 chunk_strcat(out, get_check_status_info(sv->check.status));
1807 if (!(sv->check.state & CHK_ST_INPROGRESS))
1808 fld_chksts += 2; // skip "* "
1809 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1810
1811 if (sv->check.status >= HCHK_STATUS_L57DATA)
1812 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1813
1814 if (sv->check.status >= HCHK_STATUS_CHECKED)
1815 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1816
1817 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1818 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1819 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1820 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1821 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1822 }
1823
1824 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1825 const char *fld_chksts;
1826
1827 fld_chksts = chunk_newstr(out);
1828 chunk_strcat(out, "* "); // for check in progress
1829 chunk_strcat(out, get_check_status_info(sv->agent.status));
1830 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1831 fld_chksts += 2; // skip "* "
1832 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1833
1834 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1835 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1836
1837 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1838 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1839
1840 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1841 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1842 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1843 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1844 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1845 }
1846
1847 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1848 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001849 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001850 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1851 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1852 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1853 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1854 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1855 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1856 }
1857
1858 if (ref->observe)
1859 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1860
1861 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1862 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1863 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1864
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001865 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1866 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1867 srv_samples_window = srv_samples_counter;
1868
1869 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1870 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1871 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1872 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001873
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001874 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1875 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1876 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1877 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1878
Willy Tarreau708c4162019-10-09 10:19:16 +02001879 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001880 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1881 case AF_INET:
1882 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001883 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001884 break;
1885 case AF_INET6:
1886 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001887 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001888 break;
1889 case AF_UNIX:
1890 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1891 break;
1892 case -1:
1893 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1894 chunk_strcat(out, strerror(errno));
1895 break;
1896 default: /* address family not supported */
1897 break;
1898 }
1899
1900 if (sv->cookie)
1901 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1902 }
1903
1904 return 1;
1905}
1906
1907/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001908 * from stream interface <si>, and server state <state>. The caller is
1909 * responsible for clearing the trash if needed. Returns non-zero if it emits
1910 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001911 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001912static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001913{
1914 struct appctx *appctx = __objt_appctx(si->end);
1915
Willy Tarreau43241ff2019-10-09 11:27:51 +02001916 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001917 return 0;
1918
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001919 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001920}
1921
1922/* Fill <stats> with the backend statistics. <stats> is
1923 * preallocated array of length <len>. The length of the array
1924 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1925 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001926 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001927 */
1928int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1929{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001930 long long be_samples_counter;
1931 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1932
William Lallemand74c24fb2016-11-21 17:18:36 +01001933 if (len < ST_F_TOTAL_FIELDS)
1934 return 0;
1935
1936 memset(stats, 0, sizeof(*stats) * len);
1937
1938 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1939 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1940 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1941 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1942 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001943 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001944 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1945 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1946 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1947 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1948 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1949 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1950 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1951 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1952 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1953 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1954 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001955 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001956 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001957 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1958 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001959 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1960 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1961 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1962 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1963 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1964 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1965 if (px->srv)
1966 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1967
1968 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1969 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1970 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1971 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1972 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1973 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1974 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1975
Willy Tarreau708c4162019-10-09 10:19:16 +02001976 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001977 if (px->cookie_name)
1978 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1979 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1980 }
1981
1982 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1983 if (px->mode == PR_MODE_HTTP) {
1984 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1985 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1986 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1987 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1988 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1989 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1990 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001991 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1992 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001993 }
1994
1995 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1996 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1997
1998 /* compression: in, out, bypassed, responses */
1999 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2000 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2001 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2002 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2003 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
2004
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002005 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2006 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2007 be_samples_window = be_samples_counter;
2008
2009 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2010 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2011 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2012 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 +01002013
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002014 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2015 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2016 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2017 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2018
William Lallemand74c24fb2016-11-21 17:18:36 +01002019 return 1;
2020}
2021
2022/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002023 * interface <si>. The caller is responsible for clearing the trash if needed.
2024 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002025 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002026static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002027{
2028 struct appctx *appctx = __objt_appctx(si->end);
2029
2030 if (!(px->cap & PR_CAP_BE))
2031 return 0;
2032
2033 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2034 return 0;
2035
Willy Tarreau43241ff2019-10-09 11:27:51 +02002036 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002037 return 0;
2038
Amaury Denoyelle97323c92020-10-02 18:32:01 +02002039 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002040}
2041
2042/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2043 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2044 * for clearing the trash if needed.
2045 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002046static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002047{
2048 struct appctx *appctx = __objt_appctx(si->end);
2049 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2050
2051 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2052 /* A form to enable/disable this proxy servers */
2053
2054 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2055 scope_txt[0] = 0;
2056 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002057 const char *scope_ptr = stats_scope_ptr(appctx, si);
2058
William Lallemand74c24fb2016-11-21 17:18:36 +01002059 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002060 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002061 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2062 }
2063
2064 chunk_appendf(&trash,
2065 "<form method=\"post\">");
2066 }
2067
2068 /* print a new table */
2069 chunk_appendf(&trash,
2070 "<table class=\"tbl\" width=\"100%%\">\n"
2071 "<tr class=\"titre\">"
2072 "<th class=\"pxname\" width=\"10%%\">");
2073
2074 chunk_appendf(&trash,
2075 "<a name=\"%s\"></a>%s"
2076 "<a class=px href=\"#%s\">%s</a>",
2077 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002078 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002079 px->id, px->id);
2080
Willy Tarreau676c29e2019-10-09 10:50:01 +02002081 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002082 /* cap, mode, id */
2083 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2084 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2085 px->uuid);
2086 chunk_appendf(&trash, "</div>");
2087 }
2088
2089 chunk_appendf(&trash,
2090 "%s</th>"
2091 "<th class=\"%s\" width=\"90%%\">%s</th>"
2092 "</tr>\n"
2093 "</table>\n"
2094 "<table class=\"tbl\" width=\"100%%\">\n"
2095 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002096 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002097 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2098
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002099 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002100 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002101 if ((px->cap & PR_CAP_BE) && px->srv)
2102 chunk_appendf(&trash,
2103 "<th rowspan=2 width=1><input type=\"checkbox\" "
2104 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2105 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2106 px->id,
2107 px->id);
2108 else
2109 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002110 }
2111
2112 chunk_appendf(&trash,
2113 "<th rowspan=2></th>"
2114 "<th colspan=3>Queue</th>"
2115 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2116 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2117 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2118 "<th colspan=9>Server</th>"
2119 "</tr>\n"
2120 "<tr class=\"titre\">"
2121 "<th>Cur</th><th>Max</th><th>Limit</th>"
2122 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2123 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2124 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2125 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2126 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2127 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2128 "<th>Thrtle</th>\n"
2129 "</tr>");
2130}
2131
2132/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2133 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2134 */
2135static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2136{
2137 struct appctx *appctx = __objt_appctx(si->end);
2138 chunk_appendf(&trash, "</table>");
2139
2140 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2141 /* close the form used to enable/disable this proxy servers */
2142 chunk_appendf(&trash,
2143 "Choose the action to perform on the checked servers : "
2144 "<select name=action>"
2145 "<option value=\"\"></option>"
2146 "<option value=\"ready\">Set state to READY</option>"
2147 "<option value=\"drain\">Set state to DRAIN</option>"
2148 "<option value=\"maint\">Set state to MAINT</option>"
2149 "<option value=\"dhlth\">Health: disable checks</option>"
2150 "<option value=\"ehlth\">Health: enable checks</option>"
2151 "<option value=\"hrunn\">Health: force UP</option>"
2152 "<option value=\"hnolb\">Health: force NOLB</option>"
2153 "<option value=\"hdown\">Health: force DOWN</option>"
2154 "<option value=\"dagent\">Agent: disable checks</option>"
2155 "<option value=\"eagent\">Agent: enable checks</option>"
2156 "<option value=\"arunn\">Agent: force UP</option>"
2157 "<option value=\"adown\">Agent: force DOWN</option>"
2158 "<option value=\"shutdown\">Kill Sessions</option>"
2159 "</select>"
2160 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2161 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2162 "</form>",
2163 px->uuid);
2164 }
2165
2166 chunk_appendf(&trash, "<p>\n");
2167}
2168
2169/*
2170 * Dumps statistics for a proxy. The output is sent to the stream interface's
2171 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2172 * buffer space, or non-zero if everything completed. This function is used
2173 * both by the CLI and the HTTP entry points, and is able to dump the output
2174 * in HTML or CSV formats. If the later, <uri> must be NULL.
2175 */
Christopher Fauletef779222018-10-31 08:47:01 +01002176int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2177 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002178{
2179 struct appctx *appctx = __objt_appctx(si->end);
2180 struct stream *s = si_strm(si);
2181 struct channel *rep = si_ic(si);
2182 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2183 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002184
2185 chunk_reset(&trash);
2186
2187 switch (appctx->ctx.stats.px_st) {
2188 case STAT_PX_ST_INIT:
2189 /* we are on a new proxy */
2190 if (uri && uri->scope) {
2191 /* we have a limited scope, we have to check the proxy name */
2192 struct stat_scope *scope;
2193 int len;
2194
2195 len = strlen(px->id);
2196 scope = uri->scope;
2197
2198 while (scope) {
2199 /* match exact proxy name */
2200 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2201 break;
2202
2203 /* match '.' which means 'self' proxy */
2204 if (!strcmp(scope->px_id, ".") && px == s->be)
2205 break;
2206 scope = scope->next;
2207 }
2208
2209 /* proxy name not found : don't dump anything */
2210 if (scope == NULL)
2211 return 1;
2212 }
2213
2214 /* if the user has requested a limited output and the proxy
2215 * name does not match, skip it.
2216 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002217 if (appctx->ctx.stats.scope_len) {
2218 const char *scope_ptr = stats_scope_ptr(appctx, si);
2219
2220 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2221 return 1;
2222 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002223
2224 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2225 (appctx->ctx.stats.iid != -1) &&
2226 (px->uuid != appctx->ctx.stats.iid))
2227 return 1;
2228
2229 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2230 /* fall through */
2231
2232 case STAT_PX_ST_TH:
2233 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002234 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002235 if (!stats_putchk(rep, htx, &trash))
2236 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002237 }
2238
2239 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2240 /* fall through */
2241
2242 case STAT_PX_ST_FE:
2243 /* print the frontend */
2244 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002245 if (!stats_putchk(rep, htx, &trash))
2246 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002247 }
2248
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002249 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01002250 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2251 /* fall through */
2252
2253 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002254 /* obj2 points to listeners list as initialized above */
2255 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002256 if (htx) {
2257 if (htx_almost_full(htx))
2258 goto full;
2259 }
2260 else {
2261 if (buffer_almost_full(&rep->buf))
2262 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002263 }
2264
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002265 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01002266 if (!l->counters)
2267 continue;
2268
2269 if (appctx->ctx.stats.flags & STAT_BOUND) {
2270 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2271 break;
2272
2273 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2274 continue;
2275 }
2276
2277 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002278 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002279 if (!stats_putchk(rep, htx, &trash))
2280 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002281 }
2282 }
2283
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002284 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01002285 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2286 /* fall through */
2287
2288 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002289 /* obj2 points to servers list as initialized above */
2290 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002291 if (htx) {
2292 if (htx_almost_full(htx))
2293 goto full;
2294 }
2295 else {
2296 if (buffer_almost_full(&rep->buf))
2297 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002298 }
2299
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002300 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01002301
2302 if (appctx->ctx.stats.flags & STAT_BOUND) {
2303 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2304 break;
2305
2306 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2307 continue;
2308 }
2309
2310 svs = sv;
2311 while (svs->track)
2312 svs = svs->track;
2313
2314 /* do not report servers which are DOWN and not changing state */
2315 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002316 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2317 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002318 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2319 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2320 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2321 continue;
2322 }
2323
Willy Tarreau43241ff2019-10-09 11:27:51 +02002324 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002325 if (!stats_putchk(rep, htx, &trash))
2326 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002327 }
2328 } /* for sv */
2329
2330 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2331 /* fall through */
2332
2333 case STAT_PX_ST_BE:
2334 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002335 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002336 if (!stats_putchk(rep, htx, &trash))
2337 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002338 }
2339
2340 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2341 /* fall through */
2342
2343 case STAT_PX_ST_END:
2344 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2345 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002346 if (!stats_putchk(rep, htx, &trash))
2347 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002348 }
2349
2350 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2351 /* fall through */
2352
2353 case STAT_PX_ST_FIN:
2354 return 1;
2355
2356 default:
2357 /* unknown state, we should put an abort() here ! */
2358 return 1;
2359 }
Christopher Fauletef779222018-10-31 08:47:01 +01002360
2361 full:
2362 si_rx_room_blk(si);
2363 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002364}
2365
2366/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2367 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2368 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002369static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002370{
2371 /* WARNING! This must fit in the first buffer !!! */
2372 chunk_appendf(&trash,
2373 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2374 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2375 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03002376 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002377 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2378 "<style type=\"text/css\"><!--\n"
2379 "body {"
2380 " font-family: arial, helvetica, sans-serif;"
2381 " font-size: 12px;"
2382 " font-weight: normal;"
2383 " color: black;"
2384 " background: white;"
2385 "}\n"
2386 "th,td {"
2387 " font-size: 10px;"
2388 "}\n"
2389 "h1 {"
2390 " font-size: x-large;"
2391 " margin-bottom: 0.5em;"
2392 "}\n"
2393 "h2 {"
2394 " font-family: helvetica, arial;"
2395 " font-size: x-large;"
2396 " font-weight: bold;"
2397 " font-style: italic;"
2398 " color: #6020a0;"
2399 " margin-top: 0em;"
2400 " margin-bottom: 0em;"
2401 "}\n"
2402 "h3 {"
2403 " font-family: helvetica, arial;"
2404 " font-size: 16px;"
2405 " font-weight: bold;"
2406 " color: #b00040;"
2407 " background: #e8e8d0;"
2408 " margin-top: 0em;"
2409 " margin-bottom: 0em;"
2410 "}\n"
2411 "li {"
2412 " margin-top: 0.25em;"
2413 " margin-right: 2em;"
2414 "}\n"
2415 ".hr {margin-top: 0.25em;"
2416 " border-color: black;"
2417 " border-bottom-style: solid;"
2418 "}\n"
2419 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2420 ".total {background: #20D0D0;color: #ffff80;}\n"
2421 ".frontend {background: #e8e8d0;}\n"
2422 ".socket {background: #d0d0d0;}\n"
2423 ".backend {background: #e8e8d0;}\n"
2424 ".active_down {background: #ff9090;}\n"
2425 ".active_going_up {background: #ffd020;}\n"
2426 ".active_going_down {background: #ffffa0;}\n"
2427 ".active_up {background: #c0ffc0;}\n"
2428 ".active_nolb {background: #20a0ff;}\n"
2429 ".active_draining {background: #20a0FF;}\n"
2430 ".active_no_check {background: #e0e0e0;}\n"
2431 ".backup_down {background: #ff9090;}\n"
2432 ".backup_going_up {background: #ff80ff;}\n"
2433 ".backup_going_down {background: #c060ff;}\n"
2434 ".backup_up {background: #b0d0ff;}\n"
2435 ".backup_nolb {background: #90b0e0;}\n"
2436 ".backup_draining {background: #cc9900;}\n"
2437 ".backup_no_check {background: #e0e0e0;}\n"
2438 ".maintain {background: #c07820;}\n"
2439 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2440 "\n"
2441 "a.px:link {color: #ffff40; text-decoration: none;}"
2442 "a.px:visited {color: #ffff40; text-decoration: none;}"
2443 "a.px:hover {color: #ffffff; text-decoration: none;}"
2444 "a.lfsb:link {color: #000000; text-decoration: none;}"
2445 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2446 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2447 "\n"
2448 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2449 "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"
2450 "table.tbl td.ac { text-align: center;}\n"
2451 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2452 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2453 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2454 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2455 "\n"
2456 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2457 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2458 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2459 "table.det { border-collapse: collapse; border-style: none; }\n"
2460 "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"
2461 "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"
2462 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2463 "div.tips {\n"
2464 " display:block;\n"
2465 " visibility:hidden;\n"
2466 " z-index:2147483647;\n"
2467 " position:absolute;\n"
2468 " padding:2px 4px 3px;\n"
2469 " background:#f0f060; color:#000000;\n"
2470 " border:1px solid #7040c0;\n"
2471 " white-space:nowrap;\n"
2472 " font-style:normal;font-size:11px;font-weight:normal;\n"
2473 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2474 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2475 "}\n"
2476 "u:hover div.tips {visibility:visible;}\n"
2477 "-->\n"
2478 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002479 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002480 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002481 );
2482}
2483
2484/* Dumps the HTML stats information block to the trash for and uses the state from
2485 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2486 * for clearing the trash if needed.
2487 */
2488static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2489{
2490 struct appctx *appctx = __objt_appctx(si->end);
2491 unsigned int up = (now.tv_sec - start_date.tv_sec);
2492 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002493 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002494 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2495
2496 /* Turn the bytes per second to bits per second and take care of the
2497 * usual ethernet overhead in order to help figure how far we are from
2498 * interface saturation since it's the only case which usually matters.
2499 * For this we count the total size of an Ethernet frame on the wire
2500 * including preamble and IFG (1538) for the largest TCP segment it
2501 * transports (1448 with TCP timestamps). This is not valid for smaller
2502 * packets (under-estimated), but it gives a reasonably accurate
2503 * estimation of how far we are from uplink saturation.
2504 */
2505 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002506
2507 /* WARNING! this has to fit the first packet too.
2508 * We are around 3.5 kB, add adding entries will
2509 * become tricky if we want to support 4kB buffers !
2510 */
2511 chunk_appendf(&trash,
2512 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2513 PRODUCT_NAME "%s</a></h1>\n"
2514 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2515 "<hr width=\"100%%\" class=\"hr\">\n"
2516 "<h3>&gt; General process information</h3>\n"
2517 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002518 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002519 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2520 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2521 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002522 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002523 "Running tasks: %d/%d; idle = %d %%<br>\n"
2524 "</td><td align=\"center\" nowrap>\n"
2525 "<table class=\"lgd\"><tr>\n"
2526 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2527 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2528 "</tr><tr>\n"
2529 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2530 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2531 "</tr><tr>\n"
2532 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2533 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2534 "</tr><tr>\n"
2535 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2536 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2537 "</tr><tr>\n"
2538 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2539 "</tr><tr>\n"
2540 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2541 "</tr></table>\n"
2542 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2543 "</td>"
2544 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2545 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2546 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002547 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2548 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2549 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2550 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2551 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002552 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002553 up / 86400, (up % 86400) / 3600,
2554 (up % 3600) / 60, (up % 60),
2555 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2556 global.rlimit_memmax ? " MB" : "",
2557 global.rlimit_nofile,
2558 global.maxsock, global.maxconn, global.maxpipes,
2559 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002560 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2561 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002562 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002563 );
2564
2565 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002566 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002567 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2568
2569 chunk_appendf(&trash,
2570 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2571 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2572 STAT_SCOPE_TXT_MAXLEN);
2573
2574 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2575 scope_txt[0] = 0;
2576 if (appctx->ctx.stats.scope_len) {
2577 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002578 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002579 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2580 }
2581
2582 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2583 chunk_appendf(&trash,
2584 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2585 uri->uri_prefix,
2586 "",
2587 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2588 scope_txt);
2589 else
2590 chunk_appendf(&trash,
2591 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2592 uri->uri_prefix,
2593 ";up",
2594 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2595 scope_txt);
2596
2597 if (uri->refresh > 0) {
2598 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2599 chunk_appendf(&trash,
2600 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2601 uri->uri_prefix,
2602 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2603 "",
2604 scope_txt);
2605 else
2606 chunk_appendf(&trash,
2607 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2608 uri->uri_prefix,
2609 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2610 ";norefresh",
2611 scope_txt);
2612 }
2613
2614 chunk_appendf(&trash,
2615 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2616 uri->uri_prefix,
2617 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2618 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2619 scope_txt);
2620
2621 chunk_appendf(&trash,
2622 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2623 uri->uri_prefix,
2624 (uri->refresh > 0) ? ";norefresh" : "",
2625 scope_txt);
2626
2627 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002628 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2629 uri->uri_prefix,
2630 (uri->refresh > 0) ? ";norefresh" : "",
2631 scope_txt, uri->uri_prefix);
2632
2633 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002634 "</ul></td>"
2635 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2636 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2637 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2638 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2639 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2640 "</ul>"
2641 "</td>"
2642 "</tr></table>\n"
2643 ""
2644 );
2645
2646 if (appctx->ctx.stats.st_code) {
2647 switch (appctx->ctx.stats.st_code) {
2648 case STAT_STATUS_DONE:
2649 chunk_appendf(&trash,
2650 "<p><div class=active_up>"
2651 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2652 "Action processed successfully."
2653 "</div>\n", uri->uri_prefix,
2654 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2655 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2656 scope_txt);
2657 break;
2658 case STAT_STATUS_NONE:
2659 chunk_appendf(&trash,
2660 "<p><div class=active_going_down>"
2661 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2662 "Nothing has changed."
2663 "</div>\n", uri->uri_prefix,
2664 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2665 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2666 scope_txt);
2667 break;
2668 case STAT_STATUS_PART:
2669 chunk_appendf(&trash,
2670 "<p><div class=active_going_down>"
2671 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2672 "Action partially processed.<br>"
2673 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2674 "</div>\n", uri->uri_prefix,
2675 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2676 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2677 scope_txt);
2678 break;
2679 case STAT_STATUS_ERRP:
2680 chunk_appendf(&trash,
2681 "<p><div class=active_down>"
2682 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2683 "Action not processed because of invalid parameters."
2684 "<ul>"
2685 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002686 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002687 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2688 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2689 "</ul>"
2690 "</div>\n", uri->uri_prefix,
2691 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2692 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2693 scope_txt);
2694 break;
2695 case STAT_STATUS_EXCD:
2696 chunk_appendf(&trash,
2697 "<p><div class=active_down>"
2698 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2699 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2700 "You should retry with less servers at a time.</b>"
2701 "</div>\n", uri->uri_prefix,
2702 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2703 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2704 scope_txt);
2705 break;
2706 case STAT_STATUS_DENY:
2707 chunk_appendf(&trash,
2708 "<p><div class=active_down>"
2709 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2710 "<b>Action denied.</b>"
2711 "</div>\n", uri->uri_prefix,
2712 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2713 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2714 scope_txt);
2715 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002716 case STAT_STATUS_IVAL:
2717 chunk_appendf(&trash,
2718 "<p><div class=active_down>"
2719 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2720 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2721 "</div>\n", uri->uri_prefix,
2722 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2723 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2724 scope_txt);
2725 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002726 default:
2727 chunk_appendf(&trash,
2728 "<p><div class=active_no_check>"
2729 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2730 "Unexpected result."
2731 "</div>\n", uri->uri_prefix,
2732 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2733 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2734 scope_txt);
2735 }
2736 chunk_appendf(&trash, "<p>\n");
2737 }
2738}
2739
2740/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2741 * for clearing the trash if needed.
2742 */
2743static void stats_dump_html_end()
2744{
2745 chunk_appendf(&trash, "</body></html>\n");
2746}
2747
Simon Horman05ee2132017-01-04 09:37:25 +01002748/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2749 * for clearing it if needed.
2750 */
2751static void stats_dump_json_header()
2752{
2753 chunk_strcat(&trash, "[");
2754}
2755
2756
2757/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2758 * for clearing the trash if needed.
2759 */
2760static void stats_dump_json_end()
2761{
2762 chunk_strcat(&trash, "]");
2763}
2764
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002765/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
2766 * a pointer to the current server/listener.
2767 */
2768static int stats_dump_proxies(struct stream_interface *si,
2769 struct htx *htx,
2770 struct uri_auth *uri)
2771{
2772 struct appctx *appctx = __objt_appctx(si->end);
2773 struct channel *rep = si_ic(si);
2774 struct proxy *px;
2775
2776 /* dump proxies */
2777 while (appctx->ctx.stats.obj1) {
2778 if (htx) {
2779 if (htx_almost_full(htx))
2780 goto full;
2781 }
2782 else {
2783 if (buffer_almost_full(&rep->buf))
2784 goto full;
2785 }
2786
2787 px = appctx->ctx.stats.obj1;
2788 /* skip the disabled proxies, global frontend and non-networked ones */
2789 if (px->state != PR_STSTOPPED && px->uuid > 0
2790 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
2791
2792 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
2793 return 0;
2794 }
2795
2796 appctx->ctx.stats.obj1 = px->next;
2797 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2798 }
2799
2800 return 1;
2801
2802 full:
2803 si_rx_room_blk(si);
2804 return 0;
2805}
2806
William Lallemand74c24fb2016-11-21 17:18:36 +01002807/* This function dumps statistics onto the stream interface's read buffer in
2808 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2809 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2810 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2811 * and the stream must be closed, or -1 in case of any error. This function is
2812 * used by both the CLI and the HTTP handlers.
2813 */
Christopher Fauletef779222018-10-31 08:47:01 +01002814static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2815 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002816{
2817 struct appctx *appctx = __objt_appctx(si->end);
2818 struct channel *rep = si_ic(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002819
2820 chunk_reset(&trash);
2821
2822 switch (appctx->st2) {
2823 case STAT_ST_INIT:
2824 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2825 /* fall through */
2826
2827 case STAT_ST_HEAD:
2828 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002829 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002830 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2831 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002832 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002833 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002834 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2835 stats_dump_csv_header();
2836
Christopher Fauletef779222018-10-31 08:47:01 +01002837 if (!stats_putchk(rep, htx, &trash))
2838 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002839
Christopher Faulet6338a082019-09-09 15:50:54 +02002840 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2841 appctx->st2 = STAT_ST_FIN;
2842 return 1;
2843 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002844 appctx->st2 = STAT_ST_INFO;
2845 /* fall through */
2846
2847 case STAT_ST_INFO:
2848 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2849 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002850 if (!stats_putchk(rep, htx, &trash))
2851 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002852 }
2853
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002854 appctx->ctx.stats.obj1 = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002855 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2856 appctx->st2 = STAT_ST_LIST;
2857 /* fall through */
2858
2859 case STAT_ST_LIST:
2860 /* dump proxies */
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002861 if (!stats_dump_proxies(si, htx, uri))
2862 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002863
2864 appctx->st2 = STAT_ST_END;
2865 /* fall through */
2866
2867 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002868 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2869 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2870 stats_dump_html_end();
2871 else
2872 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002873 if (!stats_putchk(rep, htx, &trash))
2874 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002875 }
2876
2877 appctx->st2 = STAT_ST_FIN;
2878 /* fall through */
2879
2880 case STAT_ST_FIN:
2881 return 1;
2882
2883 default:
2884 /* unknown state ! */
2885 appctx->st2 = STAT_ST_FIN;
2886 return -1;
2887 }
Christopher Fauletef779222018-10-31 08:47:01 +01002888
2889 full:
2890 si_rx_room_blk(si);
2891 return 0;
2892
William Lallemand74c24fb2016-11-21 17:18:36 +01002893}
2894
2895/* We reached the stats page through a POST request. The appctx is
2896 * expected to have already been allocated by the caller.
2897 * Parse the posted data and enable/disable servers if necessary.
2898 * Returns 1 if request was parsed or zero if it needs more data.
2899 */
2900static int stats_process_http_post(struct stream_interface *si)
2901{
2902 struct stream *s = si_strm(si);
2903 struct appctx *appctx = objt_appctx(si->end);
2904
2905 struct proxy *px = NULL;
2906 struct server *sv = NULL;
2907
2908 char key[LINESIZE];
2909 int action = ST_ADM_ACTION_NONE;
2910 int reprocess = 0;
2911
2912 int total_servers = 0;
2913 int altered_servers = 0;
2914
2915 char *first_param, *cur_param, *next_param, *end_params;
2916 char *st_cur_param = NULL;
2917 char *st_next_param = NULL;
2918
Christopher Fauleta3618372018-12-13 21:59:56 +01002919 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002920
Christopher Fauletb7f88902019-07-15 21:56:43 +02002921 struct htx *htx = htxbuf(&s->req.buf);
2922 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002923
Christopher Fauletb7f88902019-07-15 21:56:43 +02002924 /* we need more data */
2925 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2926 /* check if we can receive more */
2927 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2928 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2929 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002930 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002931 goto wait;
2932 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002933
Christopher Fauletb7f88902019-07-15 21:56:43 +02002934 /* The request was fully received. Copy data */
2935 blk = htx_get_head_blk(htx);
2936 while (blk) {
2937 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002938
Christopher Fauletb7f88902019-07-15 21:56:43 +02002939 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2940 break;
2941 if (type == HTX_BLK_DATA) {
2942 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002943
Christopher Fauletb7f88902019-07-15 21:56:43 +02002944 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002945 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2946 goto out;
2947 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002948 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002949 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002950 }
2951
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002952 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002953 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002954 cur_param = next_param = end_params;
2955 *end_params = '\0';
2956
2957 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2958
2959 /*
2960 * Parse the parameters in reverse order to only store the last value.
2961 * From the html form, the backend and the action are at the end.
2962 */
2963 while (cur_param > first_param) {
2964 char *value;
2965 int poffset, plen;
2966
2967 cur_param--;
2968
2969 if ((*cur_param == '&') || (cur_param == first_param)) {
2970 reprocess_servers:
2971 /* Parse the key */
2972 poffset = (cur_param != first_param ? 1 : 0);
2973 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2974 if ((plen > 0) && (plen <= sizeof(key))) {
2975 strncpy(key, cur_param + poffset, plen);
2976 key[plen - 1] = '\0';
2977 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002978 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002979 goto out;
2980 }
2981
2982 /* Parse the value */
2983 value = key;
2984 while (*value != '\0' && *value != '=') {
2985 value++;
2986 }
2987 if (*value == '=') {
2988 /* Ok, a value is found, we can mark the end of the key */
2989 *value++ = '\0';
2990 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002991 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002992 break;
2993
2994 /* Now we can check the key to see what to do */
2995 if (!px && (strcmp(key, "b") == 0)) {
2996 if ((px = proxy_be_by_name(value)) == NULL) {
2997 /* the backend name is unknown or ambiguous (duplicate names) */
2998 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2999 goto out;
3000 }
3001 }
3002 else if (!action && (strcmp(key, "action") == 0)) {
3003 if (strcmp(value, "ready") == 0) {
3004 action = ST_ADM_ACTION_READY;
3005 }
3006 else if (strcmp(value, "drain") == 0) {
3007 action = ST_ADM_ACTION_DRAIN;
3008 }
3009 else if (strcmp(value, "maint") == 0) {
3010 action = ST_ADM_ACTION_MAINT;
3011 }
3012 else if (strcmp(value, "shutdown") == 0) {
3013 action = ST_ADM_ACTION_SHUTDOWN;
3014 }
3015 else if (strcmp(value, "dhlth") == 0) {
3016 action = ST_ADM_ACTION_DHLTH;
3017 }
3018 else if (strcmp(value, "ehlth") == 0) {
3019 action = ST_ADM_ACTION_EHLTH;
3020 }
3021 else if (strcmp(value, "hrunn") == 0) {
3022 action = ST_ADM_ACTION_HRUNN;
3023 }
3024 else if (strcmp(value, "hnolb") == 0) {
3025 action = ST_ADM_ACTION_HNOLB;
3026 }
3027 else if (strcmp(value, "hdown") == 0) {
3028 action = ST_ADM_ACTION_HDOWN;
3029 }
3030 else if (strcmp(value, "dagent") == 0) {
3031 action = ST_ADM_ACTION_DAGENT;
3032 }
3033 else if (strcmp(value, "eagent") == 0) {
3034 action = ST_ADM_ACTION_EAGENT;
3035 }
3036 else if (strcmp(value, "arunn") == 0) {
3037 action = ST_ADM_ACTION_ARUNN;
3038 }
3039 else if (strcmp(value, "adown") == 0) {
3040 action = ST_ADM_ACTION_ADOWN;
3041 }
3042 /* else these are the old supported methods */
3043 else if (strcmp(value, "disable") == 0) {
3044 action = ST_ADM_ACTION_DISABLE;
3045 }
3046 else if (strcmp(value, "enable") == 0) {
3047 action = ST_ADM_ACTION_ENABLE;
3048 }
3049 else if (strcmp(value, "stop") == 0) {
3050 action = ST_ADM_ACTION_STOP;
3051 }
3052 else if (strcmp(value, "start") == 0) {
3053 action = ST_ADM_ACTION_START;
3054 }
3055 else {
3056 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3057 goto out;
3058 }
3059 }
3060 else if (strcmp(key, "s") == 0) {
3061 if (!(px && action)) {
3062 /*
3063 * Indicates that we'll need to reprocess the parameters
3064 * as soon as backend and action are known
3065 */
3066 if (!reprocess) {
3067 st_cur_param = cur_param;
3068 st_next_param = next_param;
3069 }
3070 reprocess = 1;
3071 }
3072 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003073 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003074 switch (action) {
3075 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003076 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003077 altered_servers++;
3078 total_servers++;
3079 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3080 }
3081 break;
3082 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003083 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003084 altered_servers++;
3085 total_servers++;
3086 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3087 }
3088 break;
3089 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003090 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003091 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3092 altered_servers++;
3093 total_servers++;
3094 }
3095 break;
3096 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003097 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003098 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3099 altered_servers++;
3100 total_servers++;
3101 }
3102 break;
3103 case ST_ADM_ACTION_DHLTH:
3104 if (sv->check.state & CHK_ST_CONFIGURED) {
3105 sv->check.state &= ~CHK_ST_ENABLED;
3106 altered_servers++;
3107 total_servers++;
3108 }
3109 break;
3110 case ST_ADM_ACTION_EHLTH:
3111 if (sv->check.state & CHK_ST_CONFIGURED) {
3112 sv->check.state |= CHK_ST_ENABLED;
3113 altered_servers++;
3114 total_servers++;
3115 }
3116 break;
3117 case ST_ADM_ACTION_HRUNN:
3118 if (!(sv->track)) {
3119 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003120 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003121 altered_servers++;
3122 total_servers++;
3123 }
3124 break;
3125 case ST_ADM_ACTION_HNOLB:
3126 if (!(sv->track)) {
3127 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003128 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003129 altered_servers++;
3130 total_servers++;
3131 }
3132 break;
3133 case ST_ADM_ACTION_HDOWN:
3134 if (!(sv->track)) {
3135 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003136 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003137 altered_servers++;
3138 total_servers++;
3139 }
3140 break;
3141 case ST_ADM_ACTION_DAGENT:
3142 if (sv->agent.state & CHK_ST_CONFIGURED) {
3143 sv->agent.state &= ~CHK_ST_ENABLED;
3144 altered_servers++;
3145 total_servers++;
3146 }
3147 break;
3148 case ST_ADM_ACTION_EAGENT:
3149 if (sv->agent.state & CHK_ST_CONFIGURED) {
3150 sv->agent.state |= CHK_ST_ENABLED;
3151 altered_servers++;
3152 total_servers++;
3153 }
3154 break;
3155 case ST_ADM_ACTION_ARUNN:
3156 if (sv->agent.state & CHK_ST_ENABLED) {
3157 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003158 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003159 altered_servers++;
3160 total_servers++;
3161 }
3162 break;
3163 case ST_ADM_ACTION_ADOWN:
3164 if (sv->agent.state & CHK_ST_ENABLED) {
3165 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003166 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003167 altered_servers++;
3168 total_servers++;
3169 }
3170 break;
3171 case ST_ADM_ACTION_READY:
3172 srv_adm_set_ready(sv);
3173 altered_servers++;
3174 total_servers++;
3175 break;
3176 case ST_ADM_ACTION_DRAIN:
3177 srv_adm_set_drain(sv);
3178 altered_servers++;
3179 total_servers++;
3180 break;
3181 case ST_ADM_ACTION_MAINT:
3182 srv_adm_set_maint(sv);
3183 altered_servers++;
3184 total_servers++;
3185 break;
3186 case ST_ADM_ACTION_SHUTDOWN:
3187 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003188 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003189 altered_servers++;
3190 total_servers++;
3191 }
3192 break;
3193 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003194 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003195 } else {
3196 /* the server name is unknown or ambiguous (duplicate names) */
3197 total_servers++;
3198 }
3199 }
3200 if (reprocess && px && action) {
3201 /* Now, we know the backend and the action chosen by the user.
3202 * We can safely restart from the first server parameter
3203 * to reprocess them
3204 */
3205 cur_param = st_cur_param;
3206 next_param = st_next_param;
3207 reprocess = 0;
3208 goto reprocess_servers;
3209 }
3210
3211 next_param = cur_param;
3212 }
3213 }
3214
3215 if (total_servers == 0) {
3216 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3217 }
3218 else if (altered_servers == 0) {
3219 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3220 }
3221 else if (altered_servers == total_servers) {
3222 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3223 }
3224 else {
3225 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3226 }
3227 out:
3228 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003229 wait:
3230 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3231 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003232}
3233
3234
Christopher Fauletb7f88902019-07-15 21:56:43 +02003235static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003236{
3237 struct stream *s = si_strm(si);
3238 struct uri_auth *uri = s->be->uri_auth;
3239 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003240 struct htx_sl *sl;
3241 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003242
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003243 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);
3244 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3245 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003246 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003247 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003248
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003249 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003250 goto full;
3251 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3252 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3253 goto full;
3254 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003255 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3256 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3257 goto full;
3258 }
Christopher Fauletef779222018-10-31 08:47:01 +01003259 else {
3260 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3261 goto full;
3262 }
3263
3264 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3265 const char *refresh = U2A(uri->refresh);
3266 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3267 goto full;
3268 }
3269
3270 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3271 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3272 goto full;
3273 }
3274
3275 if (!htx_add_endof(htx, HTX_BLK_EOH))
3276 goto full;
3277
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003278 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003279 return 1;
3280
3281 full:
3282 htx_reset(htx);
3283 si_rx_room_blk(si);
3284 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003285}
3286
Christopher Fauletef779222018-10-31 08:47:01 +01003287
Christopher Fauletb7f88902019-07-15 21:56:43 +02003288static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003289{
3290 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3291 struct stream *s = si_strm(si);
3292 struct uri_auth *uri = s->be->uri_auth;
3293 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003294 struct htx_sl *sl;
3295 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003296
3297 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3298 scope_txt[0] = 0;
3299 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003300 const char *scope_ptr = stats_scope_ptr(appctx, si);
3301
Christopher Fauletef779222018-10-31 08:47:01 +01003302 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003303 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003304 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3305 }
3306
3307 /* We don't want to land on the posted stats page because a refresh will
3308 * repost the data. We don't want this to happen on accident so we redirect
3309 * the browse to the stats page with a GET.
3310 */
3311 chunk_printf(&trash, "%s;st=%s%s%s%s",
3312 uri->uri_prefix,
3313 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3314 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3315 stat_status_codes[appctx->ctx.stats.st_code]) ?
3316 stat_status_codes[appctx->ctx.stats.st_code] :
3317 stat_status_codes[STAT_STATUS_UNKN],
3318 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3319 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3320 scope_txt);
3321
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003322 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3323 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3324 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003325 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003326 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003327
3328 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003329 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3330 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3331 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3332 goto full;
3333
3334 if (!htx_add_endof(htx, HTX_BLK_EOH))
3335 goto full;
3336
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003337 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003338 return 1;
3339
3340full:
3341 htx_reset(htx);
3342 si_rx_room_blk(si);
3343 return 0;
3344}
William Lallemand74c24fb2016-11-21 17:18:36 +01003345
Simon Horman05ee2132017-01-04 09:37:25 +01003346
William Lallemand74c24fb2016-11-21 17:18:36 +01003347/* This I/O handler runs as an applet embedded in a stream interface. It is
3348 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3349 * appctx->st0 contains the operation in progress (dump, done). The handler
3350 * automatically unregisters itself once transfer is complete.
3351 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003352static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003353{
3354 struct stream_interface *si = appctx->owner;
3355 struct stream *s = si_strm(si);
3356 struct channel *req = si_oc(si);
3357 struct channel *res = si_ic(si);
3358 struct htx *req_htx, *res_htx;
3359
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003360 /* only proxy stats are available via http */
3361 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3362
Christopher Fauletef779222018-10-31 08:47:01 +01003363 res_htx = htx_from_buf(&res->buf);
3364
3365 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3366 goto out;
3367
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003368 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003369 if (!b_size(&res->buf)) {
3370 si_rx_room_blk(si);
3371 goto out;
3372 }
3373
3374 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003375 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3376 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003377
3378 /* all states are processed in sequence */
3379 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003380 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003381 if (s->txn->meth == HTTP_METH_HEAD)
3382 appctx->st0 = STAT_HTTP_DONE;
3383 else
3384 appctx->st0 = STAT_HTTP_DUMP;
3385 }
3386 }
3387
3388 if (appctx->st0 == STAT_HTTP_DUMP) {
3389 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3390 appctx->st0 = STAT_HTTP_DONE;
3391 }
3392
3393 if (appctx->st0 == STAT_HTTP_POST) {
3394 if (stats_process_http_post(si))
3395 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003396 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003397 appctx->st0 = STAT_HTTP_DONE;
3398 }
3399
3400 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003401 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003402 appctx->st0 = STAT_HTTP_DONE;
3403 }
3404
3405 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003406 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003407 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003408 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3409 si_rx_room_blk(si);
3410 goto out;
3411 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003412 channel_add_input(&s->res, 1);
3413 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003414 }
3415
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003416 if (appctx->st0 == STAT_HTTP_END) {
3417 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003418 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003419 si_shutr(si);
3420 }
3421
3422 /* eat the whole request */
3423 if (co_data(req)) {
3424 req_htx = htx_from_buf(&req->buf);
3425 co_htx_skip(req, req_htx, co_data(req));
3426 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003427 }
3428 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003429
Christopher Fauletef779222018-10-31 08:47:01 +01003430 out:
3431 /* we have left the request in the buffer for the case where we
3432 * process a POST, and this automatically re-enables activity on
3433 * read. It's better to indicate that we want to stop reading when
3434 * we're sending, so that we know there's at most one direction
3435 * deciding to wake the applet up. It saves it from looping when
3436 * emitting large blocks into small TCP windows.
3437 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003438 htx_to_buf(res_htx, &res->buf);
3439 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003440 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003441}
3442
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003443/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003444static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003445 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003446{
3447 int field;
3448
3449 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3450 if (!field_format(info, field))
3451 continue;
3452
Willy Tarreaueaa55372019-10-09 07:39:11 +02003453 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003454 return 0;
3455 if (!stats_emit_raw_data_field(out, &info[field]))
3456 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003457 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3458 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003459 if (!chunk_strcat(out, "\n"))
3460 return 0;
3461 }
3462 return 1;
3463}
3464
3465/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003466static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003467 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003468{
3469 int field;
3470
3471 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3472 if (!field_format(info, field))
3473 continue;
3474
Willy Tarreaueaa55372019-10-09 07:39:11 +02003475 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 +01003476 return 0;
3477 if (!stats_emit_field_tags(out, &info[field], ':'))
3478 return 0;
3479 if (!stats_emit_typed_data_field(out, &info[field]))
3480 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003481 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3482 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003483 if (!chunk_strcat(out, "\n"))
3484 return 0;
3485 }
3486 return 1;
3487}
3488
3489/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003490 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003491 * INF_TOTAL_FIELDS. If this length is less then this value, the
3492 * function returns 0, otherwise, it returns 1.
3493 */
3494int stats_fill_info(struct field *info, int len)
3495{
3496 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003497 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003498
3499#ifdef USE_OPENSSL
3500 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3501 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3502 int ssl_reuse = 0;
3503
3504 if (ssl_key_rate < ssl_sess_rate) {
3505 /* count the ssl reuse ratio and avoid overflows in both directions */
3506 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3507 }
3508#endif
3509
3510 if (len < INF_TOTAL_FIELDS)
3511 return 0;
3512
3513 chunk_reset(out);
3514 memset(info, 0, sizeof(*info) * len);
3515
3516 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003517 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3518 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003519
Yves Lafon95317282018-02-26 11:10:37 +01003520 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003521 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3522 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3523 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3524
3525 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3526 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3527
3528 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3529 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3530 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3531 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3532 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3533 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3534 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3535 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3536 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3537 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3538 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3539 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3540#ifdef USE_OPENSSL
3541 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3542 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3543 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3544#endif
3545 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3546 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3547 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3548 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3549 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3550 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3551 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3552 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3553 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3554
3555#ifdef USE_OPENSSL
3556 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3557 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3558 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3559 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3560 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3561 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3562 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3563 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3564 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3565 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3566#endif
3567 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3568 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3569 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3570#ifdef USE_ZLIB
3571 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3572 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3573#endif
3574 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003575 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003576 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003577 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3578 if (global.desc)
3579 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003580 info[INF_STOPPING] = mkf_u32(0, stopping);
3581 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003582 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003583 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003584 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003585 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003586 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003587 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003588 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003589 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003590 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003591 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 +02003592 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003593 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003594
3595 return 1;
3596}
3597
3598/* This function dumps information onto the stream interface's read buffer.
3599 * It returns 0 as long as it does not complete, non-zero upon completion.
3600 * No state is used.
3601 */
3602static int stats_dump_info_to_buffer(struct stream_interface *si)
3603{
3604 struct appctx *appctx = __objt_appctx(si->end);
3605
3606 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3607 return 0;
3608
3609 chunk_reset(&trash);
3610
3611 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003612 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003613 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003614 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003615 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003616 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003617
Willy Tarreau06d80a92017-10-19 14:32:15 +02003618 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003619 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003620 return 0;
3621 }
3622
3623 return 1;
3624}
3625
Simon Horman6f6bb382017-01-04 09:37:26 +01003626/* This function dumps the schema onto the stream interface's read buffer.
3627 * It returns 0 as long as it does not complete, non-zero upon completion.
3628 * No state is used.
3629 *
3630 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3631 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3632 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003633static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003634{
3635
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003636 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003637
3638 chunk_strcat(out,
3639 "{"
3640 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3641 "\"oneOf\":["
3642 "{"
3643 "\"title\":\"Info\","
3644 "\"type\":\"array\","
3645 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003646 "\"title\":\"InfoItem\","
3647 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003648 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003649 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3650 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3651 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3652 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3653 "},"
3654 "\"required\":[\"field\",\"processNum\",\"tags\","
3655 "\"value\"]"
3656 "}"
3657 "},"
3658 "{"
3659 "\"title\":\"Stat\","
3660 "\"type\":\"array\","
3661 "\"items\":{"
3662 "\"title\":\"InfoItem\","
3663 "\"type\":\"object\","
3664 "\"properties\":{"
3665 "\"objType\":{"
3666 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3667 "\"Server\",\"Unknown\"]"
3668 "},"
3669 "\"proxyId\":{"
3670 "\"type\":\"integer\","
3671 "\"minimum\":0"
3672 "},"
3673 "\"id\":{"
3674 "\"type\":\"integer\","
3675 "\"minimum\":0"
3676 "},"
3677 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3678 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3679 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3680 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3681 "},"
3682 "\"required\":[\"objType\",\"proxyId\",\"id\","
3683 "\"field\",\"processNum\",\"tags\","
3684 "\"value\"]"
3685 "}"
3686 "},"
3687 "{"
3688 "\"title\":\"Error\","
3689 "\"type\":\"object\","
3690 "\"properties\":{"
3691 "\"errorStr\":{"
3692 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003693 "}"
3694 "},"
3695 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01003696 "}"
3697 "],"
3698 "\"definitions\":{"
3699 "\"field\":{"
3700 "\"type\":\"object\","
3701 "\"pos\":{"
3702 "\"type\":\"integer\","
3703 "\"minimum\":0"
3704 "},"
3705 "\"name\":{"
3706 "\"type\":\"string\""
3707 "},"
3708 "\"required\":[\"pos\",\"name\"]"
3709 "},"
3710 "\"processNum\":{"
3711 "\"type\":\"integer\","
3712 "\"minimum\":1"
3713 "},"
3714 "\"tags\":{"
3715 "\"type\":\"object\","
3716 "\"origin\":{"
3717 "\"type\":\"string\","
3718 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3719 "\"Config\",\"Product\",\"Unknown\"]"
3720 "},"
3721 "\"nature\":{"
3722 "\"type\":\"string\","
3723 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3724 "\"Rate\",\"Counter\",\"Duration\","
3725 "\"Age\",\"Time\",\"Name\",\"Output\","
3726 "\"Avg\", \"Unknown\"]"
3727 "},"
3728 "\"scope\":{"
3729 "\"type\":\"string\","
3730 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3731 "\"System\",\"Unknown\"]"
3732 "},"
3733 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3734 "},"
3735 "\"typedValue\":{"
3736 "\"type\":\"object\","
3737 "\"oneOf\":["
3738 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3739 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3740 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3741 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3742 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3743 "],"
3744 "\"definitions\":{"
3745 "\"s32Value\":{"
3746 "\"properties\":{"
3747 "\"type\":{"
3748 "\"type\":\"string\","
3749 "\"enum\":[\"s32\"]"
3750 "},"
3751 "\"value\":{"
3752 "\"type\":\"integer\","
3753 "\"minimum\":-2147483648,"
3754 "\"maximum\":2147483647"
3755 "}"
3756 "},"
3757 "\"required\":[\"type\",\"value\"]"
3758 "},"
3759 "\"s64Value\":{"
3760 "\"properties\":{"
3761 "\"type\":{"
3762 "\"type\":\"string\","
3763 "\"enum\":[\"s64\"]"
3764 "},"
3765 "\"value\":{"
3766 "\"type\":\"integer\","
3767 "\"minimum\":-9007199254740991,"
3768 "\"maximum\":9007199254740991"
3769 "}"
3770 "},"
3771 "\"required\":[\"type\",\"value\"]"
3772 "},"
3773 "\"u32Value\":{"
3774 "\"properties\":{"
3775 "\"type\":{"
3776 "\"type\":\"string\","
3777 "\"enum\":[\"u32\"]"
3778 "},"
3779 "\"value\":{"
3780 "\"type\":\"integer\","
3781 "\"minimum\":0,"
3782 "\"maximum\":4294967295"
3783 "}"
3784 "},"
3785 "\"required\":[\"type\",\"value\"]"
3786 "},"
3787 "\"u64Value\":{"
3788 "\"properties\":{"
3789 "\"type\":{"
3790 "\"type\":\"string\","
3791 "\"enum\":[\"u64\"]"
3792 "},"
3793 "\"value\":{"
3794 "\"type\":\"integer\","
3795 "\"minimum\":0,"
3796 "\"maximum\":9007199254740991"
3797 "}"
3798 "},"
3799 "\"required\":[\"type\",\"value\"]"
3800 "},"
3801 "\"strValue\":{"
3802 "\"properties\":{"
3803 "\"type\":{"
3804 "\"type\":\"string\","
3805 "\"enum\":[\"str\"]"
3806 "},"
3807 "\"value\":{\"type\":\"string\"}"
3808 "},"
3809 "\"required\":[\"type\",\"value\"]"
3810 "},"
3811 "\"unknownValue\":{"
3812 "\"properties\":{"
3813 "\"type\":{"
3814 "\"type\":\"integer\","
3815 "\"minimum\":0"
3816 "},"
3817 "\"value\":{"
3818 "\"type\":\"string\","
3819 "\"enum\":[\"unknown\"]"
3820 "}"
3821 "},"
3822 "\"required\":[\"type\",\"value\"]"
3823 "}"
3824 "}"
3825 "}"
3826 "}"
3827 "}");
3828
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003829 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003830 chunk_reset(out);
3831 chunk_appendf(out,
3832 "{\"errorStr\":\"output buffer too short\"}");
3833 }
3834}
3835
3836/* This function dumps the schema onto the stream interface's read buffer.
3837 * It returns 0 as long as it does not complete, non-zero upon completion.
3838 * No state is used.
3839 */
3840static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3841{
3842 chunk_reset(&trash);
3843
3844 stats_dump_json_schema(&trash);
3845
Willy Tarreau06d80a92017-10-19 14:32:15 +02003846 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003847 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003848 return 0;
3849 }
3850
3851 return 1;
3852}
3853
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003854static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003855{
3856 struct proxy *px;
3857 struct server *sv;
3858 struct listener *li;
3859 int clrall = 0;
3860
3861 if (strcmp(args[2], "all") == 0)
3862 clrall = 1;
3863
3864 /* check permissions */
3865 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3866 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3867 return 1;
3868
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003869 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003870 if (clrall) {
3871 memset(&px->be_counters, 0, sizeof(px->be_counters));
3872 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3873 }
3874 else {
3875 px->be_counters.conn_max = 0;
3876 px->be_counters.p.http.rps_max = 0;
3877 px->be_counters.sps_max = 0;
3878 px->be_counters.cps_max = 0;
3879 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003880 px->be_counters.qtime_max = 0;
3881 px->be_counters.ctime_max = 0;
3882 px->be_counters.dtime_max = 0;
3883 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003884
3885 px->fe_counters.conn_max = 0;
3886 px->fe_counters.p.http.rps_max = 0;
3887 px->fe_counters.sps_max = 0;
3888 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003889 }
3890
3891 for (sv = px->srv; sv; sv = sv->next)
3892 if (clrall)
3893 memset(&sv->counters, 0, sizeof(sv->counters));
3894 else {
3895 sv->counters.cur_sess_max = 0;
3896 sv->counters.nbpend_max = 0;
3897 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003898 sv->counters.qtime_max = 0;
3899 sv->counters.ctime_max = 0;
3900 sv->counters.dtime_max = 0;
3901 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003902 }
3903
3904 list_for_each_entry(li, &px->conf.listeners, by_fe)
3905 if (li->counters) {
3906 if (clrall)
3907 memset(li->counters, 0, sizeof(*li->counters));
3908 else
3909 li->counters->conn_max = 0;
3910 }
3911 }
3912
3913 global.cps_max = 0;
3914 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003915 global.ssl_max = 0;
3916 global.ssl_fe_keys_max = 0;
3917 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003918
3919 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003920 return 1;
3921}
3922
3923
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003924static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003925{
Willy Tarreau2f397382019-10-09 11:43:59 +02003926 int arg = 2;
3927
Willy Tarreaud25fc792016-12-16 12:33:47 +01003928 appctx->ctx.stats.scope_str = 0;
3929 appctx->ctx.stats.scope_len = 0;
3930 appctx->ctx.stats.flags = 0;
3931
Willy Tarreau2f397382019-10-09 11:43:59 +02003932 while (*args[arg]) {
3933 if (strcmp(args[arg], "typed") == 0)
3934 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3935 else if (strcmp(args[arg], "json") == 0)
3936 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3937 else if (strcmp(args[arg], "desc") == 0)
3938 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3939 arg++;
3940 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003941 return 0;
3942}
3943
3944
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003945static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003946{
Willy Tarreau2f397382019-10-09 11:43:59 +02003947 int arg = 2;
3948
Willy Tarreaud25fc792016-12-16 12:33:47 +01003949 appctx->ctx.stats.scope_str = 0;
3950 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003951 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3952
3953 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3954 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003955
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003956 /* proxy is the default domain */
3957 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3958 if (!strcmp(args[arg], "domain")) {
3959 ++args;
3960
3961 if (!strcmp(args[arg], "proxy"))
3962 ++args;
3963 else
3964 return cli_err(appctx, "Invalid statistics domain.\n");
3965 }
3966
3967 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
3968 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003969 struct proxy *px;
3970
Willy Tarreau2f397382019-10-09 11:43:59 +02003971 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003972 if (px)
3973 appctx->ctx.stats.iid = px->uuid;
3974 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003975 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003976
Willy Tarreau9d008692019-08-09 11:21:01 +02003977 if (!appctx->ctx.stats.iid)
3978 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003979
Willy Tarreau2b812e22016-11-22 16:18:05 +01003980 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003981 appctx->ctx.stats.type = atoi(args[arg+1]);
3982 appctx->ctx.stats.sid = atoi(args[arg+2]);
3983 arg += 3;
3984 }
3985
3986 while (*args[arg]) {
3987 if (strcmp(args[arg], "typed") == 0)
3988 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3989 else if (strcmp(args[arg], "json") == 0)
3990 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3991 else if (strcmp(args[arg], "desc") == 0)
3992 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3993 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003994 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003995
Willy Tarreau2b812e22016-11-22 16:18:05 +01003996 return 0;
3997}
3998
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003999static int cli_io_handler_dump_info(struct appctx *appctx)
4000{
4001 return stats_dump_info_to_buffer(appctx->owner);
4002}
4003
Willy Tarreau2b812e22016-11-22 16:18:05 +01004004/* This I/O handler runs as an applet embedded in a stream interface. It is
4005 * used to send raw stats over a socket.
4006 */
4007static int cli_io_handler_dump_stat(struct appctx *appctx)
4008{
Christopher Fauletef779222018-10-31 08:47:01 +01004009 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004010}
4011
Simon Horman6f6bb382017-01-04 09:37:26 +01004012static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4013{
4014 return stats_dump_json_schema_to_buffer(appctx->owner);
4015}
4016
Willy Tarreau2b812e22016-11-22 16:18:05 +01004017/* register cli keywords */
4018static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004019 { { "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 +02004020 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
4021 { { "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 +01004022 { { "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 +01004023 {{},}
4024}};
4025
Willy Tarreau0108d902018-11-25 19:14:37 +01004026INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4027
William Lallemand74c24fb2016-11-21 17:18:36 +01004028struct applet http_stats_applet = {
4029 .obj_type = OBJ_TYPE_APPLET,
4030 .name = "<STATS>", /* used for logging */
4031 .fct = http_stats_io_handler,
4032 .release = NULL,
4033};
4034
William Lallemand74c24fb2016-11-21 17:18:36 +01004035/*
4036 * Local variables:
4037 * c-indent-level: 8
4038 * c-basic-offset: 8
4039 * End:
4040 */