blob: 6ab1c06c585b511dbf80af3cec7c7cf6348fc37c [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
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200265static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
266{
267 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
268}
269
Christopher Faulet6338a082019-09-09 15:50:54 +0200270static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100271
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200272int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100273{
274 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100275 if (chk->data >= channel_htx_recv_max(chn, htx))
276 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200277 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100278 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100279 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100280 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100281 }
282 else {
283 if (ci_putchk(chn, chk) == -1)
284 return 0;
285 }
286 return 1;
287}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100288
Christopher Fauleted7a0662019-01-14 11:07:34 +0100289static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
290{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200291 struct channel *req = si_oc(si);
292 struct htx *htx = htxbuf(&req->buf);
293 struct htx_blk *blk;
294 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100295
Christopher Fauletb7f88902019-07-15 21:56:43 +0200296 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100297 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200298 ALREADY_CHECKED(blk);
299 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
300 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100301}
302
William Lallemand74c24fb2016-11-21 17:18:36 +0100303/*
304 * http_stats_io_handler()
305 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
306 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100307 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100308 * -> stats_dump_html_head() // emits the HTML headers
309 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
310 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
311 * -> stats_dump_html_px_hdr()
312 * -> stats_dump_fe_stats()
313 * -> stats_dump_li_stats()
314 * -> stats_dump_sv_stats()
315 * -> stats_dump_be_stats()
316 * -> stats_dump_html_px_end()
317 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100318 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100319 */
320
321
William Lallemand74c24fb2016-11-21 17:18:36 +0100322/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
323 * for clearing it if needed.
324 * NOTE: Some tools happen to rely on the field position instead of its name,
325 * so please only append new fields at the end, never in the middle.
326 */
327static void stats_dump_csv_header()
328{
329 int field;
330
331 chunk_appendf(&trash, "# ");
332 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200333 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100334
335 chunk_appendf(&trash, "\n");
336}
337
338
339/* Emits a stats field without any surrounding element and properly encoded to
340 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
341 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200342int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100343{
344 switch (field_format(f, 0)) {
345 case FF_EMPTY: return 1;
346 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
347 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
348 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
349 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200350 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100351 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
352 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
353 }
354}
355
356/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
357 * output is supposed to be used on its own line. Returns non-zero on success, 0
358 * if the buffer is full.
359 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200360int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100361{
362 switch (field_format(f, 0)) {
363 case FF_EMPTY: return 1;
364 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
365 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
366 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
367 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200368 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100369 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
370 default: return chunk_appendf(out, "%08x:?", f->type);
371 }
372}
373
Simon Horman05ee2132017-01-04 09:37:25 +0100374/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
375 * the recommendation for interoperable integers in section 6 of RFC 7159.
376 */
377#define JSON_INT_MAX ((1ULL << 53) - 1)
378#define JSON_INT_MIN (0 - JSON_INT_MAX)
379
380/* Emits a stats field value and its type in JSON.
381 * Returns non-zero on success, 0 on error.
382 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200383int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100384{
385 int old_len;
386 char buf[20];
387 const char *type, *value = buf, *quote = "";
388
389 switch (field_format(f, 0)) {
390 case FF_EMPTY: return 1;
391 case FF_S32: type = "\"s32\"";
392 snprintf(buf, sizeof(buf), "%d", f->u.s32);
393 break;
394 case FF_U32: type = "\"u32\"";
395 snprintf(buf, sizeof(buf), "%u", f->u.u32);
396 break;
397 case FF_S64: type = "\"s64\"";
398 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
399 return 0;
400 type = "\"s64\"";
401 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
402 break;
403 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
404 return 0;
405 type = "\"u64\"";
406 snprintf(buf, sizeof(buf), "%llu",
407 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200408 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200409 case FF_FLT: type = "\"flt\"";
410 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100411 break;
412 case FF_STR: type = "\"str\"";
413 value = field_str(f, 0);
414 quote = "\"";
415 break;
416 default: snprintf(buf, sizeof(buf), "%u", f->type);
417 type = buf;
418 value = "unknown";
419 quote = "\"";
420 break;
421 }
422
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200423 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100424 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
425 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200426 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100427}
428
William Lallemand74c24fb2016-11-21 17:18:36 +0100429/* Emits an encoding of the field type on 3 characters followed by a delimiter.
430 * Returns non-zero on success, 0 if the buffer is full.
431 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200432int stats_emit_field_tags(struct buffer *out, const struct field *f,
433 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100434{
435 char origin, nature, scope;
436
437 switch (field_origin(f, 0)) {
438 case FO_METRIC: origin = 'M'; break;
439 case FO_STATUS: origin = 'S'; break;
440 case FO_KEY: origin = 'K'; break;
441 case FO_CONFIG: origin = 'C'; break;
442 case FO_PRODUCT: origin = 'P'; break;
443 default: origin = '?'; break;
444 }
445
446 switch (field_nature(f, 0)) {
447 case FN_GAUGE: nature = 'G'; break;
448 case FN_LIMIT: nature = 'L'; break;
449 case FN_MIN: nature = 'm'; break;
450 case FN_MAX: nature = 'M'; break;
451 case FN_RATE: nature = 'R'; break;
452 case FN_COUNTER: nature = 'C'; break;
453 case FN_DURATION: nature = 'D'; break;
454 case FN_AGE: nature = 'A'; break;
455 case FN_TIME: nature = 'T'; break;
456 case FN_NAME: nature = 'N'; break;
457 case FN_OUTPUT: nature = 'O'; break;
458 case FN_AVG: nature = 'a'; break;
459 default: nature = '?'; break;
460 }
461
462 switch (field_scope(f, 0)) {
463 case FS_PROCESS: scope = 'P'; break;
464 case FS_SERVICE: scope = 'S'; break;
465 case FS_SYSTEM: scope = 's'; break;
466 case FS_CLUSTER: scope = 'C'; break;
467 default: scope = '?'; break;
468 }
469
470 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
471}
472
Simon Horman05ee2132017-01-04 09:37:25 +0100473/* Emits an encoding of the field type as JSON.
474 * Returns non-zero on success, 0 if the buffer is full.
475 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200476int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100477{
478 const char *origin, *nature, *scope;
479 int old_len;
480
481 switch (field_origin(f, 0)) {
482 case FO_METRIC: origin = "Metric"; break;
483 case FO_STATUS: origin = "Status"; break;
484 case FO_KEY: origin = "Key"; break;
485 case FO_CONFIG: origin = "Config"; break;
486 case FO_PRODUCT: origin = "Product"; break;
487 default: origin = "Unknown"; break;
488 }
489
490 switch (field_nature(f, 0)) {
491 case FN_GAUGE: nature = "Gauge"; break;
492 case FN_LIMIT: nature = "Limit"; break;
493 case FN_MIN: nature = "Min"; break;
494 case FN_MAX: nature = "Max"; break;
495 case FN_RATE: nature = "Rate"; break;
496 case FN_COUNTER: nature = "Counter"; break;
497 case FN_DURATION: nature = "Duration"; break;
498 case FN_AGE: nature = "Age"; break;
499 case FN_TIME: nature = "Time"; break;
500 case FN_NAME: nature = "Name"; break;
501 case FN_OUTPUT: nature = "Output"; break;
502 case FN_AVG: nature = "Avg"; break;
503 default: nature = "Unknown"; break;
504 }
505
506 switch (field_scope(f, 0)) {
507 case FS_PROCESS: scope = "Process"; break;
508 case FS_SERVICE: scope = "Service"; break;
509 case FS_SYSTEM: scope = "System"; break;
510 case FS_CLUSTER: scope = "Cluster"; break;
511 default: scope = "Unknown"; break;
512 }
513
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100515 chunk_appendf(out, "\"tags\":{"
516 "\"origin\":\"%s\","
517 "\"nature\":\"%s\","
518 "\"scope\":\"%s\""
519 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200520 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100521}
William Lallemand74c24fb2016-11-21 17:18:36 +0100522
523/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200524static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200525 const struct field *stats, size_t stats_count,
526 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100527{
528 int field;
529
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200530 for (field = 0; field < stats_count; field++) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100531 if (!stats_emit_raw_data_field(out, &stats[field]))
532 return 0;
533 if (!chunk_strcat(out, ","))
534 return 0;
535 }
536 chunk_strcat(out, "\n");
537 return 1;
538}
539
540/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200541static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200542 const struct field *stats,
543 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200544 unsigned int flags,
545 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100546{
547 int field;
548
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200549 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100550 if (!stats[field].type)
551 continue;
552
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200553 switch (domain) {
554 case STATS_DOMAIN_PROXY:
555 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
556 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
557 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
558 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
559 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
560 '?',
561 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
562 field,
563 stat_fields[field].name,
564 stats[ST_F_PID].u.u32);
565 break;
566
567 default:
568 break;
569 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100570
571 if (!stats_emit_field_tags(out, &stats[field], ':'))
572 return 0;
573 if (!stats_emit_typed_data_field(out, &stats[field]))
574 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200575 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
576 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100577 if (!chunk_strcat(out, "\n"))
578 return 0;
579 }
580 return 1;
581}
582
Simon Horman05ee2132017-01-04 09:37:25 +0100583/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200584static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200585 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100586{
587 int field;
588 int started = 0;
589
590 if (!chunk_strcat(out, "["))
591 return 0;
592
593 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
594 int old_len;
595
596 if (!field_format(info, field))
597 continue;
598
599 if (started && !chunk_strcat(out, ","))
600 goto err;
601 started = 1;
602
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200603 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100604 chunk_appendf(out,
605 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
606 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200607 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100608 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200609 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100610 goto err;
611
612 if (!stats_emit_json_field_tags(out, &info[field]))
613 goto err;
614
615 if (!stats_emit_json_data_field(out, &info[field]))
616 goto err;
617
618 if (!chunk_strcat(out, "}"))
619 goto err;
620 }
621
622 if (!chunk_strcat(out, "]"))
623 goto err;
624 return 1;
625
626err:
627 chunk_reset(out);
628 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
629 return 0;
630}
631
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200632static void stats_print_proxy_field_json(struct buffer *out,
633 const struct field *stat,
634 const char *name,
635 int pos,
636 uint32_t field_type,
637 uint32_t iid,
638 uint32_t sid,
639 uint32_t pid)
640{
641 const char *obj_type;
642 switch (field_type) {
643 case STATS_TYPE_FE: obj_type = "Frontend"; break;
644 case STATS_TYPE_BE: obj_type = "Backend"; break;
645 case STATS_TYPE_SO: obj_type = "Listener"; break;
646 case STATS_TYPE_SV: obj_type = "Server"; break;
647 default: obj_type = "Unknown"; break;
648 }
649
650 chunk_appendf(out,
651 "{"
652 "\"objType\":\"%s\","
653 "\"proxyId\":%u,"
654 "\"id\":%u,"
655 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
656 "\"processNum\":%u,",
657 obj_type, iid, sid, pos, name, pid);
658}
659
Simon Horman05ee2132017-01-04 09:37:25 +0100660/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200661static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200662 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200663 unsigned int flags,
664 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100665{
666 int field;
667 int started = 0;
668
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200669 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100670 return 0;
671 if (!chunk_strcat(out, "["))
672 return 0;
673
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200674 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100675 int old_len;
676
677 if (!stats[field].type)
678 continue;
679
680 if (started && !chunk_strcat(out, ","))
681 goto err;
682 started = 1;
683
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200684 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200685 if (domain == STATS_DOMAIN_PROXY) {
686 stats_print_proxy_field_json(out, &stats[field],
687 stat_fields[field].name, field,
688 stats[ST_F_TYPE].u.u32,
689 stats[ST_F_IID].u.u32,
690 stats[ST_F_SID].u.u32,
691 stats[ST_F_PID].u.u32);
692 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200693
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200694 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100695 goto err;
696
697 if (!stats_emit_json_field_tags(out, &stats[field]))
698 goto err;
699
700 if (!stats_emit_json_data_field(out, &stats[field]))
701 goto err;
702
703 if (!chunk_strcat(out, "}"))
704 goto err;
705 }
706
707 if (!chunk_strcat(out, "]"))
708 goto err;
709
710 return 1;
711
712err:
713 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200714 if (flags & STAT_STARTED)
715 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100716 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
717 return 0;
718}
719
William Lallemand74c24fb2016-11-21 17:18:36 +0100720/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200721 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200722 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100723 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200724static int stats_dump_fields_html(struct buffer *out,
725 const struct field *stats,
726 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100727{
Willy Tarreau83061a82018-07-13 11:56:34 +0200728 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100729
730 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
731 chunk_appendf(out,
732 /* name, queue */
733 "<tr class=\"frontend\">");
734
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200735 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100736 /* Column sub-heading for Enable or Disable server */
737 chunk_appendf(out, "<td></td>");
738 }
739
740 chunk_appendf(out,
741 "<td class=ac>"
742 "<a name=\"%s/Frontend\"></a>"
743 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
744 "<td colspan=3></td>"
745 "",
746 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
747
748 chunk_appendf(out,
749 /* sessions rate : current */
750 "<td><u>%s<div class=tips><table class=det>"
751 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
752 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
753 "",
754 U2H(stats[ST_F_RATE].u.u32),
755 U2H(stats[ST_F_CONN_RATE].u.u32),
756 U2H(stats[ST_F_RATE].u.u32));
757
758 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
759 chunk_appendf(out,
760 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
761 U2H(stats[ST_F_REQ_RATE].u.u32));
762
763 chunk_appendf(out,
764 "</table></div></u></td>"
765 /* sessions rate : max */
766 "<td><u>%s<div class=tips><table class=det>"
767 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
768 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
769 "",
770 U2H(stats[ST_F_RATE_MAX].u.u32),
771 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
772 U2H(stats[ST_F_RATE_MAX].u.u32));
773
774 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
775 chunk_appendf(out,
776 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
777 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
778
779 chunk_appendf(out,
780 "</table></div></u></td>"
781 /* sessions rate : limit */
782 "<td>%s</td>",
783 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
784
785 chunk_appendf(out,
786 /* sessions: current, max, limit, total */
787 "<td>%s</td><td>%s</td><td>%s</td>"
788 "<td><u>%s<div class=tips><table class=det>"
789 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
790 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
791 "",
792 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
793 U2H(stats[ST_F_STOT].u.u64),
794 U2H(stats[ST_F_CONN_TOT].u.u64),
795 U2H(stats[ST_F_STOT].u.u64));
796
797 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
798 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
799 chunk_appendf(out,
800 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
801 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
802 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
803 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
804 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
805 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
806 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
807 "<tr><th>- other responses:</th><td>%s</td></tr>"
808 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100809 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
810 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200811 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100812 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100813 "",
814 U2H(stats[ST_F_REQ_TOT].u.u64),
815 U2H(stats[ST_F_HRSP_1XX].u.u64),
816 U2H(stats[ST_F_HRSP_2XX].u.u64),
817 U2H(stats[ST_F_COMP_RSP].u.u64),
818 stats[ST_F_HRSP_2XX].u.u64 ?
819 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
820 U2H(stats[ST_F_HRSP_3XX].u.u64),
821 U2H(stats[ST_F_HRSP_4XX].u.u64),
822 U2H(stats[ST_F_HRSP_5XX].u.u64),
823 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200824 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100825 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
826 U2H(stats[ST_F_CACHE_HITS].u.u64),
827 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
828 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100829 U2H(stats[ST_F_WREW].u.u64),
830 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100831 }
832
833 chunk_appendf(out,
834 "</table></div></u></td>"
835 /* sessions: lbtot, lastsess */
836 "<td></td><td></td>"
837 /* bytes : in */
838 "<td>%s</td>"
839 "",
840 U2H(stats[ST_F_BIN].u.u64));
841
842 chunk_appendf(out,
843 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
844 "<td>%s%s<div class=tips><table class=det>"
845 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
846 "<tr><th>Compression in:</th><td>%s</td></tr>"
847 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
848 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
849 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
850 "</table></div>%s</td>",
851 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
852 U2H(stats[ST_F_BOUT].u.u64),
853 U2H(stats[ST_F_BOUT].u.u64),
854 U2H(stats[ST_F_COMP_IN].u.u64),
855 U2H(stats[ST_F_COMP_OUT].u.u64),
856 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
857 U2H(stats[ST_F_COMP_BYP].u.u64),
858 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
859 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,
860 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
861
862 chunk_appendf(out,
863 /* denied: req, resp */
864 "<td>%s</td><td>%s</td>"
865 /* errors : request, connect, response */
866 "<td>%s</td><td></td><td></td>"
867 /* warnings: retries, redispatches */
868 "<td></td><td></td>"
869 /* server status : reflect frontend status */
870 "<td class=ac>%s</td>"
871 /* rest of server: nothing */
872 "<td class=ac colspan=8></td></tr>"
873 "",
874 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
875 U2H(stats[ST_F_EREQ].u.u64),
876 field_str(stats, ST_F_STATUS));
877 }
878 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
879 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200880 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100881 /* Column sub-heading for Enable or Disable server */
882 chunk_appendf(out, "<td></td>");
883 }
884
885 chunk_appendf(out,
886 /* frontend name, listener name */
887 "<td class=ac><a name=\"%s/+%s\"></a>%s"
888 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
889 "",
890 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200891 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100892 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
893
Willy Tarreau708c4162019-10-09 10:19:16 +0200894 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100895 chunk_appendf(out, "<div class=tips>");
896
Willy Tarreau90807112020-02-25 08:16:33 +0100897 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100898 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
899 else if (*field_str(stats, ST_F_ADDR) == '[')
900 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
901 else if (*field_str(stats, ST_F_ADDR))
902 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
903
904 /* id */
905 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
906 }
907
908 chunk_appendf(out,
909 /* queue */
910 "%s</td><td colspan=3></td>"
911 /* sessions rate: current, max, limit */
912 "<td colspan=3>&nbsp;</td>"
913 /* sessions: current, max, limit, total, lbtot, lastsess */
914 "<td>%s</td><td>%s</td><td>%s</td>"
915 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
916 /* bytes: in, out */
917 "<td>%s</td><td>%s</td>"
918 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200919 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100920 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
921 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
922
923 chunk_appendf(out,
924 /* denied: req, resp */
925 "<td>%s</td><td>%s</td>"
926 /* errors: request, connect, response */
927 "<td>%s</td><td></td><td></td>"
928 /* warnings: retries, redispatches */
929 "<td></td><td></td>"
930 /* server status: reflect listener status */
931 "<td class=ac>%s</td>"
932 /* rest of server: nothing */
933 "<td class=ac colspan=8></td></tr>"
934 "",
935 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
936 U2H(stats[ST_F_EREQ].u.u64),
937 field_str(stats, ST_F_STATUS));
938 }
939 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
940 const char *style;
941
942 /* determine the style to use depending on the server's state,
943 * its health and weight. There isn't a 1-to-1 mapping between
944 * state and styles for the cases where the server is (still)
945 * up. The reason is that we don't want to report nolb and
946 * drain with the same color.
947 */
948
949 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
950 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
951 style = "down";
952 }
953 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
954 style = "going_up";
955 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400956 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
957 style = "draining";
958 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100959 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
960 style = "going_down";
961 }
962 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
963 style = "nolb";
964 }
965 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
966 style = "no_check";
967 }
968 else if (!stats[ST_F_CHKFAIL].type ||
969 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
970 /* no check or max health = UP */
971 if (stats[ST_F_WEIGHT].u.u32)
972 style = "up";
973 else
974 style = "draining";
975 }
976 else {
977 style = "going_down";
978 }
979
Willy Tarreau7b524852020-08-11 10:26:36 +0200980 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +0100981 chunk_appendf(out, "<tr class=\"maintain\">");
982 else
983 chunk_appendf(out,
984 "<tr class=\"%s_%s\">",
985 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
986
987
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200988 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100989 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000990 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
991 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100992 field_str(stats, ST_F_SVNAME));
993
994 chunk_appendf(out,
995 "<td class=ac><a name=\"%s/%s\"></a>%s"
996 "<a class=lfsb href=\"#%s/%s\">%s</a>"
997 "",
998 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200999 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001000 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1001
Willy Tarreau708c4162019-10-09 10:19:16 +02001002 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001003 chunk_appendf(out, "<div class=tips>");
1004
Willy Tarreau90807112020-02-25 08:16:33 +01001005 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001006 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1007 else if (*field_str(stats, ST_F_ADDR) == '[')
1008 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1009 else if (*field_str(stats, ST_F_ADDR))
1010 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1011
1012 /* id */
1013 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1014
1015 /* cookie */
1016 if (stats[ST_F_COOKIE].type) {
1017 chunk_appendf(out, ", cookie: '");
1018 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1019 chunk_htmlencode(out, &src);
1020 chunk_appendf(out, "'");
1021 }
1022
1023 chunk_appendf(out, "</div>");
1024 }
1025
1026 chunk_appendf(out,
1027 /* queue : current, max, limit */
1028 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1029 /* sessions rate : current, max, limit */
1030 "<td>%s</td><td>%s</td><td></td>"
1031 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001032 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001033 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1034 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1035
1036 chunk_appendf(out,
1037 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001038 "<td><u>%s<div class=tips>"
1039 "<table class=det>"
1040 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001041 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001042 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001043 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1044 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001045 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001046 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1047 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1048 "</table></div></u>"
1049 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001050 "<td><u>%s<div class=tips><table class=det>"
1051 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1052 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001053 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001054 U2H(stats[ST_F_SCUR].u.u32),
1055 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1056 U2H(stats[ST_F_SRV_ICUR].u.u32),
1057 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1058 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001059 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001060
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001061 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1062 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1063 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001064 U2H(stats[ST_F_STOT].u.u64),
1065 U2H(stats[ST_F_STOT].u.u64));
1066
1067 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1068 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001069 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001070 "<tr><th>New connections:</th><td>%s</td></tr>"
1071 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001072 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001073 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1074 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1075 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1076 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1077 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1078 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001079 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001080 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001081 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001082 U2H(stats[ST_F_CONNECT].u.u64),
1083 U2H(stats[ST_F_REUSE].u.u64),
1084 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1085 (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 +02001086 U2H(stats[ST_F_REQ_TOT].u.u64),
1087 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1088 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1089 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1090 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1091 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1092 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1093 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1094 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1095 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1096 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1097 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1098 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001099 U2H(stats[ST_F_WREW].u.u64),
1100 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001101 }
1102
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001103 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1104 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1105 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1106 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1107 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001108 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001109 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1110 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1111 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1112 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001113
1114 chunk_appendf(out,
1115 "</table></div></u></td>"
1116 /* sessions: lbtot, last */
1117 "<td>%s</td><td>%s</td>",
1118 U2H(stats[ST_F_LBTOT].u.u64),
1119 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1120
1121 chunk_appendf(out,
1122 /* bytes : in, out */
1123 "<td>%s</td><td>%s</td>"
1124 /* denied: req, resp */
1125 "<td></td><td>%s</td>"
1126 /* errors : request, connect */
1127 "<td></td><td>%s</td>"
1128 /* errors : response */
1129 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1130 /* warnings: retries, redispatches */
1131 "<td>%lld</td><td>%lld</td>"
1132 "",
1133 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1134 U2H(stats[ST_F_DRESP].u.u64),
1135 U2H(stats[ST_F_ECON].u.u64),
1136 U2H(stats[ST_F_ERESP].u.u64),
1137 (long long)stats[ST_F_CLI_ABRT].u.u64,
1138 (long long)stats[ST_F_SRV_ABRT].u.u64,
1139 (long long)stats[ST_F_WRETR].u.u64,
1140 (long long)stats[ST_F_WREDIS].u.u64);
1141
1142 /* status, last change */
1143 chunk_appendf(out, "<td class=ac>");
1144
1145 /* FIXME!!!!
1146 * LASTCHG should contain the last change for *this* server and must be computed
1147 * properly above, as was done below, ie: this server if maint, otherwise ref server
1148 * if tracking. Note that ref is either local or remote depending on tracking.
1149 */
1150
1151
Willy Tarreau7b524852020-08-11 10:26:36 +02001152 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001153 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1154 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001155 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001156 chunk_strcat(out, "<i>no check</i>");
1157 }
1158 else {
1159 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 +02001160 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001161 if (stats[ST_F_CHECK_HEALTH].u.u32)
1162 chunk_strcat(out, " &uarr;");
1163 }
1164 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1165 chunk_strcat(out, " &darr;");
1166 }
1167
Willy Tarreau7b524852020-08-11 10:26:36 +02001168 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001169 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1170 chunk_appendf(out,
1171 "</td><td class=ac><u> %s",
1172 field_str(stats, ST_F_AGENT_STATUS));
1173
1174 if (stats[ST_F_AGENT_CODE].type)
1175 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1176
1177 if (stats[ST_F_AGENT_DURATION].type)
1178 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1179
1180 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1181
1182 if (*field_str(stats, ST_F_LAST_AGT)) {
1183 chunk_appendf(out, ": ");
1184 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1185 chunk_htmlencode(out, &src);
1186 }
1187 chunk_appendf(out, "</div></u>");
1188 }
1189 else if (stats[ST_F_CHECK_STATUS].type) {
1190 chunk_appendf(out,
1191 "</td><td class=ac><u> %s",
1192 field_str(stats, ST_F_CHECK_STATUS));
1193
1194 if (stats[ST_F_CHECK_CODE].type)
1195 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1196
1197 if (stats[ST_F_CHECK_DURATION].type)
1198 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1199
1200 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1201
1202 if (*field_str(stats, ST_F_LAST_CHK)) {
1203 chunk_appendf(out, ": ");
1204 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1205 chunk_htmlencode(out, &src);
1206 }
1207 chunk_appendf(out, "</div></u>");
1208 }
1209 else
1210 chunk_appendf(out, "</td><td>");
1211
1212 chunk_appendf(out,
1213 /* weight */
1214 "</td><td class=ac>%d</td>"
1215 /* act, bck */
1216 "<td class=ac>%s</td><td class=ac>%s</td>"
1217 "",
1218 stats[ST_F_WEIGHT].u.u32,
1219 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1220 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1221
1222 /* check failures: unique, fatal, down time */
1223 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1224 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1225 }
1226 else if (stats[ST_F_CHKFAIL].type) {
1227 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1228
1229 if (stats[ST_F_HANAFAIL].type)
1230 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1231
1232 chunk_appendf(out,
1233 "<div class=tips>Failed Health Checks%s</div></u></td>"
1234 "<td>%lld</td><td>%s</td>"
1235 "",
1236 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1237 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1238 }
1239 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1240 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1241 chunk_appendf(out,
1242 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1243 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1244 }
1245 else
1246 chunk_appendf(out, "<td colspan=3></td>");
1247
1248 /* throttle */
1249 if (stats[ST_F_THROTTLE].type)
1250 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1251 else
1252 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1253 }
1254 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1255 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001256 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001257 /* Column sub-heading for Enable or Disable server */
1258 chunk_appendf(out, "<td></td>");
1259 }
1260 chunk_appendf(out,
1261 "<td class=ac>"
1262 /* name */
1263 "%s<a name=\"%s/Backend\"></a>"
1264 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1265 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001266 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001267 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1268
Willy Tarreau708c4162019-10-09 10:19:16 +02001269 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001270 /* balancing */
1271 chunk_appendf(out, "<div class=tips>balancing: %s",
1272 field_str(stats, ST_F_ALGO));
1273
1274 /* cookie */
1275 if (stats[ST_F_COOKIE].type) {
1276 chunk_appendf(out, ", cookie: '");
1277 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1278 chunk_htmlencode(out, &src);
1279 chunk_appendf(out, "'");
1280 }
1281 chunk_appendf(out, "</div>");
1282 }
1283
1284 chunk_appendf(out,
1285 "%s</td>"
1286 /* queue : current, max */
1287 "<td>%s</td><td>%s</td><td></td>"
1288 /* sessions rate : current, max, limit */
1289 "<td>%s</td><td>%s</td><td></td>"
1290 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001291 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001292 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1293 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1294
1295 chunk_appendf(out,
1296 /* sessions: current, max, limit, total */
1297 "<td>%s</td><td>%s</td><td>%s</td>"
1298 "<td><u>%s<div class=tips><table class=det>"
1299 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1300 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001301 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 +01001302 U2H(stats[ST_F_STOT].u.u64),
1303 U2H(stats[ST_F_STOT].u.u64));
1304
1305 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1306 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1307 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001308 "<tr><th>New connections:</th><td>%s</td></tr>"
1309 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001310 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1311 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1312 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1313 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1314 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1315 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1316 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1317 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001318 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1319 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001320 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001321 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001322 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001323 U2H(stats[ST_F_CONNECT].u.u64),
1324 U2H(stats[ST_F_REUSE].u.u64),
1325 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1326 (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 +01001327 U2H(stats[ST_F_REQ_TOT].u.u64),
1328 U2H(stats[ST_F_HRSP_1XX].u.u64),
1329 U2H(stats[ST_F_HRSP_2XX].u.u64),
1330 U2H(stats[ST_F_COMP_RSP].u.u64),
1331 stats[ST_F_HRSP_2XX].u.u64 ?
1332 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1333 U2H(stats[ST_F_HRSP_3XX].u.u64),
1334 U2H(stats[ST_F_HRSP_4XX].u.u64),
1335 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001336 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001337 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1338 U2H(stats[ST_F_CACHE_HITS].u.u64),
1339 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1340 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001341 U2H(stats[ST_F_WREW].u.u64),
1342 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001343 }
1344
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001345 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1346 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1347 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1348 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1349 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001350 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001351 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1352 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1353 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1354 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001355
1356 chunk_appendf(out,
1357 "</table></div></u></td>"
1358 /* sessions: lbtot, last */
1359 "<td>%s</td><td>%s</td>"
1360 /* bytes: in */
1361 "<td>%s</td>"
1362 "",
1363 U2H(stats[ST_F_LBTOT].u.u64),
1364 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1365 U2H(stats[ST_F_BIN].u.u64));
1366
1367 chunk_appendf(out,
1368 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1369 "<td>%s%s<div class=tips><table class=det>"
1370 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1371 "<tr><th>Compression in:</th><td>%s</td></tr>"
1372 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1373 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1374 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1375 "</table></div>%s</td>",
1376 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1377 U2H(stats[ST_F_BOUT].u.u64),
1378 U2H(stats[ST_F_BOUT].u.u64),
1379 U2H(stats[ST_F_COMP_IN].u.u64),
1380 U2H(stats[ST_F_COMP_OUT].u.u64),
1381 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1382 U2H(stats[ST_F_COMP_BYP].u.u64),
1383 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1384 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,
1385 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1386
1387 chunk_appendf(out,
1388 /* denied: req, resp */
1389 "<td>%s</td><td>%s</td>"
1390 /* errors : request, connect */
1391 "<td></td><td>%s</td>"
1392 /* errors : response */
1393 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1394 /* warnings: retries, redispatches */
1395 "<td>%lld</td><td>%lld</td>"
1396 /* backend status: reflect backend status (up/down): we display UP
1397 * if the backend has known working servers or if it has no server at
1398 * all (eg: for stats). Then we display the total weight, number of
1399 * active and backups. */
1400 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1401 "<td class=ac>%d</td><td class=ac>%d</td>"
1402 "",
1403 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1404 U2H(stats[ST_F_ECON].u.u64),
1405 U2H(stats[ST_F_ERESP].u.u64),
1406 (long long)stats[ST_F_CLI_ABRT].u.u64,
1407 (long long)stats[ST_F_SRV_ABRT].u.u64,
1408 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1409 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1410 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1411 stats[ST_F_WEIGHT].u.u32,
1412 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1413
1414 chunk_appendf(out,
1415 /* rest of backend: nothing, down transitions, total downtime, throttle */
1416 "<td class=ac>&nbsp;</td><td>%d</td>"
1417 "<td>%s</td>"
1418 "<td></td>"
1419 "</tr>",
1420 stats[ST_F_CHKDOWN].u.u32,
1421 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1422 }
1423 return 1;
1424}
1425
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001426int stats_dump_one_line(const struct field *stats, size_t stats_count,
1427 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001428{
Simon Horman05ee2132017-01-04 09:37:25 +01001429 int ret;
1430
William Lallemand74c24fb2016-11-21 17:18:36 +01001431 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001432 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001433 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001434 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001435 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001436 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001437 else
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001438 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001439
1440 if (ret)
1441 appctx->ctx.stats.flags |= STAT_STARTED;
1442
1443 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001444}
1445
1446/* Fill <stats> with the frontend statistics. <stats> is
1447 * preallocated array of length <len>. The length of the array
1448 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1449 * this value, the function returns 0, otherwise, it returns 1.
1450 */
1451int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1452{
1453 if (len < ST_F_TOTAL_FIELDS)
1454 return 0;
1455
1456 memset(stats, 0, sizeof(*stats) * len);
1457
1458 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1459 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1460 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1461 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1462 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1463 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1464 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1465 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1466 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1467 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1468 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1469 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1470 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1471 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1472 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1473 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1474 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1475 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1476 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1477 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1478 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1479 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001480 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001481 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001482
1483 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1484 if (px->mode == PR_MODE_HTTP) {
1485 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1486 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1487 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1488 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1489 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1490 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1491 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001492 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1493 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001494 }
1495
1496 /* requests : req_rate, req_rate_max, req_tot, */
1497 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1498 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1499 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1500
1501 /* compression: in, out, bypassed, responses */
1502 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1503 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1504 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1505 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1506
1507 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1508 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1509 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1510 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1511
1512 return 1;
1513}
1514
1515/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1516 * the state from stream interface <si>. The caller is responsible for clearing
1517 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1518 */
1519static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1520{
1521 struct appctx *appctx = __objt_appctx(si->end);
1522
1523 if (!(px->cap & PR_CAP_FE))
1524 return 0;
1525
1526 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1527 return 0;
1528
1529 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1530 return 0;
1531
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001532 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001533}
1534
1535/* Fill <stats> with the listener statistics. <stats> is
1536 * preallocated array of length <len>. The length of the array
1537 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1538 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001539 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001540 */
1541int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1542 struct field *stats, int len)
1543{
Willy Tarreau83061a82018-07-13 11:56:34 +02001544 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001545
1546 if (len < ST_F_TOTAL_FIELDS)
1547 return 0;
1548
1549 if (!l->counters)
1550 return 0;
1551
1552 chunk_reset(out);
1553 memset(stats, 0, sizeof(*stats) * len);
1554
1555 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1556 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1557 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1558 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1559 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1560 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1561 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1562 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1563 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1564 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1565 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1566 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1567 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1568 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001569 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 +01001570 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1571 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1572 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1573 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001574 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001575 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001576
Willy Tarreau708c4162019-10-09 10:19:16 +02001577 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001578 char str[INET6_ADDRSTRLEN];
1579 int port;
1580
Willy Tarreau37159062020-08-27 07:48:42 +02001581 port = get_host_port(&l->rx.addr);
1582 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001583 case AF_INET:
1584 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1585 chunk_appendf(out, "%s:%d", str, port);
1586 break;
1587 case AF_INET6:
1588 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1589 chunk_appendf(out, "[%s]:%d", str, port);
1590 break;
1591 case AF_UNIX:
1592 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1593 break;
1594 case -1:
1595 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1596 chunk_strcat(out, strerror(errno));
1597 break;
1598 default: /* address family not supported */
1599 break;
1600 }
1601 }
1602
1603 return 1;
1604}
1605
1606/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001607 * from stream interface <si>. The caller is responsible for clearing the trash
1608 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001609 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001610static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001611{
1612 struct appctx *appctx = __objt_appctx(si->end);
1613
Willy Tarreau43241ff2019-10-09 11:27:51 +02001614 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001615 return 0;
1616
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001617 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001618}
1619
1620enum srv_stats_state {
1621 SRV_STATS_STATE_DOWN = 0,
1622 SRV_STATS_STATE_DOWN_AGENT,
1623 SRV_STATS_STATE_GOING_UP,
1624 SRV_STATS_STATE_UP_GOING_DOWN,
1625 SRV_STATS_STATE_UP,
1626 SRV_STATS_STATE_NOLB_GOING_DOWN,
1627 SRV_STATS_STATE_NOLB,
1628 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1629 SRV_STATS_STATE_DRAIN,
1630 SRV_STATS_STATE_DRAIN_AGENT,
1631 SRV_STATS_STATE_NO_CHECK,
1632
1633 SRV_STATS_STATE_COUNT, /* Must be last */
1634};
1635
1636static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1637 [SRV_STATS_STATE_DOWN] = "DOWN",
1638 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1639 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1640 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1641 [SRV_STATS_STATE_UP] = "UP",
1642 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1643 [SRV_STATS_STATE_NOLB] = "NOLB",
1644 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1645 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1646 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1647 [SRV_STATS_STATE_NO_CHECK] = "no check"
1648};
1649
1650/* Fill <stats> with the server statistics. <stats> is
1651 * preallocated array of length <len>. The length of the array
1652 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1653 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001654 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001655 */
1656int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1657 struct field *stats, int len)
1658{
1659 struct server *via, *ref;
1660 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001661 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001662 enum srv_stats_state state;
1663 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001664 long long srv_samples_counter;
1665 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001666
1667 if (len < ST_F_TOTAL_FIELDS)
1668 return 0;
1669
1670 memset(stats, 0, sizeof(*stats) * len);
1671
1672 /* we have "via" which is the tracked server as described in the configuration,
1673 * and "ref" which is the checked server and the end of the chain.
1674 */
1675 via = sv->track ? sv->track : sv;
1676 ref = via;
1677 while (ref->track)
1678 ref = ref->track;
1679
Emeric Brun52a91d32017-08-31 14:41:55 +02001680 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001681 if ((ref->check.state & CHK_ST_ENABLED) &&
1682 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1683 state = SRV_STATS_STATE_UP_GOING_DOWN;
1684 } else {
1685 state = SRV_STATS_STATE_UP;
1686 }
1687
Emeric Brun52a91d32017-08-31 14:41:55 +02001688 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001689 if (ref->agent.state & CHK_ST_ENABLED)
1690 state = SRV_STATS_STATE_DRAIN_AGENT;
1691 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1692 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1693 else
1694 state = SRV_STATS_STATE_DRAIN;
1695 }
1696
1697 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1698 state = SRV_STATS_STATE_NO_CHECK;
1699 }
1700 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001701 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001702 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1703 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1704 state = SRV_STATS_STATE_NOLB;
1705 } else {
1706 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1707 }
1708 }
1709 else { /* stopped */
1710 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1711 state = SRV_STATS_STATE_DOWN_AGENT;
1712 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1713 state = SRV_STATS_STATE_DOWN; /* DOWN */
1714 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1715 state = SRV_STATS_STATE_GOING_UP;
1716 } else {
1717 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1718 }
1719 }
1720
1721 chunk_reset(out);
1722
1723 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1724 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1725 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1726 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1727 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1728 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1729 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1730
1731 if (sv->maxconn)
1732 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1733
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001734 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1735 if (sv->max_idle_conns != -1)
1736 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1737
William Lallemand74c24fb2016-11-21 17:18:36 +01001738 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1739 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1740 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001741 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001742 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1743 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1744 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1745 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001746 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001747 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001748 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1749 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001750
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001751 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1752 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1753 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001754 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001755
William Lallemand74c24fb2016-11-21 17:18:36 +01001756 /* status */
1757 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001758 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001759 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001760 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001761 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001762 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001763 chunk_appendf(out, "MAINT");
1764 else
1765 chunk_appendf(out,
1766 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001767 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1768 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001769
1770 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1771 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001772 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 +01001773 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1774 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1775
1776 /* check failures: unique, fatal; last change, total downtime */
1777 if (sv->check.state & CHK_ST_ENABLED) {
1778 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1779 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1780 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1781 }
1782
1783 if (sv->maxqueue)
1784 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1785
1786 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1787 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1788 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1789
Emeric Brun52a91d32017-08-31 14:41:55 +02001790 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001791 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1792
1793 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1794
1795 if (sv->track) {
1796 char *fld_track = chunk_newstr(out);
1797
1798 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1799 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1800 }
1801
1802 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1803 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1804 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1805
1806 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1807 const char *fld_chksts;
1808
1809 fld_chksts = chunk_newstr(out);
1810 chunk_strcat(out, "* "); // for check in progress
1811 chunk_strcat(out, get_check_status_info(sv->check.status));
1812 if (!(sv->check.state & CHK_ST_INPROGRESS))
1813 fld_chksts += 2; // skip "* "
1814 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1815
1816 if (sv->check.status >= HCHK_STATUS_L57DATA)
1817 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1818
1819 if (sv->check.status >= HCHK_STATUS_CHECKED)
1820 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1821
1822 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1823 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1824 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1825 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1826 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1827 }
1828
1829 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1830 const char *fld_chksts;
1831
1832 fld_chksts = chunk_newstr(out);
1833 chunk_strcat(out, "* "); // for check in progress
1834 chunk_strcat(out, get_check_status_info(sv->agent.status));
1835 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1836 fld_chksts += 2; // skip "* "
1837 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1838
1839 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1840 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1841
1842 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1843 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1844
1845 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1846 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1847 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1848 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1849 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1850 }
1851
1852 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1853 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001854 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001855 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1856 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1857 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1858 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1859 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1860 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1861 }
1862
1863 if (ref->observe)
1864 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1865
1866 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1867 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1868 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1869
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001870 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1871 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1872 srv_samples_window = srv_samples_counter;
1873
1874 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1875 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1876 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1877 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001878
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001879 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1880 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1881 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1882 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1883
Willy Tarreau708c4162019-10-09 10:19:16 +02001884 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001885 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1886 case AF_INET:
1887 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001888 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001889 break;
1890 case AF_INET6:
1891 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001892 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001893 break;
1894 case AF_UNIX:
1895 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1896 break;
1897 case -1:
1898 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1899 chunk_strcat(out, strerror(errno));
1900 break;
1901 default: /* address family not supported */
1902 break;
1903 }
1904
1905 if (sv->cookie)
1906 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1907 }
1908
1909 return 1;
1910}
1911
1912/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001913 * from stream interface <si>, and server state <state>. The caller is
1914 * responsible for clearing the trash if needed. Returns non-zero if it emits
1915 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001916 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001917static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001918{
1919 struct appctx *appctx = __objt_appctx(si->end);
1920
Willy Tarreau43241ff2019-10-09 11:27:51 +02001921 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001922 return 0;
1923
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001924 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001925}
1926
1927/* Fill <stats> with the backend statistics. <stats> is
1928 * preallocated array of length <len>. The length of the array
1929 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1930 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001931 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001932 */
1933int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1934{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001935 long long be_samples_counter;
1936 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1937
William Lallemand74c24fb2016-11-21 17:18:36 +01001938 if (len < ST_F_TOTAL_FIELDS)
1939 return 0;
1940
1941 memset(stats, 0, sizeof(*stats) * len);
1942
1943 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1944 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1945 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1946 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1947 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001948 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001949 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1950 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1951 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1952 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1953 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1954 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1955 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1956 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1957 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1958 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1959 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001960 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001961 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001962 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1963 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001964 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1965 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1966 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1967 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1968 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1969 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1970 if (px->srv)
1971 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1972
1973 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1974 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1975 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1976 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1977 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1978 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1979 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1980
Willy Tarreau708c4162019-10-09 10:19:16 +02001981 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001982 if (px->cookie_name)
1983 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1984 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1985 }
1986
1987 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1988 if (px->mode == PR_MODE_HTTP) {
1989 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1990 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1991 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1992 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1993 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1994 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1995 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001996 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1997 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001998 }
1999
2000 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2001 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2002
2003 /* compression: in, out, bypassed, responses */
2004 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2005 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2006 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2007 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2008 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
2009
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002010 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2011 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2012 be_samples_window = be_samples_counter;
2013
2014 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2015 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2016 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2017 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 +01002018
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002019 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2020 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2021 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2022 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2023
William Lallemand74c24fb2016-11-21 17:18:36 +01002024 return 1;
2025}
2026
2027/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002028 * interface <si>. The caller is responsible for clearing the trash if needed.
2029 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002030 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002031static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002032{
2033 struct appctx *appctx = __objt_appctx(si->end);
2034
2035 if (!(px->cap & PR_CAP_BE))
2036 return 0;
2037
2038 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2039 return 0;
2040
Willy Tarreau43241ff2019-10-09 11:27:51 +02002041 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002042 return 0;
2043
Amaury Denoyelle97323c92020-10-02 18:32:01 +02002044 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002045}
2046
2047/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2048 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2049 * for clearing the trash if needed.
2050 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002051static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002052{
2053 struct appctx *appctx = __objt_appctx(si->end);
2054 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2055
2056 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2057 /* A form to enable/disable this proxy servers */
2058
2059 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2060 scope_txt[0] = 0;
2061 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002062 const char *scope_ptr = stats_scope_ptr(appctx, si);
2063
William Lallemand74c24fb2016-11-21 17:18:36 +01002064 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002065 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002066 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2067 }
2068
2069 chunk_appendf(&trash,
2070 "<form method=\"post\">");
2071 }
2072
2073 /* print a new table */
2074 chunk_appendf(&trash,
2075 "<table class=\"tbl\" width=\"100%%\">\n"
2076 "<tr class=\"titre\">"
2077 "<th class=\"pxname\" width=\"10%%\">");
2078
2079 chunk_appendf(&trash,
2080 "<a name=\"%s\"></a>%s"
2081 "<a class=px href=\"#%s\">%s</a>",
2082 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002083 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002084 px->id, px->id);
2085
Willy Tarreau676c29e2019-10-09 10:50:01 +02002086 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002087 /* cap, mode, id */
2088 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2089 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2090 px->uuid);
2091 chunk_appendf(&trash, "</div>");
2092 }
2093
2094 chunk_appendf(&trash,
2095 "%s</th>"
2096 "<th class=\"%s\" width=\"90%%\">%s</th>"
2097 "</tr>\n"
2098 "</table>\n"
2099 "<table class=\"tbl\" width=\"100%%\">\n"
2100 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002101 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002102 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2103
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002104 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002105 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002106 if ((px->cap & PR_CAP_BE) && px->srv)
2107 chunk_appendf(&trash,
2108 "<th rowspan=2 width=1><input type=\"checkbox\" "
2109 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2110 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2111 px->id,
2112 px->id);
2113 else
2114 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002115 }
2116
2117 chunk_appendf(&trash,
2118 "<th rowspan=2></th>"
2119 "<th colspan=3>Queue</th>"
2120 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2121 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2122 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2123 "<th colspan=9>Server</th>"
2124 "</tr>\n"
2125 "<tr class=\"titre\">"
2126 "<th>Cur</th><th>Max</th><th>Limit</th>"
2127 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2128 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2129 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2130 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2131 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2132 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2133 "<th>Thrtle</th>\n"
2134 "</tr>");
2135}
2136
2137/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2138 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2139 */
2140static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2141{
2142 struct appctx *appctx = __objt_appctx(si->end);
2143 chunk_appendf(&trash, "</table>");
2144
2145 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2146 /* close the form used to enable/disable this proxy servers */
2147 chunk_appendf(&trash,
2148 "Choose the action to perform on the checked servers : "
2149 "<select name=action>"
2150 "<option value=\"\"></option>"
2151 "<option value=\"ready\">Set state to READY</option>"
2152 "<option value=\"drain\">Set state to DRAIN</option>"
2153 "<option value=\"maint\">Set state to MAINT</option>"
2154 "<option value=\"dhlth\">Health: disable checks</option>"
2155 "<option value=\"ehlth\">Health: enable checks</option>"
2156 "<option value=\"hrunn\">Health: force UP</option>"
2157 "<option value=\"hnolb\">Health: force NOLB</option>"
2158 "<option value=\"hdown\">Health: force DOWN</option>"
2159 "<option value=\"dagent\">Agent: disable checks</option>"
2160 "<option value=\"eagent\">Agent: enable checks</option>"
2161 "<option value=\"arunn\">Agent: force UP</option>"
2162 "<option value=\"adown\">Agent: force DOWN</option>"
2163 "<option value=\"shutdown\">Kill Sessions</option>"
2164 "</select>"
2165 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2166 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2167 "</form>",
2168 px->uuid);
2169 }
2170
2171 chunk_appendf(&trash, "<p>\n");
2172}
2173
2174/*
2175 * Dumps statistics for a proxy. The output is sent to the stream interface's
2176 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2177 * buffer space, or non-zero if everything completed. This function is used
2178 * both by the CLI and the HTTP entry points, and is able to dump the output
2179 * in HTML or CSV formats. If the later, <uri> must be NULL.
2180 */
Christopher Fauletef779222018-10-31 08:47:01 +01002181int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2182 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002183{
2184 struct appctx *appctx = __objt_appctx(si->end);
2185 struct stream *s = si_strm(si);
2186 struct channel *rep = si_ic(si);
2187 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2188 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002189
2190 chunk_reset(&trash);
2191
2192 switch (appctx->ctx.stats.px_st) {
2193 case STAT_PX_ST_INIT:
2194 /* we are on a new proxy */
2195 if (uri && uri->scope) {
2196 /* we have a limited scope, we have to check the proxy name */
2197 struct stat_scope *scope;
2198 int len;
2199
2200 len = strlen(px->id);
2201 scope = uri->scope;
2202
2203 while (scope) {
2204 /* match exact proxy name */
2205 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2206 break;
2207
2208 /* match '.' which means 'self' proxy */
2209 if (!strcmp(scope->px_id, ".") && px == s->be)
2210 break;
2211 scope = scope->next;
2212 }
2213
2214 /* proxy name not found : don't dump anything */
2215 if (scope == NULL)
2216 return 1;
2217 }
2218
2219 /* if the user has requested a limited output and the proxy
2220 * name does not match, skip it.
2221 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002222 if (appctx->ctx.stats.scope_len) {
2223 const char *scope_ptr = stats_scope_ptr(appctx, si);
2224
2225 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2226 return 1;
2227 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002228
2229 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2230 (appctx->ctx.stats.iid != -1) &&
2231 (px->uuid != appctx->ctx.stats.iid))
2232 return 1;
2233
2234 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2235 /* fall through */
2236
2237 case STAT_PX_ST_TH:
2238 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002239 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002240 if (!stats_putchk(rep, htx, &trash))
2241 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002242 }
2243
2244 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2245 /* fall through */
2246
2247 case STAT_PX_ST_FE:
2248 /* print the frontend */
2249 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002250 if (!stats_putchk(rep, htx, &trash))
2251 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002252 }
2253
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002254 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01002255 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2256 /* fall through */
2257
2258 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002259 /* obj2 points to listeners list as initialized above */
2260 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002261 if (htx) {
2262 if (htx_almost_full(htx))
2263 goto full;
2264 }
2265 else {
2266 if (buffer_almost_full(&rep->buf))
2267 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002268 }
2269
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002270 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01002271 if (!l->counters)
2272 continue;
2273
2274 if (appctx->ctx.stats.flags & STAT_BOUND) {
2275 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2276 break;
2277
2278 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2279 continue;
2280 }
2281
2282 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002283 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002284 if (!stats_putchk(rep, htx, &trash))
2285 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002286 }
2287 }
2288
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002289 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01002290 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2291 /* fall through */
2292
2293 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002294 /* obj2 points to servers list as initialized above */
2295 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002296 if (htx) {
2297 if (htx_almost_full(htx))
2298 goto full;
2299 }
2300 else {
2301 if (buffer_almost_full(&rep->buf))
2302 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002303 }
2304
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002305 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01002306
2307 if (appctx->ctx.stats.flags & STAT_BOUND) {
2308 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2309 break;
2310
2311 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2312 continue;
2313 }
2314
2315 svs = sv;
2316 while (svs->track)
2317 svs = svs->track;
2318
2319 /* do not report servers which are DOWN and not changing state */
2320 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002321 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2322 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002323 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2324 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2325 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2326 continue;
2327 }
2328
Willy Tarreau43241ff2019-10-09 11:27:51 +02002329 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002330 if (!stats_putchk(rep, htx, &trash))
2331 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002332 }
2333 } /* for sv */
2334
2335 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2336 /* fall through */
2337
2338 case STAT_PX_ST_BE:
2339 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002340 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002341 if (!stats_putchk(rep, htx, &trash))
2342 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002343 }
2344
2345 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2346 /* fall through */
2347
2348 case STAT_PX_ST_END:
2349 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2350 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002351 if (!stats_putchk(rep, htx, &trash))
2352 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002353 }
2354
2355 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2356 /* fall through */
2357
2358 case STAT_PX_ST_FIN:
2359 return 1;
2360
2361 default:
2362 /* unknown state, we should put an abort() here ! */
2363 return 1;
2364 }
Christopher Fauletef779222018-10-31 08:47:01 +01002365
2366 full:
2367 si_rx_room_blk(si);
2368 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002369}
2370
2371/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2372 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2373 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002374static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002375{
2376 /* WARNING! This must fit in the first buffer !!! */
2377 chunk_appendf(&trash,
2378 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2379 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2380 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03002381 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002382 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2383 "<style type=\"text/css\"><!--\n"
2384 "body {"
2385 " font-family: arial, helvetica, sans-serif;"
2386 " font-size: 12px;"
2387 " font-weight: normal;"
2388 " color: black;"
2389 " background: white;"
2390 "}\n"
2391 "th,td {"
2392 " font-size: 10px;"
2393 "}\n"
2394 "h1 {"
2395 " font-size: x-large;"
2396 " margin-bottom: 0.5em;"
2397 "}\n"
2398 "h2 {"
2399 " font-family: helvetica, arial;"
2400 " font-size: x-large;"
2401 " font-weight: bold;"
2402 " font-style: italic;"
2403 " color: #6020a0;"
2404 " margin-top: 0em;"
2405 " margin-bottom: 0em;"
2406 "}\n"
2407 "h3 {"
2408 " font-family: helvetica, arial;"
2409 " font-size: 16px;"
2410 " font-weight: bold;"
2411 " color: #b00040;"
2412 " background: #e8e8d0;"
2413 " margin-top: 0em;"
2414 " margin-bottom: 0em;"
2415 "}\n"
2416 "li {"
2417 " margin-top: 0.25em;"
2418 " margin-right: 2em;"
2419 "}\n"
2420 ".hr {margin-top: 0.25em;"
2421 " border-color: black;"
2422 " border-bottom-style: solid;"
2423 "}\n"
2424 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2425 ".total {background: #20D0D0;color: #ffff80;}\n"
2426 ".frontend {background: #e8e8d0;}\n"
2427 ".socket {background: #d0d0d0;}\n"
2428 ".backend {background: #e8e8d0;}\n"
2429 ".active_down {background: #ff9090;}\n"
2430 ".active_going_up {background: #ffd020;}\n"
2431 ".active_going_down {background: #ffffa0;}\n"
2432 ".active_up {background: #c0ffc0;}\n"
2433 ".active_nolb {background: #20a0ff;}\n"
2434 ".active_draining {background: #20a0FF;}\n"
2435 ".active_no_check {background: #e0e0e0;}\n"
2436 ".backup_down {background: #ff9090;}\n"
2437 ".backup_going_up {background: #ff80ff;}\n"
2438 ".backup_going_down {background: #c060ff;}\n"
2439 ".backup_up {background: #b0d0ff;}\n"
2440 ".backup_nolb {background: #90b0e0;}\n"
2441 ".backup_draining {background: #cc9900;}\n"
2442 ".backup_no_check {background: #e0e0e0;}\n"
2443 ".maintain {background: #c07820;}\n"
2444 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2445 "\n"
2446 "a.px:link {color: #ffff40; text-decoration: none;}"
2447 "a.px:visited {color: #ffff40; text-decoration: none;}"
2448 "a.px:hover {color: #ffffff; text-decoration: none;}"
2449 "a.lfsb:link {color: #000000; text-decoration: none;}"
2450 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2451 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2452 "\n"
2453 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2454 "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"
2455 "table.tbl td.ac { text-align: center;}\n"
2456 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2457 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2458 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2459 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2460 "\n"
2461 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2462 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2463 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2464 "table.det { border-collapse: collapse; border-style: none; }\n"
2465 "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"
2466 "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"
2467 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2468 "div.tips {\n"
2469 " display:block;\n"
2470 " visibility:hidden;\n"
2471 " z-index:2147483647;\n"
2472 " position:absolute;\n"
2473 " padding:2px 4px 3px;\n"
2474 " background:#f0f060; color:#000000;\n"
2475 " border:1px solid #7040c0;\n"
2476 " white-space:nowrap;\n"
2477 " font-style:normal;font-size:11px;font-weight:normal;\n"
2478 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2479 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2480 "}\n"
2481 "u:hover div.tips {visibility:visible;}\n"
2482 "-->\n"
2483 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002484 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002485 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002486 );
2487}
2488
2489/* Dumps the HTML stats information block to the trash for and uses the state from
2490 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2491 * for clearing the trash if needed.
2492 */
2493static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2494{
2495 struct appctx *appctx = __objt_appctx(si->end);
2496 unsigned int up = (now.tv_sec - start_date.tv_sec);
2497 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002498 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002499 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2500
2501 /* Turn the bytes per second to bits per second and take care of the
2502 * usual ethernet overhead in order to help figure how far we are from
2503 * interface saturation since it's the only case which usually matters.
2504 * For this we count the total size of an Ethernet frame on the wire
2505 * including preamble and IFG (1538) for the largest TCP segment it
2506 * transports (1448 with TCP timestamps). This is not valid for smaller
2507 * packets (under-estimated), but it gives a reasonably accurate
2508 * estimation of how far we are from uplink saturation.
2509 */
2510 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002511
2512 /* WARNING! this has to fit the first packet too.
2513 * We are around 3.5 kB, add adding entries will
2514 * become tricky if we want to support 4kB buffers !
2515 */
2516 chunk_appendf(&trash,
2517 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2518 PRODUCT_NAME "%s</a></h1>\n"
2519 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2520 "<hr width=\"100%%\" class=\"hr\">\n"
2521 "<h3>&gt; General process information</h3>\n"
2522 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002523 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002524 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2525 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2526 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002527 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002528 "Running tasks: %d/%d; idle = %d %%<br>\n"
2529 "</td><td align=\"center\" nowrap>\n"
2530 "<table class=\"lgd\"><tr>\n"
2531 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2532 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2533 "</tr><tr>\n"
2534 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2535 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2536 "</tr><tr>\n"
2537 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2538 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2539 "</tr><tr>\n"
2540 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2541 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2542 "</tr><tr>\n"
2543 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2544 "</tr><tr>\n"
2545 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2546 "</tr></table>\n"
2547 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2548 "</td>"
2549 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2550 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2551 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002552 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2553 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2554 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2555 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2556 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002557 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002558 up / 86400, (up % 86400) / 3600,
2559 (up % 3600) / 60, (up % 60),
2560 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2561 global.rlimit_memmax ? " MB" : "",
2562 global.rlimit_nofile,
2563 global.maxsock, global.maxconn, global.maxpipes,
2564 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002565 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2566 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002567 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002568 );
2569
2570 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002571 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002572 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2573
2574 chunk_appendf(&trash,
2575 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2576 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2577 STAT_SCOPE_TXT_MAXLEN);
2578
2579 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2580 scope_txt[0] = 0;
2581 if (appctx->ctx.stats.scope_len) {
2582 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002583 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002584 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2585 }
2586
2587 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2588 chunk_appendf(&trash,
2589 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2590 uri->uri_prefix,
2591 "",
2592 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2593 scope_txt);
2594 else
2595 chunk_appendf(&trash,
2596 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2597 uri->uri_prefix,
2598 ";up",
2599 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2600 scope_txt);
2601
2602 if (uri->refresh > 0) {
2603 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2604 chunk_appendf(&trash,
2605 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2606 uri->uri_prefix,
2607 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2608 "",
2609 scope_txt);
2610 else
2611 chunk_appendf(&trash,
2612 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2613 uri->uri_prefix,
2614 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2615 ";norefresh",
2616 scope_txt);
2617 }
2618
2619 chunk_appendf(&trash,
2620 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2621 uri->uri_prefix,
2622 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2623 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2624 scope_txt);
2625
2626 chunk_appendf(&trash,
2627 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2628 uri->uri_prefix,
2629 (uri->refresh > 0) ? ";norefresh" : "",
2630 scope_txt);
2631
2632 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002633 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2634 uri->uri_prefix,
2635 (uri->refresh > 0) ? ";norefresh" : "",
2636 scope_txt, uri->uri_prefix);
2637
2638 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002639 "</ul></td>"
2640 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2641 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2642 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2643 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2644 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2645 "</ul>"
2646 "</td>"
2647 "</tr></table>\n"
2648 ""
2649 );
2650
2651 if (appctx->ctx.stats.st_code) {
2652 switch (appctx->ctx.stats.st_code) {
2653 case STAT_STATUS_DONE:
2654 chunk_appendf(&trash,
2655 "<p><div class=active_up>"
2656 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2657 "Action processed successfully."
2658 "</div>\n", uri->uri_prefix,
2659 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2660 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2661 scope_txt);
2662 break;
2663 case STAT_STATUS_NONE:
2664 chunk_appendf(&trash,
2665 "<p><div class=active_going_down>"
2666 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2667 "Nothing has changed."
2668 "</div>\n", uri->uri_prefix,
2669 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2670 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2671 scope_txt);
2672 break;
2673 case STAT_STATUS_PART:
2674 chunk_appendf(&trash,
2675 "<p><div class=active_going_down>"
2676 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2677 "Action partially processed.<br>"
2678 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2679 "</div>\n", uri->uri_prefix,
2680 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2681 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2682 scope_txt);
2683 break;
2684 case STAT_STATUS_ERRP:
2685 chunk_appendf(&trash,
2686 "<p><div class=active_down>"
2687 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2688 "Action not processed because of invalid parameters."
2689 "<ul>"
2690 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002691 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002692 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2693 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2694 "</ul>"
2695 "</div>\n", uri->uri_prefix,
2696 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2697 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2698 scope_txt);
2699 break;
2700 case STAT_STATUS_EXCD:
2701 chunk_appendf(&trash,
2702 "<p><div class=active_down>"
2703 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2704 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2705 "You should retry with less servers at a time.</b>"
2706 "</div>\n", uri->uri_prefix,
2707 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2708 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2709 scope_txt);
2710 break;
2711 case STAT_STATUS_DENY:
2712 chunk_appendf(&trash,
2713 "<p><div class=active_down>"
2714 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2715 "<b>Action denied.</b>"
2716 "</div>\n", uri->uri_prefix,
2717 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2718 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2719 scope_txt);
2720 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002721 case STAT_STATUS_IVAL:
2722 chunk_appendf(&trash,
2723 "<p><div class=active_down>"
2724 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2725 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2726 "</div>\n", uri->uri_prefix,
2727 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2728 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2729 scope_txt);
2730 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002731 default:
2732 chunk_appendf(&trash,
2733 "<p><div class=active_no_check>"
2734 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2735 "Unexpected result."
2736 "</div>\n", uri->uri_prefix,
2737 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2738 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2739 scope_txt);
2740 }
2741 chunk_appendf(&trash, "<p>\n");
2742 }
2743}
2744
2745/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2746 * for clearing the trash if needed.
2747 */
2748static void stats_dump_html_end()
2749{
2750 chunk_appendf(&trash, "</body></html>\n");
2751}
2752
Simon Horman05ee2132017-01-04 09:37:25 +01002753/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2754 * for clearing it if needed.
2755 */
2756static void stats_dump_json_header()
2757{
2758 chunk_strcat(&trash, "[");
2759}
2760
2761
2762/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2763 * for clearing the trash if needed.
2764 */
2765static void stats_dump_json_end()
2766{
2767 chunk_strcat(&trash, "]");
2768}
2769
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002770/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
2771 * a pointer to the current server/listener.
2772 */
2773static int stats_dump_proxies(struct stream_interface *si,
2774 struct htx *htx,
2775 struct uri_auth *uri)
2776{
2777 struct appctx *appctx = __objt_appctx(si->end);
2778 struct channel *rep = si_ic(si);
2779 struct proxy *px;
2780
2781 /* dump proxies */
2782 while (appctx->ctx.stats.obj1) {
2783 if (htx) {
2784 if (htx_almost_full(htx))
2785 goto full;
2786 }
2787 else {
2788 if (buffer_almost_full(&rep->buf))
2789 goto full;
2790 }
2791
2792 px = appctx->ctx.stats.obj1;
2793 /* skip the disabled proxies, global frontend and non-networked ones */
2794 if (px->state != PR_STSTOPPED && px->uuid > 0
2795 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
2796
2797 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
2798 return 0;
2799 }
2800
2801 appctx->ctx.stats.obj1 = px->next;
2802 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2803 }
2804
2805 return 1;
2806
2807 full:
2808 si_rx_room_blk(si);
2809 return 0;
2810}
2811
William Lallemand74c24fb2016-11-21 17:18:36 +01002812/* This function dumps statistics onto the stream interface's read buffer in
2813 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2814 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2815 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2816 * and the stream must be closed, or -1 in case of any error. This function is
2817 * used by both the CLI and the HTTP handlers.
2818 */
Christopher Fauletef779222018-10-31 08:47:01 +01002819static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2820 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002821{
2822 struct appctx *appctx = __objt_appctx(si->end);
2823 struct channel *rep = si_ic(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002824
2825 chunk_reset(&trash);
2826
2827 switch (appctx->st2) {
2828 case STAT_ST_INIT:
2829 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2830 /* fall through */
2831
2832 case STAT_ST_HEAD:
2833 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002834 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002835 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2836 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002837 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002838 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002839 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2840 stats_dump_csv_header();
2841
Christopher Fauletef779222018-10-31 08:47:01 +01002842 if (!stats_putchk(rep, htx, &trash))
2843 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002844
Christopher Faulet6338a082019-09-09 15:50:54 +02002845 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2846 appctx->st2 = STAT_ST_FIN;
2847 return 1;
2848 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002849 appctx->st2 = STAT_ST_INFO;
2850 /* fall through */
2851
2852 case STAT_ST_INFO:
2853 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2854 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002855 if (!stats_putchk(rep, htx, &trash))
2856 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002857 }
2858
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002859 appctx->ctx.stats.obj1 = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002860 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2861 appctx->st2 = STAT_ST_LIST;
2862 /* fall through */
2863
2864 case STAT_ST_LIST:
2865 /* dump proxies */
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002866 if (!stats_dump_proxies(si, htx, uri))
2867 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002868
2869 appctx->st2 = STAT_ST_END;
2870 /* fall through */
2871
2872 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002873 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2874 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2875 stats_dump_html_end();
2876 else
2877 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002878 if (!stats_putchk(rep, htx, &trash))
2879 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002880 }
2881
2882 appctx->st2 = STAT_ST_FIN;
2883 /* fall through */
2884
2885 case STAT_ST_FIN:
2886 return 1;
2887
2888 default:
2889 /* unknown state ! */
2890 appctx->st2 = STAT_ST_FIN;
2891 return -1;
2892 }
Christopher Fauletef779222018-10-31 08:47:01 +01002893
2894 full:
2895 si_rx_room_blk(si);
2896 return 0;
2897
William Lallemand74c24fb2016-11-21 17:18:36 +01002898}
2899
2900/* We reached the stats page through a POST request. The appctx is
2901 * expected to have already been allocated by the caller.
2902 * Parse the posted data and enable/disable servers if necessary.
2903 * Returns 1 if request was parsed or zero if it needs more data.
2904 */
2905static int stats_process_http_post(struct stream_interface *si)
2906{
2907 struct stream *s = si_strm(si);
2908 struct appctx *appctx = objt_appctx(si->end);
2909
2910 struct proxy *px = NULL;
2911 struct server *sv = NULL;
2912
2913 char key[LINESIZE];
2914 int action = ST_ADM_ACTION_NONE;
2915 int reprocess = 0;
2916
2917 int total_servers = 0;
2918 int altered_servers = 0;
2919
2920 char *first_param, *cur_param, *next_param, *end_params;
2921 char *st_cur_param = NULL;
2922 char *st_next_param = NULL;
2923
Christopher Fauleta3618372018-12-13 21:59:56 +01002924 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002925
Christopher Fauletb7f88902019-07-15 21:56:43 +02002926 struct htx *htx = htxbuf(&s->req.buf);
2927 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002928
Christopher Fauletb7f88902019-07-15 21:56:43 +02002929 /* we need more data */
2930 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2931 /* check if we can receive more */
2932 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2933 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2934 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002935 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002936 goto wait;
2937 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002938
Christopher Fauletb7f88902019-07-15 21:56:43 +02002939 /* The request was fully received. Copy data */
2940 blk = htx_get_head_blk(htx);
2941 while (blk) {
2942 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002943
Christopher Fauletb7f88902019-07-15 21:56:43 +02002944 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2945 break;
2946 if (type == HTX_BLK_DATA) {
2947 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002948
Christopher Fauletb7f88902019-07-15 21:56:43 +02002949 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002950 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2951 goto out;
2952 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002953 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002954 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002955 }
2956
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002957 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002958 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002959 cur_param = next_param = end_params;
2960 *end_params = '\0';
2961
2962 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2963
2964 /*
2965 * Parse the parameters in reverse order to only store the last value.
2966 * From the html form, the backend and the action are at the end.
2967 */
2968 while (cur_param > first_param) {
2969 char *value;
2970 int poffset, plen;
2971
2972 cur_param--;
2973
2974 if ((*cur_param == '&') || (cur_param == first_param)) {
2975 reprocess_servers:
2976 /* Parse the key */
2977 poffset = (cur_param != first_param ? 1 : 0);
2978 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2979 if ((plen > 0) && (plen <= sizeof(key))) {
2980 strncpy(key, cur_param + poffset, plen);
2981 key[plen - 1] = '\0';
2982 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002983 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002984 goto out;
2985 }
2986
2987 /* Parse the value */
2988 value = key;
2989 while (*value != '\0' && *value != '=') {
2990 value++;
2991 }
2992 if (*value == '=') {
2993 /* Ok, a value is found, we can mark the end of the key */
2994 *value++ = '\0';
2995 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002996 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002997 break;
2998
2999 /* Now we can check the key to see what to do */
3000 if (!px && (strcmp(key, "b") == 0)) {
3001 if ((px = proxy_be_by_name(value)) == NULL) {
3002 /* the backend name is unknown or ambiguous (duplicate names) */
3003 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3004 goto out;
3005 }
3006 }
3007 else if (!action && (strcmp(key, "action") == 0)) {
3008 if (strcmp(value, "ready") == 0) {
3009 action = ST_ADM_ACTION_READY;
3010 }
3011 else if (strcmp(value, "drain") == 0) {
3012 action = ST_ADM_ACTION_DRAIN;
3013 }
3014 else if (strcmp(value, "maint") == 0) {
3015 action = ST_ADM_ACTION_MAINT;
3016 }
3017 else if (strcmp(value, "shutdown") == 0) {
3018 action = ST_ADM_ACTION_SHUTDOWN;
3019 }
3020 else if (strcmp(value, "dhlth") == 0) {
3021 action = ST_ADM_ACTION_DHLTH;
3022 }
3023 else if (strcmp(value, "ehlth") == 0) {
3024 action = ST_ADM_ACTION_EHLTH;
3025 }
3026 else if (strcmp(value, "hrunn") == 0) {
3027 action = ST_ADM_ACTION_HRUNN;
3028 }
3029 else if (strcmp(value, "hnolb") == 0) {
3030 action = ST_ADM_ACTION_HNOLB;
3031 }
3032 else if (strcmp(value, "hdown") == 0) {
3033 action = ST_ADM_ACTION_HDOWN;
3034 }
3035 else if (strcmp(value, "dagent") == 0) {
3036 action = ST_ADM_ACTION_DAGENT;
3037 }
3038 else if (strcmp(value, "eagent") == 0) {
3039 action = ST_ADM_ACTION_EAGENT;
3040 }
3041 else if (strcmp(value, "arunn") == 0) {
3042 action = ST_ADM_ACTION_ARUNN;
3043 }
3044 else if (strcmp(value, "adown") == 0) {
3045 action = ST_ADM_ACTION_ADOWN;
3046 }
3047 /* else these are the old supported methods */
3048 else if (strcmp(value, "disable") == 0) {
3049 action = ST_ADM_ACTION_DISABLE;
3050 }
3051 else if (strcmp(value, "enable") == 0) {
3052 action = ST_ADM_ACTION_ENABLE;
3053 }
3054 else if (strcmp(value, "stop") == 0) {
3055 action = ST_ADM_ACTION_STOP;
3056 }
3057 else if (strcmp(value, "start") == 0) {
3058 action = ST_ADM_ACTION_START;
3059 }
3060 else {
3061 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3062 goto out;
3063 }
3064 }
3065 else if (strcmp(key, "s") == 0) {
3066 if (!(px && action)) {
3067 /*
3068 * Indicates that we'll need to reprocess the parameters
3069 * as soon as backend and action are known
3070 */
3071 if (!reprocess) {
3072 st_cur_param = cur_param;
3073 st_next_param = next_param;
3074 }
3075 reprocess = 1;
3076 }
3077 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003078 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003079 switch (action) {
3080 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003081 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003082 altered_servers++;
3083 total_servers++;
3084 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3085 }
3086 break;
3087 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003088 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003089 altered_servers++;
3090 total_servers++;
3091 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3092 }
3093 break;
3094 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003095 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003096 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3097 altered_servers++;
3098 total_servers++;
3099 }
3100 break;
3101 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003102 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003103 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3104 altered_servers++;
3105 total_servers++;
3106 }
3107 break;
3108 case ST_ADM_ACTION_DHLTH:
3109 if (sv->check.state & CHK_ST_CONFIGURED) {
3110 sv->check.state &= ~CHK_ST_ENABLED;
3111 altered_servers++;
3112 total_servers++;
3113 }
3114 break;
3115 case ST_ADM_ACTION_EHLTH:
3116 if (sv->check.state & CHK_ST_CONFIGURED) {
3117 sv->check.state |= CHK_ST_ENABLED;
3118 altered_servers++;
3119 total_servers++;
3120 }
3121 break;
3122 case ST_ADM_ACTION_HRUNN:
3123 if (!(sv->track)) {
3124 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003125 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003126 altered_servers++;
3127 total_servers++;
3128 }
3129 break;
3130 case ST_ADM_ACTION_HNOLB:
3131 if (!(sv->track)) {
3132 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003133 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003134 altered_servers++;
3135 total_servers++;
3136 }
3137 break;
3138 case ST_ADM_ACTION_HDOWN:
3139 if (!(sv->track)) {
3140 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003141 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003142 altered_servers++;
3143 total_servers++;
3144 }
3145 break;
3146 case ST_ADM_ACTION_DAGENT:
3147 if (sv->agent.state & CHK_ST_CONFIGURED) {
3148 sv->agent.state &= ~CHK_ST_ENABLED;
3149 altered_servers++;
3150 total_servers++;
3151 }
3152 break;
3153 case ST_ADM_ACTION_EAGENT:
3154 if (sv->agent.state & CHK_ST_CONFIGURED) {
3155 sv->agent.state |= CHK_ST_ENABLED;
3156 altered_servers++;
3157 total_servers++;
3158 }
3159 break;
3160 case ST_ADM_ACTION_ARUNN:
3161 if (sv->agent.state & CHK_ST_ENABLED) {
3162 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003163 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003164 altered_servers++;
3165 total_servers++;
3166 }
3167 break;
3168 case ST_ADM_ACTION_ADOWN:
3169 if (sv->agent.state & CHK_ST_ENABLED) {
3170 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003171 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003172 altered_servers++;
3173 total_servers++;
3174 }
3175 break;
3176 case ST_ADM_ACTION_READY:
3177 srv_adm_set_ready(sv);
3178 altered_servers++;
3179 total_servers++;
3180 break;
3181 case ST_ADM_ACTION_DRAIN:
3182 srv_adm_set_drain(sv);
3183 altered_servers++;
3184 total_servers++;
3185 break;
3186 case ST_ADM_ACTION_MAINT:
3187 srv_adm_set_maint(sv);
3188 altered_servers++;
3189 total_servers++;
3190 break;
3191 case ST_ADM_ACTION_SHUTDOWN:
3192 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003193 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003194 altered_servers++;
3195 total_servers++;
3196 }
3197 break;
3198 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003199 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003200 } else {
3201 /* the server name is unknown or ambiguous (duplicate names) */
3202 total_servers++;
3203 }
3204 }
3205 if (reprocess && px && action) {
3206 /* Now, we know the backend and the action chosen by the user.
3207 * We can safely restart from the first server parameter
3208 * to reprocess them
3209 */
3210 cur_param = st_cur_param;
3211 next_param = st_next_param;
3212 reprocess = 0;
3213 goto reprocess_servers;
3214 }
3215
3216 next_param = cur_param;
3217 }
3218 }
3219
3220 if (total_servers == 0) {
3221 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3222 }
3223 else if (altered_servers == 0) {
3224 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3225 }
3226 else if (altered_servers == total_servers) {
3227 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3228 }
3229 else {
3230 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3231 }
3232 out:
3233 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003234 wait:
3235 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3236 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003237}
3238
3239
Christopher Fauletb7f88902019-07-15 21:56:43 +02003240static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003241{
3242 struct stream *s = si_strm(si);
3243 struct uri_auth *uri = s->be->uri_auth;
3244 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003245 struct htx_sl *sl;
3246 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003247
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003248 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);
3249 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3250 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003251 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003252 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003253
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003254 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003255 goto full;
3256 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3257 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3258 goto full;
3259 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003260 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3261 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3262 goto full;
3263 }
Christopher Fauletef779222018-10-31 08:47:01 +01003264 else {
3265 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3266 goto full;
3267 }
3268
3269 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3270 const char *refresh = U2A(uri->refresh);
3271 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3272 goto full;
3273 }
3274
3275 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3276 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3277 goto full;
3278 }
3279
3280 if (!htx_add_endof(htx, HTX_BLK_EOH))
3281 goto full;
3282
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003283 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003284 return 1;
3285
3286 full:
3287 htx_reset(htx);
3288 si_rx_room_blk(si);
3289 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003290}
3291
Christopher Fauletef779222018-10-31 08:47:01 +01003292
Christopher Fauletb7f88902019-07-15 21:56:43 +02003293static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003294{
3295 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3296 struct stream *s = si_strm(si);
3297 struct uri_auth *uri = s->be->uri_auth;
3298 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003299 struct htx_sl *sl;
3300 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003301
3302 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3303 scope_txt[0] = 0;
3304 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003305 const char *scope_ptr = stats_scope_ptr(appctx, si);
3306
Christopher Fauletef779222018-10-31 08:47:01 +01003307 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003308 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003309 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3310 }
3311
3312 /* We don't want to land on the posted stats page because a refresh will
3313 * repost the data. We don't want this to happen on accident so we redirect
3314 * the browse to the stats page with a GET.
3315 */
3316 chunk_printf(&trash, "%s;st=%s%s%s%s",
3317 uri->uri_prefix,
3318 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3319 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3320 stat_status_codes[appctx->ctx.stats.st_code]) ?
3321 stat_status_codes[appctx->ctx.stats.st_code] :
3322 stat_status_codes[STAT_STATUS_UNKN],
3323 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3324 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3325 scope_txt);
3326
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003327 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3328 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3329 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003330 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003331 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003332
3333 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003334 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3335 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3336 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3337 goto full;
3338
3339 if (!htx_add_endof(htx, HTX_BLK_EOH))
3340 goto full;
3341
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003342 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003343 return 1;
3344
3345full:
3346 htx_reset(htx);
3347 si_rx_room_blk(si);
3348 return 0;
3349}
William Lallemand74c24fb2016-11-21 17:18:36 +01003350
Simon Horman05ee2132017-01-04 09:37:25 +01003351
William Lallemand74c24fb2016-11-21 17:18:36 +01003352/* This I/O handler runs as an applet embedded in a stream interface. It is
3353 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3354 * appctx->st0 contains the operation in progress (dump, done). The handler
3355 * automatically unregisters itself once transfer is complete.
3356 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003357static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003358{
3359 struct stream_interface *si = appctx->owner;
3360 struct stream *s = si_strm(si);
3361 struct channel *req = si_oc(si);
3362 struct channel *res = si_ic(si);
3363 struct htx *req_htx, *res_htx;
3364
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003365 /* only proxy stats are available via http */
3366 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3367
Christopher Fauletef779222018-10-31 08:47:01 +01003368 res_htx = htx_from_buf(&res->buf);
3369
3370 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3371 goto out;
3372
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003373 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003374 if (!b_size(&res->buf)) {
3375 si_rx_room_blk(si);
3376 goto out;
3377 }
3378
3379 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003380 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3381 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003382
3383 /* all states are processed in sequence */
3384 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003385 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003386 if (s->txn->meth == HTTP_METH_HEAD)
3387 appctx->st0 = STAT_HTTP_DONE;
3388 else
3389 appctx->st0 = STAT_HTTP_DUMP;
3390 }
3391 }
3392
3393 if (appctx->st0 == STAT_HTTP_DUMP) {
3394 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3395 appctx->st0 = STAT_HTTP_DONE;
3396 }
3397
3398 if (appctx->st0 == STAT_HTTP_POST) {
3399 if (stats_process_http_post(si))
3400 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003401 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003402 appctx->st0 = STAT_HTTP_DONE;
3403 }
3404
3405 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003406 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003407 appctx->st0 = STAT_HTTP_DONE;
3408 }
3409
3410 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003411 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003412 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003413 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3414 si_rx_room_blk(si);
3415 goto out;
3416 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003417 channel_add_input(&s->res, 1);
3418 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003419 }
3420
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003421 if (appctx->st0 == STAT_HTTP_END) {
3422 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003423 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003424 si_shutr(si);
3425 }
3426
3427 /* eat the whole request */
3428 if (co_data(req)) {
3429 req_htx = htx_from_buf(&req->buf);
3430 co_htx_skip(req, req_htx, co_data(req));
3431 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003432 }
3433 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003434
Christopher Fauletef779222018-10-31 08:47:01 +01003435 out:
3436 /* we have left the request in the buffer for the case where we
3437 * process a POST, and this automatically re-enables activity on
3438 * read. It's better to indicate that we want to stop reading when
3439 * we're sending, so that we know there's at most one direction
3440 * deciding to wake the applet up. It saves it from looping when
3441 * emitting large blocks into small TCP windows.
3442 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003443 htx_to_buf(res_htx, &res->buf);
3444 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003445 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003446}
3447
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003448/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003449static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003450 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003451{
3452 int field;
3453
3454 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3455 if (!field_format(info, field))
3456 continue;
3457
Willy Tarreaueaa55372019-10-09 07:39:11 +02003458 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003459 return 0;
3460 if (!stats_emit_raw_data_field(out, &info[field]))
3461 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003462 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3463 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003464 if (!chunk_strcat(out, "\n"))
3465 return 0;
3466 }
3467 return 1;
3468}
3469
3470/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003471static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003472 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003473{
3474 int field;
3475
3476 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3477 if (!field_format(info, field))
3478 continue;
3479
Willy Tarreaueaa55372019-10-09 07:39:11 +02003480 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 +01003481 return 0;
3482 if (!stats_emit_field_tags(out, &info[field], ':'))
3483 return 0;
3484 if (!stats_emit_typed_data_field(out, &info[field]))
3485 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003486 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3487 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003488 if (!chunk_strcat(out, "\n"))
3489 return 0;
3490 }
3491 return 1;
3492}
3493
3494/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003495 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003496 * INF_TOTAL_FIELDS. If this length is less then this value, the
3497 * function returns 0, otherwise, it returns 1.
3498 */
3499int stats_fill_info(struct field *info, int len)
3500{
3501 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003502 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003503
3504#ifdef USE_OPENSSL
3505 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3506 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3507 int ssl_reuse = 0;
3508
3509 if (ssl_key_rate < ssl_sess_rate) {
3510 /* count the ssl reuse ratio and avoid overflows in both directions */
3511 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3512 }
3513#endif
3514
3515 if (len < INF_TOTAL_FIELDS)
3516 return 0;
3517
3518 chunk_reset(out);
3519 memset(info, 0, sizeof(*info) * len);
3520
3521 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003522 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3523 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003524
Yves Lafon95317282018-02-26 11:10:37 +01003525 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003526 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3527 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3528 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3529
3530 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3531 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3532
3533 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3534 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3535 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3536 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3537 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3538 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3539 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3540 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3541 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3542 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3543 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3544 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3545#ifdef USE_OPENSSL
3546 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3547 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3548 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3549#endif
3550 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3551 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3552 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3553 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3554 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3555 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3556 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3557 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3558 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3559
3560#ifdef USE_OPENSSL
3561 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3562 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3563 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3564 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3565 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3566 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3567 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3568 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3569 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3570 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3571#endif
3572 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3573 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3574 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3575#ifdef USE_ZLIB
3576 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3577 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3578#endif
3579 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003580 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003581 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003582 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3583 if (global.desc)
3584 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003585 info[INF_STOPPING] = mkf_u32(0, stopping);
3586 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003587 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003588 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003589 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003590 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003591 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003592 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003593 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003594 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003595 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003596 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 +02003597 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003598 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003599
3600 return 1;
3601}
3602
3603/* This function dumps information onto the stream interface's read buffer.
3604 * It returns 0 as long as it does not complete, non-zero upon completion.
3605 * No state is used.
3606 */
3607static int stats_dump_info_to_buffer(struct stream_interface *si)
3608{
3609 struct appctx *appctx = __objt_appctx(si->end);
3610
3611 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3612 return 0;
3613
3614 chunk_reset(&trash);
3615
3616 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003617 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003618 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003619 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003620 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003621 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003622
Willy Tarreau06d80a92017-10-19 14:32:15 +02003623 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003624 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003625 return 0;
3626 }
3627
3628 return 1;
3629}
3630
Simon Horman6f6bb382017-01-04 09:37:26 +01003631/* This function dumps the schema onto the stream interface's read buffer.
3632 * It returns 0 as long as it does not complete, non-zero upon completion.
3633 * No state is used.
3634 *
3635 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3636 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3637 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003638static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003639{
3640
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003641 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003642
3643 chunk_strcat(out,
3644 "{"
3645 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3646 "\"oneOf\":["
3647 "{"
3648 "\"title\":\"Info\","
3649 "\"type\":\"array\","
3650 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003651 "\"title\":\"InfoItem\","
3652 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003653 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003654 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3655 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3656 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3657 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3658 "},"
3659 "\"required\":[\"field\",\"processNum\",\"tags\","
3660 "\"value\"]"
3661 "}"
3662 "},"
3663 "{"
3664 "\"title\":\"Stat\","
3665 "\"type\":\"array\","
3666 "\"items\":{"
3667 "\"title\":\"InfoItem\","
3668 "\"type\":\"object\","
3669 "\"properties\":{"
3670 "\"objType\":{"
3671 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3672 "\"Server\",\"Unknown\"]"
3673 "},"
3674 "\"proxyId\":{"
3675 "\"type\":\"integer\","
3676 "\"minimum\":0"
3677 "},"
3678 "\"id\":{"
3679 "\"type\":\"integer\","
3680 "\"minimum\":0"
3681 "},"
3682 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3683 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3684 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3685 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3686 "},"
3687 "\"required\":[\"objType\",\"proxyId\",\"id\","
3688 "\"field\",\"processNum\",\"tags\","
3689 "\"value\"]"
3690 "}"
3691 "},"
3692 "{"
3693 "\"title\":\"Error\","
3694 "\"type\":\"object\","
3695 "\"properties\":{"
3696 "\"errorStr\":{"
3697 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003698 "}"
3699 "},"
3700 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01003701 "}"
3702 "],"
3703 "\"definitions\":{"
3704 "\"field\":{"
3705 "\"type\":\"object\","
3706 "\"pos\":{"
3707 "\"type\":\"integer\","
3708 "\"minimum\":0"
3709 "},"
3710 "\"name\":{"
3711 "\"type\":\"string\""
3712 "},"
3713 "\"required\":[\"pos\",\"name\"]"
3714 "},"
3715 "\"processNum\":{"
3716 "\"type\":\"integer\","
3717 "\"minimum\":1"
3718 "},"
3719 "\"tags\":{"
3720 "\"type\":\"object\","
3721 "\"origin\":{"
3722 "\"type\":\"string\","
3723 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3724 "\"Config\",\"Product\",\"Unknown\"]"
3725 "},"
3726 "\"nature\":{"
3727 "\"type\":\"string\","
3728 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3729 "\"Rate\",\"Counter\",\"Duration\","
3730 "\"Age\",\"Time\",\"Name\",\"Output\","
3731 "\"Avg\", \"Unknown\"]"
3732 "},"
3733 "\"scope\":{"
3734 "\"type\":\"string\","
3735 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3736 "\"System\",\"Unknown\"]"
3737 "},"
3738 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3739 "},"
3740 "\"typedValue\":{"
3741 "\"type\":\"object\","
3742 "\"oneOf\":["
3743 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3744 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3745 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3746 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3747 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3748 "],"
3749 "\"definitions\":{"
3750 "\"s32Value\":{"
3751 "\"properties\":{"
3752 "\"type\":{"
3753 "\"type\":\"string\","
3754 "\"enum\":[\"s32\"]"
3755 "},"
3756 "\"value\":{"
3757 "\"type\":\"integer\","
3758 "\"minimum\":-2147483648,"
3759 "\"maximum\":2147483647"
3760 "}"
3761 "},"
3762 "\"required\":[\"type\",\"value\"]"
3763 "},"
3764 "\"s64Value\":{"
3765 "\"properties\":{"
3766 "\"type\":{"
3767 "\"type\":\"string\","
3768 "\"enum\":[\"s64\"]"
3769 "},"
3770 "\"value\":{"
3771 "\"type\":\"integer\","
3772 "\"minimum\":-9007199254740991,"
3773 "\"maximum\":9007199254740991"
3774 "}"
3775 "},"
3776 "\"required\":[\"type\",\"value\"]"
3777 "},"
3778 "\"u32Value\":{"
3779 "\"properties\":{"
3780 "\"type\":{"
3781 "\"type\":\"string\","
3782 "\"enum\":[\"u32\"]"
3783 "},"
3784 "\"value\":{"
3785 "\"type\":\"integer\","
3786 "\"minimum\":0,"
3787 "\"maximum\":4294967295"
3788 "}"
3789 "},"
3790 "\"required\":[\"type\",\"value\"]"
3791 "},"
3792 "\"u64Value\":{"
3793 "\"properties\":{"
3794 "\"type\":{"
3795 "\"type\":\"string\","
3796 "\"enum\":[\"u64\"]"
3797 "},"
3798 "\"value\":{"
3799 "\"type\":\"integer\","
3800 "\"minimum\":0,"
3801 "\"maximum\":9007199254740991"
3802 "}"
3803 "},"
3804 "\"required\":[\"type\",\"value\"]"
3805 "},"
3806 "\"strValue\":{"
3807 "\"properties\":{"
3808 "\"type\":{"
3809 "\"type\":\"string\","
3810 "\"enum\":[\"str\"]"
3811 "},"
3812 "\"value\":{\"type\":\"string\"}"
3813 "},"
3814 "\"required\":[\"type\",\"value\"]"
3815 "},"
3816 "\"unknownValue\":{"
3817 "\"properties\":{"
3818 "\"type\":{"
3819 "\"type\":\"integer\","
3820 "\"minimum\":0"
3821 "},"
3822 "\"value\":{"
3823 "\"type\":\"string\","
3824 "\"enum\":[\"unknown\"]"
3825 "}"
3826 "},"
3827 "\"required\":[\"type\",\"value\"]"
3828 "}"
3829 "}"
3830 "}"
3831 "}"
3832 "}");
3833
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003834 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003835 chunk_reset(out);
3836 chunk_appendf(out,
3837 "{\"errorStr\":\"output buffer too short\"}");
3838 }
3839}
3840
3841/* This function dumps the schema onto the stream interface's read buffer.
3842 * It returns 0 as long as it does not complete, non-zero upon completion.
3843 * No state is used.
3844 */
3845static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3846{
3847 chunk_reset(&trash);
3848
3849 stats_dump_json_schema(&trash);
3850
Willy Tarreau06d80a92017-10-19 14:32:15 +02003851 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003852 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003853 return 0;
3854 }
3855
3856 return 1;
3857}
3858
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003859static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003860{
3861 struct proxy *px;
3862 struct server *sv;
3863 struct listener *li;
3864 int clrall = 0;
3865
3866 if (strcmp(args[2], "all") == 0)
3867 clrall = 1;
3868
3869 /* check permissions */
3870 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3871 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3872 return 1;
3873
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003874 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003875 if (clrall) {
3876 memset(&px->be_counters, 0, sizeof(px->be_counters));
3877 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3878 }
3879 else {
3880 px->be_counters.conn_max = 0;
3881 px->be_counters.p.http.rps_max = 0;
3882 px->be_counters.sps_max = 0;
3883 px->be_counters.cps_max = 0;
3884 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003885 px->be_counters.qtime_max = 0;
3886 px->be_counters.ctime_max = 0;
3887 px->be_counters.dtime_max = 0;
3888 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003889
3890 px->fe_counters.conn_max = 0;
3891 px->fe_counters.p.http.rps_max = 0;
3892 px->fe_counters.sps_max = 0;
3893 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003894 }
3895
3896 for (sv = px->srv; sv; sv = sv->next)
3897 if (clrall)
3898 memset(&sv->counters, 0, sizeof(sv->counters));
3899 else {
3900 sv->counters.cur_sess_max = 0;
3901 sv->counters.nbpend_max = 0;
3902 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003903 sv->counters.qtime_max = 0;
3904 sv->counters.ctime_max = 0;
3905 sv->counters.dtime_max = 0;
3906 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003907 }
3908
3909 list_for_each_entry(li, &px->conf.listeners, by_fe)
3910 if (li->counters) {
3911 if (clrall)
3912 memset(li->counters, 0, sizeof(*li->counters));
3913 else
3914 li->counters->conn_max = 0;
3915 }
3916 }
3917
3918 global.cps_max = 0;
3919 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003920 global.ssl_max = 0;
3921 global.ssl_fe_keys_max = 0;
3922 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003923
3924 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003925 return 1;
3926}
3927
3928
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003929static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003930{
Willy Tarreau2f397382019-10-09 11:43:59 +02003931 int arg = 2;
3932
Willy Tarreaud25fc792016-12-16 12:33:47 +01003933 appctx->ctx.stats.scope_str = 0;
3934 appctx->ctx.stats.scope_len = 0;
3935 appctx->ctx.stats.flags = 0;
3936
Willy Tarreau2f397382019-10-09 11:43:59 +02003937 while (*args[arg]) {
3938 if (strcmp(args[arg], "typed") == 0)
3939 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3940 else if (strcmp(args[arg], "json") == 0)
3941 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3942 else if (strcmp(args[arg], "desc") == 0)
3943 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3944 arg++;
3945 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003946 return 0;
3947}
3948
3949
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003950static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003951{
Willy Tarreau2f397382019-10-09 11:43:59 +02003952 int arg = 2;
3953
Willy Tarreaud25fc792016-12-16 12:33:47 +01003954 appctx->ctx.stats.scope_str = 0;
3955 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003956 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3957
3958 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3959 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003960
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003961 /* proxy is the default domain */
3962 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3963 if (!strcmp(args[arg], "domain")) {
3964 ++args;
3965
3966 if (!strcmp(args[arg], "proxy"))
3967 ++args;
3968 else
3969 return cli_err(appctx, "Invalid statistics domain.\n");
3970 }
3971
3972 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
3973 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003974 struct proxy *px;
3975
Willy Tarreau2f397382019-10-09 11:43:59 +02003976 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003977 if (px)
3978 appctx->ctx.stats.iid = px->uuid;
3979 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003980 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003981
Willy Tarreau9d008692019-08-09 11:21:01 +02003982 if (!appctx->ctx.stats.iid)
3983 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003984
Willy Tarreau2b812e22016-11-22 16:18:05 +01003985 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003986 appctx->ctx.stats.type = atoi(args[arg+1]);
3987 appctx->ctx.stats.sid = atoi(args[arg+2]);
3988 arg += 3;
3989 }
3990
3991 while (*args[arg]) {
3992 if (strcmp(args[arg], "typed") == 0)
3993 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3994 else if (strcmp(args[arg], "json") == 0)
3995 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3996 else if (strcmp(args[arg], "desc") == 0)
3997 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3998 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003999 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004000
Willy Tarreau2b812e22016-11-22 16:18:05 +01004001 return 0;
4002}
4003
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004004static int cli_io_handler_dump_info(struct appctx *appctx)
4005{
4006 return stats_dump_info_to_buffer(appctx->owner);
4007}
4008
Willy Tarreau2b812e22016-11-22 16:18:05 +01004009/* This I/O handler runs as an applet embedded in a stream interface. It is
4010 * used to send raw stats over a socket.
4011 */
4012static int cli_io_handler_dump_stat(struct appctx *appctx)
4013{
Christopher Fauletef779222018-10-31 08:47:01 +01004014 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004015}
4016
Simon Horman6f6bb382017-01-04 09:37:26 +01004017static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4018{
4019 return stats_dump_json_schema_to_buffer(appctx->owner);
4020}
4021
Willy Tarreau2b812e22016-11-22 16:18:05 +01004022/* register cli keywords */
4023static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004024 { { "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 +02004025 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
4026 { { "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 +01004027 { { "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 +01004028 {{},}
4029}};
4030
Willy Tarreau0108d902018-11-25 19:14:37 +01004031INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4032
William Lallemand74c24fb2016-11-21 17:18:36 +01004033struct applet http_stats_applet = {
4034 .obj_type = OBJ_TYPE_APPLET,
4035 .name = "<STATS>", /* used for logging */
4036 .fct = http_stats_io_handler,
4037 .release = NULL,
4038};
4039
William Lallemand74c24fb2016-11-21 17:18:36 +01004040/*
4041 * Local variables:
4042 * c-indent-level: 8
4043 * c-basic-offset: 8
4044 * End:
4045 */