blob: 66b8a49475733773ab62bd9545b24e20f3980aed [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
Christopher Faulet6338a082019-09-09 15:50:54 +0200260static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100261
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200262int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100263{
264 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100265 if (chk->data >= channel_htx_recv_max(chn, htx))
266 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200267 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100268 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100269 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100270 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100271 }
272 else {
273 if (ci_putchk(chn, chk) == -1)
274 return 0;
275 }
276 return 1;
277}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100278
Christopher Fauleted7a0662019-01-14 11:07:34 +0100279static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
280{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200281 struct channel *req = si_oc(si);
282 struct htx *htx = htxbuf(&req->buf);
283 struct htx_blk *blk;
284 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100285
Christopher Fauletb7f88902019-07-15 21:56:43 +0200286 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100287 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200288 ALREADY_CHECKED(blk);
289 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
290 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100291}
292
William Lallemand74c24fb2016-11-21 17:18:36 +0100293/*
294 * http_stats_io_handler()
295 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
296 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100297 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100298 * -> stats_dump_html_head() // emits the HTML headers
299 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
300 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
301 * -> stats_dump_html_px_hdr()
302 * -> stats_dump_fe_stats()
303 * -> stats_dump_li_stats()
304 * -> stats_dump_sv_stats()
305 * -> stats_dump_be_stats()
306 * -> stats_dump_html_px_end()
307 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100308 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100309 */
310
311
William Lallemand74c24fb2016-11-21 17:18:36 +0100312/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
313 * for clearing it if needed.
314 * NOTE: Some tools happen to rely on the field position instead of its name,
315 * so please only append new fields at the end, never in the middle.
316 */
317static void stats_dump_csv_header()
318{
319 int field;
320
321 chunk_appendf(&trash, "# ");
322 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200323 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100324
325 chunk_appendf(&trash, "\n");
326}
327
328
329/* Emits a stats field without any surrounding element and properly encoded to
330 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
331 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200332int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100333{
334 switch (field_format(f, 0)) {
335 case FF_EMPTY: return 1;
336 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
337 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
338 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
339 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200340 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100341 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
342 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
343 }
344}
345
346/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
347 * output is supposed to be used on its own line. Returns non-zero on success, 0
348 * if the buffer is full.
349 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200350int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100351{
352 switch (field_format(f, 0)) {
353 case FF_EMPTY: return 1;
354 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
355 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
356 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
357 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200358 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100359 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
360 default: return chunk_appendf(out, "%08x:?", f->type);
361 }
362}
363
Simon Horman05ee2132017-01-04 09:37:25 +0100364/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
365 * the recommendation for interoperable integers in section 6 of RFC 7159.
366 */
367#define JSON_INT_MAX ((1ULL << 53) - 1)
368#define JSON_INT_MIN (0 - JSON_INT_MAX)
369
370/* Emits a stats field value and its type in JSON.
371 * Returns non-zero on success, 0 on error.
372 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200373int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100374{
375 int old_len;
376 char buf[20];
377 const char *type, *value = buf, *quote = "";
378
379 switch (field_format(f, 0)) {
380 case FF_EMPTY: return 1;
381 case FF_S32: type = "\"s32\"";
382 snprintf(buf, sizeof(buf), "%d", f->u.s32);
383 break;
384 case FF_U32: type = "\"u32\"";
385 snprintf(buf, sizeof(buf), "%u", f->u.u32);
386 break;
387 case FF_S64: type = "\"s64\"";
388 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
389 return 0;
390 type = "\"s64\"";
391 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
392 break;
393 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
394 return 0;
395 type = "\"u64\"";
396 snprintf(buf, sizeof(buf), "%llu",
397 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200398 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200399 case FF_FLT: type = "\"flt\"";
400 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100401 break;
402 case FF_STR: type = "\"str\"";
403 value = field_str(f, 0);
404 quote = "\"";
405 break;
406 default: snprintf(buf, sizeof(buf), "%u", f->type);
407 type = buf;
408 value = "unknown";
409 quote = "\"";
410 break;
411 }
412
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200413 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100414 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
415 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200416 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100417}
418
William Lallemand74c24fb2016-11-21 17:18:36 +0100419/* Emits an encoding of the field type on 3 characters followed by a delimiter.
420 * Returns non-zero on success, 0 if the buffer is full.
421 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200422int stats_emit_field_tags(struct buffer *out, const struct field *f,
423 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100424{
425 char origin, nature, scope;
426
427 switch (field_origin(f, 0)) {
428 case FO_METRIC: origin = 'M'; break;
429 case FO_STATUS: origin = 'S'; break;
430 case FO_KEY: origin = 'K'; break;
431 case FO_CONFIG: origin = 'C'; break;
432 case FO_PRODUCT: origin = 'P'; break;
433 default: origin = '?'; break;
434 }
435
436 switch (field_nature(f, 0)) {
437 case FN_GAUGE: nature = 'G'; break;
438 case FN_LIMIT: nature = 'L'; break;
439 case FN_MIN: nature = 'm'; break;
440 case FN_MAX: nature = 'M'; break;
441 case FN_RATE: nature = 'R'; break;
442 case FN_COUNTER: nature = 'C'; break;
443 case FN_DURATION: nature = 'D'; break;
444 case FN_AGE: nature = 'A'; break;
445 case FN_TIME: nature = 'T'; break;
446 case FN_NAME: nature = 'N'; break;
447 case FN_OUTPUT: nature = 'O'; break;
448 case FN_AVG: nature = 'a'; break;
449 default: nature = '?'; break;
450 }
451
452 switch (field_scope(f, 0)) {
453 case FS_PROCESS: scope = 'P'; break;
454 case FS_SERVICE: scope = 'S'; break;
455 case FS_SYSTEM: scope = 's'; break;
456 case FS_CLUSTER: scope = 'C'; break;
457 default: scope = '?'; break;
458 }
459
460 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
461}
462
Simon Horman05ee2132017-01-04 09:37:25 +0100463/* Emits an encoding of the field type as JSON.
464 * Returns non-zero on success, 0 if the buffer is full.
465 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200466int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100467{
468 const char *origin, *nature, *scope;
469 int old_len;
470
471 switch (field_origin(f, 0)) {
472 case FO_METRIC: origin = "Metric"; break;
473 case FO_STATUS: origin = "Status"; break;
474 case FO_KEY: origin = "Key"; break;
475 case FO_CONFIG: origin = "Config"; break;
476 case FO_PRODUCT: origin = "Product"; break;
477 default: origin = "Unknown"; break;
478 }
479
480 switch (field_nature(f, 0)) {
481 case FN_GAUGE: nature = "Gauge"; break;
482 case FN_LIMIT: nature = "Limit"; break;
483 case FN_MIN: nature = "Min"; break;
484 case FN_MAX: nature = "Max"; break;
485 case FN_RATE: nature = "Rate"; break;
486 case FN_COUNTER: nature = "Counter"; break;
487 case FN_DURATION: nature = "Duration"; break;
488 case FN_AGE: nature = "Age"; break;
489 case FN_TIME: nature = "Time"; break;
490 case FN_NAME: nature = "Name"; break;
491 case FN_OUTPUT: nature = "Output"; break;
492 case FN_AVG: nature = "Avg"; break;
493 default: nature = "Unknown"; break;
494 }
495
496 switch (field_scope(f, 0)) {
497 case FS_PROCESS: scope = "Process"; break;
498 case FS_SERVICE: scope = "Service"; break;
499 case FS_SYSTEM: scope = "System"; break;
500 case FS_CLUSTER: scope = "Cluster"; break;
501 default: scope = "Unknown"; break;
502 }
503
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200504 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100505 chunk_appendf(out, "\"tags\":{"
506 "\"origin\":\"%s\","
507 "\"nature\":\"%s\","
508 "\"scope\":\"%s\""
509 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200510 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100511}
William Lallemand74c24fb2016-11-21 17:18:36 +0100512
513/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200514static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200515 const struct field *stats, size_t stats_count,
516 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100517{
518 int field;
519
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200520 for (field = 0; field < stats_count; field++) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100521 if (!stats_emit_raw_data_field(out, &stats[field]))
522 return 0;
523 if (!chunk_strcat(out, ","))
524 return 0;
525 }
526 chunk_strcat(out, "\n");
527 return 1;
528}
529
530/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200531static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200532 const struct field *stats,
533 size_t stats_count,
534 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100535{
536 int field;
537
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200538 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100539 if (!stats[field].type)
540 continue;
541
542 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
543 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
544 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
545 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
546 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
547 '?',
548 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200549 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100550
551 if (!stats_emit_field_tags(out, &stats[field], ':'))
552 return 0;
553 if (!stats_emit_typed_data_field(out, &stats[field]))
554 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200555 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
556 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100557 if (!chunk_strcat(out, "\n"))
558 return 0;
559 }
560 return 1;
561}
562
Simon Horman05ee2132017-01-04 09:37:25 +0100563/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200564static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200565 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100566{
567 int field;
568 int started = 0;
569
570 if (!chunk_strcat(out, "["))
571 return 0;
572
573 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
574 int old_len;
575
576 if (!field_format(info, field))
577 continue;
578
579 if (started && !chunk_strcat(out, ","))
580 goto err;
581 started = 1;
582
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100584 chunk_appendf(out,
585 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
586 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200587 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100588 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200589 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100590 goto err;
591
592 if (!stats_emit_json_field_tags(out, &info[field]))
593 goto err;
594
595 if (!stats_emit_json_data_field(out, &info[field]))
596 goto err;
597
598 if (!chunk_strcat(out, "}"))
599 goto err;
600 }
601
602 if (!chunk_strcat(out, "]"))
603 goto err;
604 return 1;
605
606err:
607 chunk_reset(out);
608 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
609 return 0;
610}
611
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200612static void stats_print_proxy_field_json(struct buffer *out,
613 const struct field *stat,
614 const char *name,
615 int pos,
616 uint32_t field_type,
617 uint32_t iid,
618 uint32_t sid,
619 uint32_t pid)
620{
621 const char *obj_type;
622 switch (field_type) {
623 case STATS_TYPE_FE: obj_type = "Frontend"; break;
624 case STATS_TYPE_BE: obj_type = "Backend"; break;
625 case STATS_TYPE_SO: obj_type = "Listener"; break;
626 case STATS_TYPE_SV: obj_type = "Server"; break;
627 default: obj_type = "Unknown"; break;
628 }
629
630 chunk_appendf(out,
631 "{"
632 "\"objType\":\"%s\","
633 "\"proxyId\":%u,"
634 "\"id\":%u,"
635 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
636 "\"processNum\":%u,",
637 obj_type, iid, sid, pos, name, pid);
638}
639
Simon Horman05ee2132017-01-04 09:37:25 +0100640/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200641static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200642 const struct field *stats, size_t stats_count,
643 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100644{
645 int field;
646 int started = 0;
647
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200648 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100649 return 0;
650 if (!chunk_strcat(out, "["))
651 return 0;
652
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200653 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100654 int old_len;
655
656 if (!stats[field].type)
657 continue;
658
659 if (started && !chunk_strcat(out, ","))
660 goto err;
661 started = 1;
662
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200663 old_len = out->data;
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200664 stats_print_proxy_field_json(out, &stats[field],
665 stat_fields[field].name, field,
666 stats[ST_F_TYPE].u.u32,
667 stats[ST_F_IID].u.u32,
668 stats[ST_F_SID].u.u32,
669 stats[ST_F_PID].u.u32);
670
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200671 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100672 goto err;
673
674 if (!stats_emit_json_field_tags(out, &stats[field]))
675 goto err;
676
677 if (!stats_emit_json_data_field(out, &stats[field]))
678 goto err;
679
680 if (!chunk_strcat(out, "}"))
681 goto err;
682 }
683
684 if (!chunk_strcat(out, "]"))
685 goto err;
686
687 return 1;
688
689err:
690 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200691 if (flags & STAT_STARTED)
692 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100693 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
694 return 0;
695}
696
William Lallemand74c24fb2016-11-21 17:18:36 +0100697/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200698 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200699 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100700 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200701static int stats_dump_fields_html(struct buffer *out,
702 const struct field *stats,
703 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100704{
Willy Tarreau83061a82018-07-13 11:56:34 +0200705 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100706
707 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
708 chunk_appendf(out,
709 /* name, queue */
710 "<tr class=\"frontend\">");
711
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200712 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100713 /* Column sub-heading for Enable or Disable server */
714 chunk_appendf(out, "<td></td>");
715 }
716
717 chunk_appendf(out,
718 "<td class=ac>"
719 "<a name=\"%s/Frontend\"></a>"
720 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
721 "<td colspan=3></td>"
722 "",
723 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
724
725 chunk_appendf(out,
726 /* sessions rate : current */
727 "<td><u>%s<div class=tips><table class=det>"
728 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
729 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
730 "",
731 U2H(stats[ST_F_RATE].u.u32),
732 U2H(stats[ST_F_CONN_RATE].u.u32),
733 U2H(stats[ST_F_RATE].u.u32));
734
735 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
736 chunk_appendf(out,
737 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
738 U2H(stats[ST_F_REQ_RATE].u.u32));
739
740 chunk_appendf(out,
741 "</table></div></u></td>"
742 /* sessions rate : max */
743 "<td><u>%s<div class=tips><table class=det>"
744 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
745 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
746 "",
747 U2H(stats[ST_F_RATE_MAX].u.u32),
748 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
749 U2H(stats[ST_F_RATE_MAX].u.u32));
750
751 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
752 chunk_appendf(out,
753 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
754 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
755
756 chunk_appendf(out,
757 "</table></div></u></td>"
758 /* sessions rate : limit */
759 "<td>%s</td>",
760 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
761
762 chunk_appendf(out,
763 /* sessions: current, max, limit, total */
764 "<td>%s</td><td>%s</td><td>%s</td>"
765 "<td><u>%s<div class=tips><table class=det>"
766 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
767 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
768 "",
769 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
770 U2H(stats[ST_F_STOT].u.u64),
771 U2H(stats[ST_F_CONN_TOT].u.u64),
772 U2H(stats[ST_F_STOT].u.u64));
773
774 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
775 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
776 chunk_appendf(out,
777 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
778 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
779 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
780 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
781 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
782 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
783 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
784 "<tr><th>- other responses:</th><td>%s</td></tr>"
785 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100786 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
787 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200788 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100789 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100790 "",
791 U2H(stats[ST_F_REQ_TOT].u.u64),
792 U2H(stats[ST_F_HRSP_1XX].u.u64),
793 U2H(stats[ST_F_HRSP_2XX].u.u64),
794 U2H(stats[ST_F_COMP_RSP].u.u64),
795 stats[ST_F_HRSP_2XX].u.u64 ?
796 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
797 U2H(stats[ST_F_HRSP_3XX].u.u64),
798 U2H(stats[ST_F_HRSP_4XX].u.u64),
799 U2H(stats[ST_F_HRSP_5XX].u.u64),
800 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200801 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100802 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
803 U2H(stats[ST_F_CACHE_HITS].u.u64),
804 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
805 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100806 U2H(stats[ST_F_WREW].u.u64),
807 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100808 }
809
810 chunk_appendf(out,
811 "</table></div></u></td>"
812 /* sessions: lbtot, lastsess */
813 "<td></td><td></td>"
814 /* bytes : in */
815 "<td>%s</td>"
816 "",
817 U2H(stats[ST_F_BIN].u.u64));
818
819 chunk_appendf(out,
820 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
821 "<td>%s%s<div class=tips><table class=det>"
822 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
823 "<tr><th>Compression in:</th><td>%s</td></tr>"
824 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
825 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
826 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
827 "</table></div>%s</td>",
828 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
829 U2H(stats[ST_F_BOUT].u.u64),
830 U2H(stats[ST_F_BOUT].u.u64),
831 U2H(stats[ST_F_COMP_IN].u.u64),
832 U2H(stats[ST_F_COMP_OUT].u.u64),
833 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
834 U2H(stats[ST_F_COMP_BYP].u.u64),
835 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
836 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,
837 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
838
839 chunk_appendf(out,
840 /* denied: req, resp */
841 "<td>%s</td><td>%s</td>"
842 /* errors : request, connect, response */
843 "<td>%s</td><td></td><td></td>"
844 /* warnings: retries, redispatches */
845 "<td></td><td></td>"
846 /* server status : reflect frontend status */
847 "<td class=ac>%s</td>"
848 /* rest of server: nothing */
849 "<td class=ac colspan=8></td></tr>"
850 "",
851 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
852 U2H(stats[ST_F_EREQ].u.u64),
853 field_str(stats, ST_F_STATUS));
854 }
855 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
856 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200857 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100858 /* Column sub-heading for Enable or Disable server */
859 chunk_appendf(out, "<td></td>");
860 }
861
862 chunk_appendf(out,
863 /* frontend name, listener name */
864 "<td class=ac><a name=\"%s/+%s\"></a>%s"
865 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
866 "",
867 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200868 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100869 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
870
Willy Tarreau708c4162019-10-09 10:19:16 +0200871 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100872 chunk_appendf(out, "<div class=tips>");
873
Willy Tarreau90807112020-02-25 08:16:33 +0100874 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100875 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
876 else if (*field_str(stats, ST_F_ADDR) == '[')
877 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
878 else if (*field_str(stats, ST_F_ADDR))
879 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
880
881 /* id */
882 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
883 }
884
885 chunk_appendf(out,
886 /* queue */
887 "%s</td><td colspan=3></td>"
888 /* sessions rate: current, max, limit */
889 "<td colspan=3>&nbsp;</td>"
890 /* sessions: current, max, limit, total, lbtot, lastsess */
891 "<td>%s</td><td>%s</td><td>%s</td>"
892 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
893 /* bytes: in, out */
894 "<td>%s</td><td>%s</td>"
895 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200896 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100897 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
898 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
899
900 chunk_appendf(out,
901 /* denied: req, resp */
902 "<td>%s</td><td>%s</td>"
903 /* errors: request, connect, response */
904 "<td>%s</td><td></td><td></td>"
905 /* warnings: retries, redispatches */
906 "<td></td><td></td>"
907 /* server status: reflect listener status */
908 "<td class=ac>%s</td>"
909 /* rest of server: nothing */
910 "<td class=ac colspan=8></td></tr>"
911 "",
912 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
913 U2H(stats[ST_F_EREQ].u.u64),
914 field_str(stats, ST_F_STATUS));
915 }
916 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
917 const char *style;
918
919 /* determine the style to use depending on the server's state,
920 * its health and weight. There isn't a 1-to-1 mapping between
921 * state and styles for the cases where the server is (still)
922 * up. The reason is that we don't want to report nolb and
923 * drain with the same color.
924 */
925
926 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
927 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
928 style = "down";
929 }
930 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
931 style = "going_up";
932 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400933 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
934 style = "draining";
935 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100936 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
937 style = "going_down";
938 }
939 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
940 style = "nolb";
941 }
942 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
943 style = "no_check";
944 }
945 else if (!stats[ST_F_CHKFAIL].type ||
946 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
947 /* no check or max health = UP */
948 if (stats[ST_F_WEIGHT].u.u32)
949 style = "up";
950 else
951 style = "draining";
952 }
953 else {
954 style = "going_down";
955 }
956
Willy Tarreau7b524852020-08-11 10:26:36 +0200957 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +0100958 chunk_appendf(out, "<tr class=\"maintain\">");
959 else
960 chunk_appendf(out,
961 "<tr class=\"%s_%s\">",
962 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
963
964
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200965 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100966 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000967 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
968 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100969 field_str(stats, ST_F_SVNAME));
970
971 chunk_appendf(out,
972 "<td class=ac><a name=\"%s/%s\"></a>%s"
973 "<a class=lfsb href=\"#%s/%s\">%s</a>"
974 "",
975 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200976 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100977 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
978
Willy Tarreau708c4162019-10-09 10:19:16 +0200979 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100980 chunk_appendf(out, "<div class=tips>");
981
Willy Tarreau90807112020-02-25 08:16:33 +0100982 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100983 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
984 else if (*field_str(stats, ST_F_ADDR) == '[')
985 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
986 else if (*field_str(stats, ST_F_ADDR))
987 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
988
989 /* id */
990 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
991
992 /* cookie */
993 if (stats[ST_F_COOKIE].type) {
994 chunk_appendf(out, ", cookie: '");
995 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
996 chunk_htmlencode(out, &src);
997 chunk_appendf(out, "'");
998 }
999
1000 chunk_appendf(out, "</div>");
1001 }
1002
1003 chunk_appendf(out,
1004 /* queue : current, max, limit */
1005 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1006 /* sessions rate : current, max, limit */
1007 "<td>%s</td><td>%s</td><td></td>"
1008 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001009 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001010 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1011 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1012
1013 chunk_appendf(out,
1014 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001015 "<td><u>%s<div class=tips>"
1016 "<table class=det>"
1017 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001018 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001019 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001020 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1021 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001022 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001023 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1024 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1025 "</table></div></u>"
1026 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001027 "<td><u>%s<div class=tips><table class=det>"
1028 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1029 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001030 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001031 U2H(stats[ST_F_SCUR].u.u32),
1032 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1033 U2H(stats[ST_F_SRV_ICUR].u.u32),
1034 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1035 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001036 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001037
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001038 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1039 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1040 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001041 U2H(stats[ST_F_STOT].u.u64),
1042 U2H(stats[ST_F_STOT].u.u64));
1043
1044 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1045 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001046 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001047 "<tr><th>New connections:</th><td>%s</td></tr>"
1048 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001049 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001050 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1051 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1052 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1053 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1054 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1055 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001056 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001057 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001058 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001059 U2H(stats[ST_F_CONNECT].u.u64),
1060 U2H(stats[ST_F_REUSE].u.u64),
1061 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1062 (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 +02001063 U2H(stats[ST_F_REQ_TOT].u.u64),
1064 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1065 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1066 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1067 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1068 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1069 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1070 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1071 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1072 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1073 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1074 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1075 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001076 U2H(stats[ST_F_WREW].u.u64),
1077 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001078 }
1079
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001080 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1081 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1082 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1083 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1084 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001085 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001086 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1087 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1088 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1089 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001090
1091 chunk_appendf(out,
1092 "</table></div></u></td>"
1093 /* sessions: lbtot, last */
1094 "<td>%s</td><td>%s</td>",
1095 U2H(stats[ST_F_LBTOT].u.u64),
1096 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1097
1098 chunk_appendf(out,
1099 /* bytes : in, out */
1100 "<td>%s</td><td>%s</td>"
1101 /* denied: req, resp */
1102 "<td></td><td>%s</td>"
1103 /* errors : request, connect */
1104 "<td></td><td>%s</td>"
1105 /* errors : response */
1106 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1107 /* warnings: retries, redispatches */
1108 "<td>%lld</td><td>%lld</td>"
1109 "",
1110 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1111 U2H(stats[ST_F_DRESP].u.u64),
1112 U2H(stats[ST_F_ECON].u.u64),
1113 U2H(stats[ST_F_ERESP].u.u64),
1114 (long long)stats[ST_F_CLI_ABRT].u.u64,
1115 (long long)stats[ST_F_SRV_ABRT].u.u64,
1116 (long long)stats[ST_F_WRETR].u.u64,
1117 (long long)stats[ST_F_WREDIS].u.u64);
1118
1119 /* status, last change */
1120 chunk_appendf(out, "<td class=ac>");
1121
1122 /* FIXME!!!!
1123 * LASTCHG should contain the last change for *this* server and must be computed
1124 * properly above, as was done below, ie: this server if maint, otherwise ref server
1125 * if tracking. Note that ref is either local or remote depending on tracking.
1126 */
1127
1128
Willy Tarreau7b524852020-08-11 10:26:36 +02001129 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001130 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1131 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001132 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001133 chunk_strcat(out, "<i>no check</i>");
1134 }
1135 else {
1136 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 +02001137 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001138 if (stats[ST_F_CHECK_HEALTH].u.u32)
1139 chunk_strcat(out, " &uarr;");
1140 }
1141 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1142 chunk_strcat(out, " &darr;");
1143 }
1144
Willy Tarreau7b524852020-08-11 10:26:36 +02001145 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001146 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1147 chunk_appendf(out,
1148 "</td><td class=ac><u> %s",
1149 field_str(stats, ST_F_AGENT_STATUS));
1150
1151 if (stats[ST_F_AGENT_CODE].type)
1152 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1153
1154 if (stats[ST_F_AGENT_DURATION].type)
1155 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1156
1157 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1158
1159 if (*field_str(stats, ST_F_LAST_AGT)) {
1160 chunk_appendf(out, ": ");
1161 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1162 chunk_htmlencode(out, &src);
1163 }
1164 chunk_appendf(out, "</div></u>");
1165 }
1166 else if (stats[ST_F_CHECK_STATUS].type) {
1167 chunk_appendf(out,
1168 "</td><td class=ac><u> %s",
1169 field_str(stats, ST_F_CHECK_STATUS));
1170
1171 if (stats[ST_F_CHECK_CODE].type)
1172 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1173
1174 if (stats[ST_F_CHECK_DURATION].type)
1175 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1176
1177 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1178
1179 if (*field_str(stats, ST_F_LAST_CHK)) {
1180 chunk_appendf(out, ": ");
1181 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1182 chunk_htmlencode(out, &src);
1183 }
1184 chunk_appendf(out, "</div></u>");
1185 }
1186 else
1187 chunk_appendf(out, "</td><td>");
1188
1189 chunk_appendf(out,
1190 /* weight */
1191 "</td><td class=ac>%d</td>"
1192 /* act, bck */
1193 "<td class=ac>%s</td><td class=ac>%s</td>"
1194 "",
1195 stats[ST_F_WEIGHT].u.u32,
1196 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1197 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1198
1199 /* check failures: unique, fatal, down time */
1200 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1201 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1202 }
1203 else if (stats[ST_F_CHKFAIL].type) {
1204 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1205
1206 if (stats[ST_F_HANAFAIL].type)
1207 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1208
1209 chunk_appendf(out,
1210 "<div class=tips>Failed Health Checks%s</div></u></td>"
1211 "<td>%lld</td><td>%s</td>"
1212 "",
1213 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1214 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1215 }
1216 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1217 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1218 chunk_appendf(out,
1219 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1220 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1221 }
1222 else
1223 chunk_appendf(out, "<td colspan=3></td>");
1224
1225 /* throttle */
1226 if (stats[ST_F_THROTTLE].type)
1227 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1228 else
1229 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1230 }
1231 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1232 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001233 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001234 /* Column sub-heading for Enable or Disable server */
1235 chunk_appendf(out, "<td></td>");
1236 }
1237 chunk_appendf(out,
1238 "<td class=ac>"
1239 /* name */
1240 "%s<a name=\"%s/Backend\"></a>"
1241 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1242 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001243 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001244 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1245
Willy Tarreau708c4162019-10-09 10:19:16 +02001246 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001247 /* balancing */
1248 chunk_appendf(out, "<div class=tips>balancing: %s",
1249 field_str(stats, ST_F_ALGO));
1250
1251 /* cookie */
1252 if (stats[ST_F_COOKIE].type) {
1253 chunk_appendf(out, ", cookie: '");
1254 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1255 chunk_htmlencode(out, &src);
1256 chunk_appendf(out, "'");
1257 }
1258 chunk_appendf(out, "</div>");
1259 }
1260
1261 chunk_appendf(out,
1262 "%s</td>"
1263 /* queue : current, max */
1264 "<td>%s</td><td>%s</td><td></td>"
1265 /* sessions rate : current, max, limit */
1266 "<td>%s</td><td>%s</td><td></td>"
1267 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001268 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001269 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1270 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1271
1272 chunk_appendf(out,
1273 /* sessions: current, max, limit, total */
1274 "<td>%s</td><td>%s</td><td>%s</td>"
1275 "<td><u>%s<div class=tips><table class=det>"
1276 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1277 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001278 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 +01001279 U2H(stats[ST_F_STOT].u.u64),
1280 U2H(stats[ST_F_STOT].u.u64));
1281
1282 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1283 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1284 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001285 "<tr><th>New connections:</th><td>%s</td></tr>"
1286 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001287 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1288 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1289 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1290 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1291 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1292 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1293 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1294 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001295 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1296 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001297 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001298 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001299 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001300 U2H(stats[ST_F_CONNECT].u.u64),
1301 U2H(stats[ST_F_REUSE].u.u64),
1302 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1303 (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 +01001304 U2H(stats[ST_F_REQ_TOT].u.u64),
1305 U2H(stats[ST_F_HRSP_1XX].u.u64),
1306 U2H(stats[ST_F_HRSP_2XX].u.u64),
1307 U2H(stats[ST_F_COMP_RSP].u.u64),
1308 stats[ST_F_HRSP_2XX].u.u64 ?
1309 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1310 U2H(stats[ST_F_HRSP_3XX].u.u64),
1311 U2H(stats[ST_F_HRSP_4XX].u.u64),
1312 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001313 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001314 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1315 U2H(stats[ST_F_CACHE_HITS].u.u64),
1316 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1317 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001318 U2H(stats[ST_F_WREW].u.u64),
1319 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001320 }
1321
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001322 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1323 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1324 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1325 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1326 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001327 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001328 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1329 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1330 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1331 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001332
1333 chunk_appendf(out,
1334 "</table></div></u></td>"
1335 /* sessions: lbtot, last */
1336 "<td>%s</td><td>%s</td>"
1337 /* bytes: in */
1338 "<td>%s</td>"
1339 "",
1340 U2H(stats[ST_F_LBTOT].u.u64),
1341 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1342 U2H(stats[ST_F_BIN].u.u64));
1343
1344 chunk_appendf(out,
1345 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1346 "<td>%s%s<div class=tips><table class=det>"
1347 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1348 "<tr><th>Compression in:</th><td>%s</td></tr>"
1349 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1350 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1351 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1352 "</table></div>%s</td>",
1353 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1354 U2H(stats[ST_F_BOUT].u.u64),
1355 U2H(stats[ST_F_BOUT].u.u64),
1356 U2H(stats[ST_F_COMP_IN].u.u64),
1357 U2H(stats[ST_F_COMP_OUT].u.u64),
1358 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1359 U2H(stats[ST_F_COMP_BYP].u.u64),
1360 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1361 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,
1362 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1363
1364 chunk_appendf(out,
1365 /* denied: req, resp */
1366 "<td>%s</td><td>%s</td>"
1367 /* errors : request, connect */
1368 "<td></td><td>%s</td>"
1369 /* errors : response */
1370 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1371 /* warnings: retries, redispatches */
1372 "<td>%lld</td><td>%lld</td>"
1373 /* backend status: reflect backend status (up/down): we display UP
1374 * if the backend has known working servers or if it has no server at
1375 * all (eg: for stats). Then we display the total weight, number of
1376 * active and backups. */
1377 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1378 "<td class=ac>%d</td><td class=ac>%d</td>"
1379 "",
1380 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1381 U2H(stats[ST_F_ECON].u.u64),
1382 U2H(stats[ST_F_ERESP].u.u64),
1383 (long long)stats[ST_F_CLI_ABRT].u.u64,
1384 (long long)stats[ST_F_SRV_ABRT].u.u64,
1385 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1386 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1387 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1388 stats[ST_F_WEIGHT].u.u32,
1389 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1390
1391 chunk_appendf(out,
1392 /* rest of backend: nothing, down transitions, total downtime, throttle */
1393 "<td class=ac>&nbsp;</td><td>%d</td>"
1394 "<td>%s</td>"
1395 "<td></td>"
1396 "</tr>",
1397 stats[ST_F_CHKDOWN].u.u32,
1398 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1399 }
1400 return 1;
1401}
1402
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001403int stats_dump_one_line(const struct field *stats, size_t stats_count,
1404 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001405{
Simon Horman05ee2132017-01-04 09:37:25 +01001406 int ret;
1407
William Lallemand74c24fb2016-11-21 17:18:36 +01001408 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001409 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001410 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001411 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001412 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001413 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001414 else
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001415 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001416
1417 if (ret)
1418 appctx->ctx.stats.flags |= STAT_STARTED;
1419
1420 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001421}
1422
1423/* Fill <stats> with the frontend statistics. <stats> is
1424 * preallocated array of length <len>. The length of the array
1425 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1426 * this value, the function returns 0, otherwise, it returns 1.
1427 */
1428int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1429{
1430 if (len < ST_F_TOTAL_FIELDS)
1431 return 0;
1432
1433 memset(stats, 0, sizeof(*stats) * len);
1434
1435 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1436 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1437 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1438 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1439 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1440 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1441 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1442 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1443 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1444 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1445 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1446 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1447 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1448 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1449 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1450 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1451 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1452 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1453 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1454 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1455 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1456 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001457 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001458 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001459
1460 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1461 if (px->mode == PR_MODE_HTTP) {
1462 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1463 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1464 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1465 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1466 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1467 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1468 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001469 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1470 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001471 }
1472
1473 /* requests : req_rate, req_rate_max, req_tot, */
1474 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1475 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1476 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1477
1478 /* compression: in, out, bypassed, responses */
1479 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1480 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1481 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1482 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1483
1484 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1485 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1486 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1487 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1488
1489 return 1;
1490}
1491
1492/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1493 * the state from stream interface <si>. The caller is responsible for clearing
1494 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1495 */
1496static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1497{
1498 struct appctx *appctx = __objt_appctx(si->end);
1499
1500 if (!(px->cap & PR_CAP_FE))
1501 return 0;
1502
1503 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1504 return 0;
1505
1506 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1507 return 0;
1508
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001509 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001510}
1511
1512/* Fill <stats> with the listener statistics. <stats> is
1513 * preallocated array of length <len>. The length of the array
1514 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1515 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001516 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001517 */
1518int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1519 struct field *stats, int len)
1520{
Willy Tarreau83061a82018-07-13 11:56:34 +02001521 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001522
1523 if (len < ST_F_TOTAL_FIELDS)
1524 return 0;
1525
1526 if (!l->counters)
1527 return 0;
1528
1529 chunk_reset(out);
1530 memset(stats, 0, sizeof(*stats) * len);
1531
1532 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1533 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1534 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1535 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1536 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1537 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1538 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1539 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1540 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1541 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1542 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1543 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1544 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1545 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001546 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 +01001547 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1548 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1549 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1550 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001551 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001552 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001553
Willy Tarreau708c4162019-10-09 10:19:16 +02001554 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001555 char str[INET6_ADDRSTRLEN];
1556 int port;
1557
Willy Tarreau37159062020-08-27 07:48:42 +02001558 port = get_host_port(&l->rx.addr);
1559 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001560 case AF_INET:
1561 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1562 chunk_appendf(out, "%s:%d", str, port);
1563 break;
1564 case AF_INET6:
1565 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1566 chunk_appendf(out, "[%s]:%d", str, port);
1567 break;
1568 case AF_UNIX:
1569 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1570 break;
1571 case -1:
1572 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1573 chunk_strcat(out, strerror(errno));
1574 break;
1575 default: /* address family not supported */
1576 break;
1577 }
1578 }
1579
1580 return 1;
1581}
1582
1583/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001584 * from stream interface <si>. The caller is responsible for clearing the trash
1585 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001586 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001587static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001588{
1589 struct appctx *appctx = __objt_appctx(si->end);
1590
Willy Tarreau43241ff2019-10-09 11:27:51 +02001591 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001592 return 0;
1593
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001594 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001595}
1596
1597enum srv_stats_state {
1598 SRV_STATS_STATE_DOWN = 0,
1599 SRV_STATS_STATE_DOWN_AGENT,
1600 SRV_STATS_STATE_GOING_UP,
1601 SRV_STATS_STATE_UP_GOING_DOWN,
1602 SRV_STATS_STATE_UP,
1603 SRV_STATS_STATE_NOLB_GOING_DOWN,
1604 SRV_STATS_STATE_NOLB,
1605 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1606 SRV_STATS_STATE_DRAIN,
1607 SRV_STATS_STATE_DRAIN_AGENT,
1608 SRV_STATS_STATE_NO_CHECK,
1609
1610 SRV_STATS_STATE_COUNT, /* Must be last */
1611};
1612
1613static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1614 [SRV_STATS_STATE_DOWN] = "DOWN",
1615 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1616 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1617 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1618 [SRV_STATS_STATE_UP] = "UP",
1619 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1620 [SRV_STATS_STATE_NOLB] = "NOLB",
1621 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1622 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1623 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1624 [SRV_STATS_STATE_NO_CHECK] = "no check"
1625};
1626
1627/* Fill <stats> with the server statistics. <stats> is
1628 * preallocated array of length <len>. The length of the array
1629 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1630 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001631 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001632 */
1633int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1634 struct field *stats, int len)
1635{
1636 struct server *via, *ref;
1637 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001638 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001639 enum srv_stats_state state;
1640 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001641 long long srv_samples_counter;
1642 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001643
1644 if (len < ST_F_TOTAL_FIELDS)
1645 return 0;
1646
1647 memset(stats, 0, sizeof(*stats) * len);
1648
1649 /* we have "via" which is the tracked server as described in the configuration,
1650 * and "ref" which is the checked server and the end of the chain.
1651 */
1652 via = sv->track ? sv->track : sv;
1653 ref = via;
1654 while (ref->track)
1655 ref = ref->track;
1656
Emeric Brun52a91d32017-08-31 14:41:55 +02001657 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001658 if ((ref->check.state & CHK_ST_ENABLED) &&
1659 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1660 state = SRV_STATS_STATE_UP_GOING_DOWN;
1661 } else {
1662 state = SRV_STATS_STATE_UP;
1663 }
1664
Emeric Brun52a91d32017-08-31 14:41:55 +02001665 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001666 if (ref->agent.state & CHK_ST_ENABLED)
1667 state = SRV_STATS_STATE_DRAIN_AGENT;
1668 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1669 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1670 else
1671 state = SRV_STATS_STATE_DRAIN;
1672 }
1673
1674 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1675 state = SRV_STATS_STATE_NO_CHECK;
1676 }
1677 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001678 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001679 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1680 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1681 state = SRV_STATS_STATE_NOLB;
1682 } else {
1683 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1684 }
1685 }
1686 else { /* stopped */
1687 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1688 state = SRV_STATS_STATE_DOWN_AGENT;
1689 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1690 state = SRV_STATS_STATE_DOWN; /* DOWN */
1691 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1692 state = SRV_STATS_STATE_GOING_UP;
1693 } else {
1694 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1695 }
1696 }
1697
1698 chunk_reset(out);
1699
1700 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1701 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1702 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1703 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1704 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1705 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1706 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1707
1708 if (sv->maxconn)
1709 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1710
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001711 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1712 if (sv->max_idle_conns != -1)
1713 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1714
William Lallemand74c24fb2016-11-21 17:18:36 +01001715 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1716 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1717 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001718 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001719 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1720 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1721 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1722 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001723 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001724 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001725 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1726 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001727
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001728 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1729 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1730 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001731 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001732
William Lallemand74c24fb2016-11-21 17:18:36 +01001733 /* status */
1734 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001735 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001736 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001737 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001738 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001739 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001740 chunk_appendf(out, "MAINT");
1741 else
1742 chunk_appendf(out,
1743 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001744 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1745 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001746
1747 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1748 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001749 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 +01001750 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1751 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1752
1753 /* check failures: unique, fatal; last change, total downtime */
1754 if (sv->check.state & CHK_ST_ENABLED) {
1755 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1756 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1757 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1758 }
1759
1760 if (sv->maxqueue)
1761 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1762
1763 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1764 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1765 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1766
Emeric Brun52a91d32017-08-31 14:41:55 +02001767 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001768 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1769
1770 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1771
1772 if (sv->track) {
1773 char *fld_track = chunk_newstr(out);
1774
1775 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1776 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1777 }
1778
1779 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1780 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1781 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1782
1783 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1784 const char *fld_chksts;
1785
1786 fld_chksts = chunk_newstr(out);
1787 chunk_strcat(out, "* "); // for check in progress
1788 chunk_strcat(out, get_check_status_info(sv->check.status));
1789 if (!(sv->check.state & CHK_ST_INPROGRESS))
1790 fld_chksts += 2; // skip "* "
1791 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1792
1793 if (sv->check.status >= HCHK_STATUS_L57DATA)
1794 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1795
1796 if (sv->check.status >= HCHK_STATUS_CHECKED)
1797 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1798
1799 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1800 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1801 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1802 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1803 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1804 }
1805
1806 if ((sv->agent.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->agent.status));
1812 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1813 fld_chksts += 2; // skip "* "
1814 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1815
1816 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1817 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1818
1819 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1820 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1821
1822 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1823 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1824 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1825 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1826 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1827 }
1828
1829 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1830 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001831 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001832 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1833 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1834 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1835 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1836 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1837 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1838 }
1839
1840 if (ref->observe)
1841 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1842
1843 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1844 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1845 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1846
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001847 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1848 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1849 srv_samples_window = srv_samples_counter;
1850
1851 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1852 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1853 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1854 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001855
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001856 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1857 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1858 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1859 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1860
Willy Tarreau708c4162019-10-09 10:19:16 +02001861 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001862 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1863 case AF_INET:
1864 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001865 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001866 break;
1867 case AF_INET6:
1868 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001869 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001870 break;
1871 case AF_UNIX:
1872 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1873 break;
1874 case -1:
1875 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1876 chunk_strcat(out, strerror(errno));
1877 break;
1878 default: /* address family not supported */
1879 break;
1880 }
1881
1882 if (sv->cookie)
1883 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1884 }
1885
1886 return 1;
1887}
1888
1889/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001890 * from stream interface <si>, and server state <state>. The caller is
1891 * responsible for clearing the trash if needed. Returns non-zero if it emits
1892 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001893 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001894static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001895{
1896 struct appctx *appctx = __objt_appctx(si->end);
1897
Willy Tarreau43241ff2019-10-09 11:27:51 +02001898 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001899 return 0;
1900
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001901 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001902}
1903
1904/* Fill <stats> with the backend statistics. <stats> is
1905 * preallocated array of length <len>. The length of the array
1906 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1907 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001908 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001909 */
1910int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1911{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001912 long long be_samples_counter;
1913 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1914
William Lallemand74c24fb2016-11-21 17:18:36 +01001915 if (len < ST_F_TOTAL_FIELDS)
1916 return 0;
1917
1918 memset(stats, 0, sizeof(*stats) * len);
1919
1920 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1921 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1922 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1923 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1924 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001925 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001926 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1927 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1928 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1929 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1930 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1931 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1932 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1933 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1934 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1935 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1936 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001937 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001938 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001939 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1940 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001941 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1942 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1943 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1944 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1945 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1946 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1947 if (px->srv)
1948 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1949
1950 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1951 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1952 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1953 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1954 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1955 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1956 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1957
Willy Tarreau708c4162019-10-09 10:19:16 +02001958 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001959 if (px->cookie_name)
1960 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1961 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1962 }
1963
1964 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1965 if (px->mode == PR_MODE_HTTP) {
1966 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1967 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1968 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1969 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1970 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1971 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1972 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001973 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1974 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001975 }
1976
1977 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1978 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1979
1980 /* compression: in, out, bypassed, responses */
1981 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1982 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1983 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1984 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1985 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1986
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001987 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1988 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1989 be_samples_window = be_samples_counter;
1990
1991 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1992 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1993 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1994 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 +01001995
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001996 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1997 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1998 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1999 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2000
William Lallemand74c24fb2016-11-21 17:18:36 +01002001 return 1;
2002}
2003
2004/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002005 * interface <si>. The caller is responsible for clearing the trash if needed.
2006 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002007 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002008static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002009{
2010 struct appctx *appctx = __objt_appctx(si->end);
2011
2012 if (!(px->cap & PR_CAP_BE))
2013 return 0;
2014
2015 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2016 return 0;
2017
Willy Tarreau43241ff2019-10-09 11:27:51 +02002018 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002019 return 0;
2020
Amaury Denoyelle97323c92020-10-02 18:32:01 +02002021 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002022}
2023
2024/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2025 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2026 * for clearing the trash if needed.
2027 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002028static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002029{
2030 struct appctx *appctx = __objt_appctx(si->end);
2031 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2032
2033 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2034 /* A form to enable/disable this proxy servers */
2035
2036 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2037 scope_txt[0] = 0;
2038 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002039 const char *scope_ptr = stats_scope_ptr(appctx, si);
2040
William Lallemand74c24fb2016-11-21 17:18:36 +01002041 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002042 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002043 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2044 }
2045
2046 chunk_appendf(&trash,
2047 "<form method=\"post\">");
2048 }
2049
2050 /* print a new table */
2051 chunk_appendf(&trash,
2052 "<table class=\"tbl\" width=\"100%%\">\n"
2053 "<tr class=\"titre\">"
2054 "<th class=\"pxname\" width=\"10%%\">");
2055
2056 chunk_appendf(&trash,
2057 "<a name=\"%s\"></a>%s"
2058 "<a class=px href=\"#%s\">%s</a>",
2059 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002060 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002061 px->id, px->id);
2062
Willy Tarreau676c29e2019-10-09 10:50:01 +02002063 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002064 /* cap, mode, id */
2065 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2066 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2067 px->uuid);
2068 chunk_appendf(&trash, "</div>");
2069 }
2070
2071 chunk_appendf(&trash,
2072 "%s</th>"
2073 "<th class=\"%s\" width=\"90%%\">%s</th>"
2074 "</tr>\n"
2075 "</table>\n"
2076 "<table class=\"tbl\" width=\"100%%\">\n"
2077 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002078 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002079 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2080
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002081 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002082 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002083 if ((px->cap & PR_CAP_BE) && px->srv)
2084 chunk_appendf(&trash,
2085 "<th rowspan=2 width=1><input type=\"checkbox\" "
2086 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2087 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2088 px->id,
2089 px->id);
2090 else
2091 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002092 }
2093
2094 chunk_appendf(&trash,
2095 "<th rowspan=2></th>"
2096 "<th colspan=3>Queue</th>"
2097 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2098 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2099 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2100 "<th colspan=9>Server</th>"
2101 "</tr>\n"
2102 "<tr class=\"titre\">"
2103 "<th>Cur</th><th>Max</th><th>Limit</th>"
2104 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2105 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2106 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2107 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2108 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2109 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2110 "<th>Thrtle</th>\n"
2111 "</tr>");
2112}
2113
2114/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2115 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2116 */
2117static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2118{
2119 struct appctx *appctx = __objt_appctx(si->end);
2120 chunk_appendf(&trash, "</table>");
2121
2122 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2123 /* close the form used to enable/disable this proxy servers */
2124 chunk_appendf(&trash,
2125 "Choose the action to perform on the checked servers : "
2126 "<select name=action>"
2127 "<option value=\"\"></option>"
2128 "<option value=\"ready\">Set state to READY</option>"
2129 "<option value=\"drain\">Set state to DRAIN</option>"
2130 "<option value=\"maint\">Set state to MAINT</option>"
2131 "<option value=\"dhlth\">Health: disable checks</option>"
2132 "<option value=\"ehlth\">Health: enable checks</option>"
2133 "<option value=\"hrunn\">Health: force UP</option>"
2134 "<option value=\"hnolb\">Health: force NOLB</option>"
2135 "<option value=\"hdown\">Health: force DOWN</option>"
2136 "<option value=\"dagent\">Agent: disable checks</option>"
2137 "<option value=\"eagent\">Agent: enable checks</option>"
2138 "<option value=\"arunn\">Agent: force UP</option>"
2139 "<option value=\"adown\">Agent: force DOWN</option>"
2140 "<option value=\"shutdown\">Kill Sessions</option>"
2141 "</select>"
2142 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2143 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2144 "</form>",
2145 px->uuid);
2146 }
2147
2148 chunk_appendf(&trash, "<p>\n");
2149}
2150
2151/*
2152 * Dumps statistics for a proxy. The output is sent to the stream interface's
2153 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2154 * buffer space, or non-zero if everything completed. This function is used
2155 * both by the CLI and the HTTP entry points, and is able to dump the output
2156 * in HTML or CSV formats. If the later, <uri> must be NULL.
2157 */
Christopher Fauletef779222018-10-31 08:47:01 +01002158int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2159 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002160{
2161 struct appctx *appctx = __objt_appctx(si->end);
2162 struct stream *s = si_strm(si);
2163 struct channel *rep = si_ic(si);
2164 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2165 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002166
2167 chunk_reset(&trash);
2168
2169 switch (appctx->ctx.stats.px_st) {
2170 case STAT_PX_ST_INIT:
2171 /* we are on a new proxy */
2172 if (uri && uri->scope) {
2173 /* we have a limited scope, we have to check the proxy name */
2174 struct stat_scope *scope;
2175 int len;
2176
2177 len = strlen(px->id);
2178 scope = uri->scope;
2179
2180 while (scope) {
2181 /* match exact proxy name */
2182 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2183 break;
2184
2185 /* match '.' which means 'self' proxy */
2186 if (!strcmp(scope->px_id, ".") && px == s->be)
2187 break;
2188 scope = scope->next;
2189 }
2190
2191 /* proxy name not found : don't dump anything */
2192 if (scope == NULL)
2193 return 1;
2194 }
2195
2196 /* if the user has requested a limited output and the proxy
2197 * name does not match, skip it.
2198 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002199 if (appctx->ctx.stats.scope_len) {
2200 const char *scope_ptr = stats_scope_ptr(appctx, si);
2201
2202 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2203 return 1;
2204 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002205
2206 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2207 (appctx->ctx.stats.iid != -1) &&
2208 (px->uuid != appctx->ctx.stats.iid))
2209 return 1;
2210
2211 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2212 /* fall through */
2213
2214 case STAT_PX_ST_TH:
2215 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002216 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002217 if (!stats_putchk(rep, htx, &trash))
2218 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002219 }
2220
2221 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2222 /* fall through */
2223
2224 case STAT_PX_ST_FE:
2225 /* print the frontend */
2226 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002227 if (!stats_putchk(rep, htx, &trash))
2228 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002229 }
2230
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002231 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01002232 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2233 /* fall through */
2234
2235 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002236 /* obj2 points to listeners list as initialized above */
2237 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002238 if (htx) {
2239 if (htx_almost_full(htx))
2240 goto full;
2241 }
2242 else {
2243 if (buffer_almost_full(&rep->buf))
2244 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002245 }
2246
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002247 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01002248 if (!l->counters)
2249 continue;
2250
2251 if (appctx->ctx.stats.flags & STAT_BOUND) {
2252 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2253 break;
2254
2255 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2256 continue;
2257 }
2258
2259 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002260 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002261 if (!stats_putchk(rep, htx, &trash))
2262 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002263 }
2264 }
2265
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002266 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01002267 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2268 /* fall through */
2269
2270 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002271 /* obj2 points to servers list as initialized above */
2272 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002273 if (htx) {
2274 if (htx_almost_full(htx))
2275 goto full;
2276 }
2277 else {
2278 if (buffer_almost_full(&rep->buf))
2279 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002280 }
2281
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002282 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01002283
2284 if (appctx->ctx.stats.flags & STAT_BOUND) {
2285 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2286 break;
2287
2288 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2289 continue;
2290 }
2291
2292 svs = sv;
2293 while (svs->track)
2294 svs = svs->track;
2295
2296 /* do not report servers which are DOWN and not changing state */
2297 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002298 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2299 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002300 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2301 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2302 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2303 continue;
2304 }
2305
Willy Tarreau43241ff2019-10-09 11:27:51 +02002306 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002307 if (!stats_putchk(rep, htx, &trash))
2308 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002309 }
2310 } /* for sv */
2311
2312 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2313 /* fall through */
2314
2315 case STAT_PX_ST_BE:
2316 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002317 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002318 if (!stats_putchk(rep, htx, &trash))
2319 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002320 }
2321
2322 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2323 /* fall through */
2324
2325 case STAT_PX_ST_END:
2326 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2327 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002328 if (!stats_putchk(rep, htx, &trash))
2329 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002330 }
2331
2332 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2333 /* fall through */
2334
2335 case STAT_PX_ST_FIN:
2336 return 1;
2337
2338 default:
2339 /* unknown state, we should put an abort() here ! */
2340 return 1;
2341 }
Christopher Fauletef779222018-10-31 08:47:01 +01002342
2343 full:
2344 si_rx_room_blk(si);
2345 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002346}
2347
2348/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2349 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2350 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002351static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002352{
2353 /* WARNING! This must fit in the first buffer !!! */
2354 chunk_appendf(&trash,
2355 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2356 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2357 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03002358 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002359 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2360 "<style type=\"text/css\"><!--\n"
2361 "body {"
2362 " font-family: arial, helvetica, sans-serif;"
2363 " font-size: 12px;"
2364 " font-weight: normal;"
2365 " color: black;"
2366 " background: white;"
2367 "}\n"
2368 "th,td {"
2369 " font-size: 10px;"
2370 "}\n"
2371 "h1 {"
2372 " font-size: x-large;"
2373 " margin-bottom: 0.5em;"
2374 "}\n"
2375 "h2 {"
2376 " font-family: helvetica, arial;"
2377 " font-size: x-large;"
2378 " font-weight: bold;"
2379 " font-style: italic;"
2380 " color: #6020a0;"
2381 " margin-top: 0em;"
2382 " margin-bottom: 0em;"
2383 "}\n"
2384 "h3 {"
2385 " font-family: helvetica, arial;"
2386 " font-size: 16px;"
2387 " font-weight: bold;"
2388 " color: #b00040;"
2389 " background: #e8e8d0;"
2390 " margin-top: 0em;"
2391 " margin-bottom: 0em;"
2392 "}\n"
2393 "li {"
2394 " margin-top: 0.25em;"
2395 " margin-right: 2em;"
2396 "}\n"
2397 ".hr {margin-top: 0.25em;"
2398 " border-color: black;"
2399 " border-bottom-style: solid;"
2400 "}\n"
2401 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2402 ".total {background: #20D0D0;color: #ffff80;}\n"
2403 ".frontend {background: #e8e8d0;}\n"
2404 ".socket {background: #d0d0d0;}\n"
2405 ".backend {background: #e8e8d0;}\n"
2406 ".active_down {background: #ff9090;}\n"
2407 ".active_going_up {background: #ffd020;}\n"
2408 ".active_going_down {background: #ffffa0;}\n"
2409 ".active_up {background: #c0ffc0;}\n"
2410 ".active_nolb {background: #20a0ff;}\n"
2411 ".active_draining {background: #20a0FF;}\n"
2412 ".active_no_check {background: #e0e0e0;}\n"
2413 ".backup_down {background: #ff9090;}\n"
2414 ".backup_going_up {background: #ff80ff;}\n"
2415 ".backup_going_down {background: #c060ff;}\n"
2416 ".backup_up {background: #b0d0ff;}\n"
2417 ".backup_nolb {background: #90b0e0;}\n"
2418 ".backup_draining {background: #cc9900;}\n"
2419 ".backup_no_check {background: #e0e0e0;}\n"
2420 ".maintain {background: #c07820;}\n"
2421 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2422 "\n"
2423 "a.px:link {color: #ffff40; text-decoration: none;}"
2424 "a.px:visited {color: #ffff40; text-decoration: none;}"
2425 "a.px:hover {color: #ffffff; text-decoration: none;}"
2426 "a.lfsb:link {color: #000000; text-decoration: none;}"
2427 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2428 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2429 "\n"
2430 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2431 "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"
2432 "table.tbl td.ac { text-align: center;}\n"
2433 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2434 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2435 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2436 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2437 "\n"
2438 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2439 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2440 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2441 "table.det { border-collapse: collapse; border-style: none; }\n"
2442 "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"
2443 "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"
2444 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2445 "div.tips {\n"
2446 " display:block;\n"
2447 " visibility:hidden;\n"
2448 " z-index:2147483647;\n"
2449 " position:absolute;\n"
2450 " padding:2px 4px 3px;\n"
2451 " background:#f0f060; color:#000000;\n"
2452 " border:1px solid #7040c0;\n"
2453 " white-space:nowrap;\n"
2454 " font-style:normal;font-size:11px;font-weight:normal;\n"
2455 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2456 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2457 "}\n"
2458 "u:hover div.tips {visibility:visible;}\n"
2459 "-->\n"
2460 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002461 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002462 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002463 );
2464}
2465
2466/* Dumps the HTML stats information block to the trash for and uses the state from
2467 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2468 * for clearing the trash if needed.
2469 */
2470static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2471{
2472 struct appctx *appctx = __objt_appctx(si->end);
2473 unsigned int up = (now.tv_sec - start_date.tv_sec);
2474 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002475 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002476 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2477
2478 /* Turn the bytes per second to bits per second and take care of the
2479 * usual ethernet overhead in order to help figure how far we are from
2480 * interface saturation since it's the only case which usually matters.
2481 * For this we count the total size of an Ethernet frame on the wire
2482 * including preamble and IFG (1538) for the largest TCP segment it
2483 * transports (1448 with TCP timestamps). This is not valid for smaller
2484 * packets (under-estimated), but it gives a reasonably accurate
2485 * estimation of how far we are from uplink saturation.
2486 */
2487 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002488
2489 /* WARNING! this has to fit the first packet too.
2490 * We are around 3.5 kB, add adding entries will
2491 * become tricky if we want to support 4kB buffers !
2492 */
2493 chunk_appendf(&trash,
2494 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2495 PRODUCT_NAME "%s</a></h1>\n"
2496 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2497 "<hr width=\"100%%\" class=\"hr\">\n"
2498 "<h3>&gt; General process information</h3>\n"
2499 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002500 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002501 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2502 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2503 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002504 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002505 "Running tasks: %d/%d; idle = %d %%<br>\n"
2506 "</td><td align=\"center\" nowrap>\n"
2507 "<table class=\"lgd\"><tr>\n"
2508 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2509 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2510 "</tr><tr>\n"
2511 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2512 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2513 "</tr><tr>\n"
2514 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2515 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2516 "</tr><tr>\n"
2517 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2518 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2519 "</tr><tr>\n"
2520 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2521 "</tr><tr>\n"
2522 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2523 "</tr></table>\n"
2524 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2525 "</td>"
2526 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2527 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2528 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002529 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2530 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2531 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2532 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2533 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002534 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002535 up / 86400, (up % 86400) / 3600,
2536 (up % 3600) / 60, (up % 60),
2537 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2538 global.rlimit_memmax ? " MB" : "",
2539 global.rlimit_nofile,
2540 global.maxsock, global.maxconn, global.maxpipes,
2541 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002542 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2543 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002544 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002545 );
2546
2547 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002548 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002549 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2550
2551 chunk_appendf(&trash,
2552 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2553 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2554 STAT_SCOPE_TXT_MAXLEN);
2555
2556 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2557 scope_txt[0] = 0;
2558 if (appctx->ctx.stats.scope_len) {
2559 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002560 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002561 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2562 }
2563
2564 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2565 chunk_appendf(&trash,
2566 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2567 uri->uri_prefix,
2568 "",
2569 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2570 scope_txt);
2571 else
2572 chunk_appendf(&trash,
2573 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2574 uri->uri_prefix,
2575 ";up",
2576 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2577 scope_txt);
2578
2579 if (uri->refresh > 0) {
2580 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2581 chunk_appendf(&trash,
2582 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2583 uri->uri_prefix,
2584 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2585 "",
2586 scope_txt);
2587 else
2588 chunk_appendf(&trash,
2589 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2590 uri->uri_prefix,
2591 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2592 ";norefresh",
2593 scope_txt);
2594 }
2595
2596 chunk_appendf(&trash,
2597 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2598 uri->uri_prefix,
2599 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2600 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2601 scope_txt);
2602
2603 chunk_appendf(&trash,
2604 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2605 uri->uri_prefix,
2606 (uri->refresh > 0) ? ";norefresh" : "",
2607 scope_txt);
2608
2609 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002610 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2611 uri->uri_prefix,
2612 (uri->refresh > 0) ? ";norefresh" : "",
2613 scope_txt, uri->uri_prefix);
2614
2615 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002616 "</ul></td>"
2617 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2618 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2619 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2620 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2621 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2622 "</ul>"
2623 "</td>"
2624 "</tr></table>\n"
2625 ""
2626 );
2627
2628 if (appctx->ctx.stats.st_code) {
2629 switch (appctx->ctx.stats.st_code) {
2630 case STAT_STATUS_DONE:
2631 chunk_appendf(&trash,
2632 "<p><div class=active_up>"
2633 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2634 "Action processed successfully."
2635 "</div>\n", uri->uri_prefix,
2636 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2637 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2638 scope_txt);
2639 break;
2640 case STAT_STATUS_NONE:
2641 chunk_appendf(&trash,
2642 "<p><div class=active_going_down>"
2643 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2644 "Nothing has changed."
2645 "</div>\n", uri->uri_prefix,
2646 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2647 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2648 scope_txt);
2649 break;
2650 case STAT_STATUS_PART:
2651 chunk_appendf(&trash,
2652 "<p><div class=active_going_down>"
2653 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2654 "Action partially processed.<br>"
2655 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2656 "</div>\n", uri->uri_prefix,
2657 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2658 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2659 scope_txt);
2660 break;
2661 case STAT_STATUS_ERRP:
2662 chunk_appendf(&trash,
2663 "<p><div class=active_down>"
2664 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2665 "Action not processed because of invalid parameters."
2666 "<ul>"
2667 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002668 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002669 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2670 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2671 "</ul>"
2672 "</div>\n", uri->uri_prefix,
2673 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2674 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2675 scope_txt);
2676 break;
2677 case STAT_STATUS_EXCD:
2678 chunk_appendf(&trash,
2679 "<p><div class=active_down>"
2680 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2681 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2682 "You should retry with less servers at a time.</b>"
2683 "</div>\n", uri->uri_prefix,
2684 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2685 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2686 scope_txt);
2687 break;
2688 case STAT_STATUS_DENY:
2689 chunk_appendf(&trash,
2690 "<p><div class=active_down>"
2691 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2692 "<b>Action denied.</b>"
2693 "</div>\n", uri->uri_prefix,
2694 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2695 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2696 scope_txt);
2697 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002698 case STAT_STATUS_IVAL:
2699 chunk_appendf(&trash,
2700 "<p><div class=active_down>"
2701 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2702 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2703 "</div>\n", uri->uri_prefix,
2704 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2705 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2706 scope_txt);
2707 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002708 default:
2709 chunk_appendf(&trash,
2710 "<p><div class=active_no_check>"
2711 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2712 "Unexpected result."
2713 "</div>\n", uri->uri_prefix,
2714 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2715 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2716 scope_txt);
2717 }
2718 chunk_appendf(&trash, "<p>\n");
2719 }
2720}
2721
2722/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2723 * for clearing the trash if needed.
2724 */
2725static void stats_dump_html_end()
2726{
2727 chunk_appendf(&trash, "</body></html>\n");
2728}
2729
Simon Horman05ee2132017-01-04 09:37:25 +01002730/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2731 * for clearing it if needed.
2732 */
2733static void stats_dump_json_header()
2734{
2735 chunk_strcat(&trash, "[");
2736}
2737
2738
2739/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2740 * for clearing the trash if needed.
2741 */
2742static void stats_dump_json_end()
2743{
2744 chunk_strcat(&trash, "]");
2745}
2746
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002747/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
2748 * a pointer to the current server/listener.
2749 */
2750static int stats_dump_proxies(struct stream_interface *si,
2751 struct htx *htx,
2752 struct uri_auth *uri)
2753{
2754 struct appctx *appctx = __objt_appctx(si->end);
2755 struct channel *rep = si_ic(si);
2756 struct proxy *px;
2757
2758 /* dump proxies */
2759 while (appctx->ctx.stats.obj1) {
2760 if (htx) {
2761 if (htx_almost_full(htx))
2762 goto full;
2763 }
2764 else {
2765 if (buffer_almost_full(&rep->buf))
2766 goto full;
2767 }
2768
2769 px = appctx->ctx.stats.obj1;
2770 /* skip the disabled proxies, global frontend and non-networked ones */
2771 if (px->state != PR_STSTOPPED && px->uuid > 0
2772 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
2773
2774 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
2775 return 0;
2776 }
2777
2778 appctx->ctx.stats.obj1 = px->next;
2779 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2780 }
2781
2782 return 1;
2783
2784 full:
2785 si_rx_room_blk(si);
2786 return 0;
2787}
2788
William Lallemand74c24fb2016-11-21 17:18:36 +01002789/* This function dumps statistics onto the stream interface's read buffer in
2790 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2791 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2792 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2793 * and the stream must be closed, or -1 in case of any error. This function is
2794 * used by both the CLI and the HTTP handlers.
2795 */
Christopher Fauletef779222018-10-31 08:47:01 +01002796static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2797 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002798{
2799 struct appctx *appctx = __objt_appctx(si->end);
2800 struct channel *rep = si_ic(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002801
2802 chunk_reset(&trash);
2803
2804 switch (appctx->st2) {
2805 case STAT_ST_INIT:
2806 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2807 /* fall through */
2808
2809 case STAT_ST_HEAD:
2810 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002811 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002812 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2813 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002814 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002815 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002816 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2817 stats_dump_csv_header();
2818
Christopher Fauletef779222018-10-31 08:47:01 +01002819 if (!stats_putchk(rep, htx, &trash))
2820 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002821
Christopher Faulet6338a082019-09-09 15:50:54 +02002822 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2823 appctx->st2 = STAT_ST_FIN;
2824 return 1;
2825 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002826 appctx->st2 = STAT_ST_INFO;
2827 /* fall through */
2828
2829 case STAT_ST_INFO:
2830 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2831 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002832 if (!stats_putchk(rep, htx, &trash))
2833 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002834 }
2835
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002836 appctx->ctx.stats.obj1 = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002837 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2838 appctx->st2 = STAT_ST_LIST;
2839 /* fall through */
2840
2841 case STAT_ST_LIST:
2842 /* dump proxies */
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002843 if (!stats_dump_proxies(si, htx, uri))
2844 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002845
2846 appctx->st2 = STAT_ST_END;
2847 /* fall through */
2848
2849 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002850 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2851 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2852 stats_dump_html_end();
2853 else
2854 stats_dump_json_end();
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
2859 appctx->st2 = STAT_ST_FIN;
2860 /* fall through */
2861
2862 case STAT_ST_FIN:
2863 return 1;
2864
2865 default:
2866 /* unknown state ! */
2867 appctx->st2 = STAT_ST_FIN;
2868 return -1;
2869 }
Christopher Fauletef779222018-10-31 08:47:01 +01002870
2871 full:
2872 si_rx_room_blk(si);
2873 return 0;
2874
William Lallemand74c24fb2016-11-21 17:18:36 +01002875}
2876
2877/* We reached the stats page through a POST request. The appctx is
2878 * expected to have already been allocated by the caller.
2879 * Parse the posted data and enable/disable servers if necessary.
2880 * Returns 1 if request was parsed or zero if it needs more data.
2881 */
2882static int stats_process_http_post(struct stream_interface *si)
2883{
2884 struct stream *s = si_strm(si);
2885 struct appctx *appctx = objt_appctx(si->end);
2886
2887 struct proxy *px = NULL;
2888 struct server *sv = NULL;
2889
2890 char key[LINESIZE];
2891 int action = ST_ADM_ACTION_NONE;
2892 int reprocess = 0;
2893
2894 int total_servers = 0;
2895 int altered_servers = 0;
2896
2897 char *first_param, *cur_param, *next_param, *end_params;
2898 char *st_cur_param = NULL;
2899 char *st_next_param = NULL;
2900
Christopher Fauleta3618372018-12-13 21:59:56 +01002901 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002902
Christopher Fauletb7f88902019-07-15 21:56:43 +02002903 struct htx *htx = htxbuf(&s->req.buf);
2904 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002905
Christopher Fauletb7f88902019-07-15 21:56:43 +02002906 /* we need more data */
2907 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2908 /* check if we can receive more */
2909 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2910 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2911 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002912 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002913 goto wait;
2914 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002915
Christopher Fauletb7f88902019-07-15 21:56:43 +02002916 /* The request was fully received. Copy data */
2917 blk = htx_get_head_blk(htx);
2918 while (blk) {
2919 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002920
Christopher Fauletb7f88902019-07-15 21:56:43 +02002921 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2922 break;
2923 if (type == HTX_BLK_DATA) {
2924 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002925
Christopher Fauletb7f88902019-07-15 21:56:43 +02002926 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002927 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2928 goto out;
2929 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002930 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002931 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002932 }
2933
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002934 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002935 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002936 cur_param = next_param = end_params;
2937 *end_params = '\0';
2938
2939 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2940
2941 /*
2942 * Parse the parameters in reverse order to only store the last value.
2943 * From the html form, the backend and the action are at the end.
2944 */
2945 while (cur_param > first_param) {
2946 char *value;
2947 int poffset, plen;
2948
2949 cur_param--;
2950
2951 if ((*cur_param == '&') || (cur_param == first_param)) {
2952 reprocess_servers:
2953 /* Parse the key */
2954 poffset = (cur_param != first_param ? 1 : 0);
2955 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2956 if ((plen > 0) && (plen <= sizeof(key))) {
2957 strncpy(key, cur_param + poffset, plen);
2958 key[plen - 1] = '\0';
2959 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002960 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002961 goto out;
2962 }
2963
2964 /* Parse the value */
2965 value = key;
2966 while (*value != '\0' && *value != '=') {
2967 value++;
2968 }
2969 if (*value == '=') {
2970 /* Ok, a value is found, we can mark the end of the key */
2971 *value++ = '\0';
2972 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002973 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002974 break;
2975
2976 /* Now we can check the key to see what to do */
2977 if (!px && (strcmp(key, "b") == 0)) {
2978 if ((px = proxy_be_by_name(value)) == NULL) {
2979 /* the backend name is unknown or ambiguous (duplicate names) */
2980 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2981 goto out;
2982 }
2983 }
2984 else if (!action && (strcmp(key, "action") == 0)) {
2985 if (strcmp(value, "ready") == 0) {
2986 action = ST_ADM_ACTION_READY;
2987 }
2988 else if (strcmp(value, "drain") == 0) {
2989 action = ST_ADM_ACTION_DRAIN;
2990 }
2991 else if (strcmp(value, "maint") == 0) {
2992 action = ST_ADM_ACTION_MAINT;
2993 }
2994 else if (strcmp(value, "shutdown") == 0) {
2995 action = ST_ADM_ACTION_SHUTDOWN;
2996 }
2997 else if (strcmp(value, "dhlth") == 0) {
2998 action = ST_ADM_ACTION_DHLTH;
2999 }
3000 else if (strcmp(value, "ehlth") == 0) {
3001 action = ST_ADM_ACTION_EHLTH;
3002 }
3003 else if (strcmp(value, "hrunn") == 0) {
3004 action = ST_ADM_ACTION_HRUNN;
3005 }
3006 else if (strcmp(value, "hnolb") == 0) {
3007 action = ST_ADM_ACTION_HNOLB;
3008 }
3009 else if (strcmp(value, "hdown") == 0) {
3010 action = ST_ADM_ACTION_HDOWN;
3011 }
3012 else if (strcmp(value, "dagent") == 0) {
3013 action = ST_ADM_ACTION_DAGENT;
3014 }
3015 else if (strcmp(value, "eagent") == 0) {
3016 action = ST_ADM_ACTION_EAGENT;
3017 }
3018 else if (strcmp(value, "arunn") == 0) {
3019 action = ST_ADM_ACTION_ARUNN;
3020 }
3021 else if (strcmp(value, "adown") == 0) {
3022 action = ST_ADM_ACTION_ADOWN;
3023 }
3024 /* else these are the old supported methods */
3025 else if (strcmp(value, "disable") == 0) {
3026 action = ST_ADM_ACTION_DISABLE;
3027 }
3028 else if (strcmp(value, "enable") == 0) {
3029 action = ST_ADM_ACTION_ENABLE;
3030 }
3031 else if (strcmp(value, "stop") == 0) {
3032 action = ST_ADM_ACTION_STOP;
3033 }
3034 else if (strcmp(value, "start") == 0) {
3035 action = ST_ADM_ACTION_START;
3036 }
3037 else {
3038 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3039 goto out;
3040 }
3041 }
3042 else if (strcmp(key, "s") == 0) {
3043 if (!(px && action)) {
3044 /*
3045 * Indicates that we'll need to reprocess the parameters
3046 * as soon as backend and action are known
3047 */
3048 if (!reprocess) {
3049 st_cur_param = cur_param;
3050 st_next_param = next_param;
3051 }
3052 reprocess = 1;
3053 }
3054 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003055 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003056 switch (action) {
3057 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003058 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003059 altered_servers++;
3060 total_servers++;
3061 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3062 }
3063 break;
3064 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003065 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003066 altered_servers++;
3067 total_servers++;
3068 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3069 }
3070 break;
3071 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003072 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003073 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3074 altered_servers++;
3075 total_servers++;
3076 }
3077 break;
3078 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003079 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003080 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3081 altered_servers++;
3082 total_servers++;
3083 }
3084 break;
3085 case ST_ADM_ACTION_DHLTH:
3086 if (sv->check.state & CHK_ST_CONFIGURED) {
3087 sv->check.state &= ~CHK_ST_ENABLED;
3088 altered_servers++;
3089 total_servers++;
3090 }
3091 break;
3092 case ST_ADM_ACTION_EHLTH:
3093 if (sv->check.state & CHK_ST_CONFIGURED) {
3094 sv->check.state |= CHK_ST_ENABLED;
3095 altered_servers++;
3096 total_servers++;
3097 }
3098 break;
3099 case ST_ADM_ACTION_HRUNN:
3100 if (!(sv->track)) {
3101 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003102 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003103 altered_servers++;
3104 total_servers++;
3105 }
3106 break;
3107 case ST_ADM_ACTION_HNOLB:
3108 if (!(sv->track)) {
3109 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003110 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003111 altered_servers++;
3112 total_servers++;
3113 }
3114 break;
3115 case ST_ADM_ACTION_HDOWN:
3116 if (!(sv->track)) {
3117 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003118 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003119 altered_servers++;
3120 total_servers++;
3121 }
3122 break;
3123 case ST_ADM_ACTION_DAGENT:
3124 if (sv->agent.state & CHK_ST_CONFIGURED) {
3125 sv->agent.state &= ~CHK_ST_ENABLED;
3126 altered_servers++;
3127 total_servers++;
3128 }
3129 break;
3130 case ST_ADM_ACTION_EAGENT:
3131 if (sv->agent.state & CHK_ST_CONFIGURED) {
3132 sv->agent.state |= CHK_ST_ENABLED;
3133 altered_servers++;
3134 total_servers++;
3135 }
3136 break;
3137 case ST_ADM_ACTION_ARUNN:
3138 if (sv->agent.state & CHK_ST_ENABLED) {
3139 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003140 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003141 altered_servers++;
3142 total_servers++;
3143 }
3144 break;
3145 case ST_ADM_ACTION_ADOWN:
3146 if (sv->agent.state & CHK_ST_ENABLED) {
3147 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003148 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003149 altered_servers++;
3150 total_servers++;
3151 }
3152 break;
3153 case ST_ADM_ACTION_READY:
3154 srv_adm_set_ready(sv);
3155 altered_servers++;
3156 total_servers++;
3157 break;
3158 case ST_ADM_ACTION_DRAIN:
3159 srv_adm_set_drain(sv);
3160 altered_servers++;
3161 total_servers++;
3162 break;
3163 case ST_ADM_ACTION_MAINT:
3164 srv_adm_set_maint(sv);
3165 altered_servers++;
3166 total_servers++;
3167 break;
3168 case ST_ADM_ACTION_SHUTDOWN:
3169 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003170 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003171 altered_servers++;
3172 total_servers++;
3173 }
3174 break;
3175 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003176 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003177 } else {
3178 /* the server name is unknown or ambiguous (duplicate names) */
3179 total_servers++;
3180 }
3181 }
3182 if (reprocess && px && action) {
3183 /* Now, we know the backend and the action chosen by the user.
3184 * We can safely restart from the first server parameter
3185 * to reprocess them
3186 */
3187 cur_param = st_cur_param;
3188 next_param = st_next_param;
3189 reprocess = 0;
3190 goto reprocess_servers;
3191 }
3192
3193 next_param = cur_param;
3194 }
3195 }
3196
3197 if (total_servers == 0) {
3198 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3199 }
3200 else if (altered_servers == 0) {
3201 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3202 }
3203 else if (altered_servers == total_servers) {
3204 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3205 }
3206 else {
3207 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3208 }
3209 out:
3210 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003211 wait:
3212 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3213 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003214}
3215
3216
Christopher Fauletb7f88902019-07-15 21:56:43 +02003217static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003218{
3219 struct stream *s = si_strm(si);
3220 struct uri_auth *uri = s->be->uri_auth;
3221 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003222 struct htx_sl *sl;
3223 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003224
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003225 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);
3226 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3227 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003228 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003229 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003230
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003231 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003232 goto full;
3233 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3234 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3235 goto full;
3236 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003237 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3238 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3239 goto full;
3240 }
Christopher Fauletef779222018-10-31 08:47:01 +01003241 else {
3242 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3243 goto full;
3244 }
3245
3246 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3247 const char *refresh = U2A(uri->refresh);
3248 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3249 goto full;
3250 }
3251
3252 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3253 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3254 goto full;
3255 }
3256
3257 if (!htx_add_endof(htx, HTX_BLK_EOH))
3258 goto full;
3259
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003260 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003261 return 1;
3262
3263 full:
3264 htx_reset(htx);
3265 si_rx_room_blk(si);
3266 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003267}
3268
Christopher Fauletef779222018-10-31 08:47:01 +01003269
Christopher Fauletb7f88902019-07-15 21:56:43 +02003270static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003271{
3272 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3273 struct stream *s = si_strm(si);
3274 struct uri_auth *uri = s->be->uri_auth;
3275 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003276 struct htx_sl *sl;
3277 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003278
3279 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3280 scope_txt[0] = 0;
3281 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003282 const char *scope_ptr = stats_scope_ptr(appctx, si);
3283
Christopher Fauletef779222018-10-31 08:47:01 +01003284 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003285 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003286 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3287 }
3288
3289 /* We don't want to land on the posted stats page because a refresh will
3290 * repost the data. We don't want this to happen on accident so we redirect
3291 * the browse to the stats page with a GET.
3292 */
3293 chunk_printf(&trash, "%s;st=%s%s%s%s",
3294 uri->uri_prefix,
3295 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3296 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3297 stat_status_codes[appctx->ctx.stats.st_code]) ?
3298 stat_status_codes[appctx->ctx.stats.st_code] :
3299 stat_status_codes[STAT_STATUS_UNKN],
3300 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3301 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3302 scope_txt);
3303
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003304 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3305 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3306 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003307 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003308 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003309
3310 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003311 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3312 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3313 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3314 goto full;
3315
3316 if (!htx_add_endof(htx, HTX_BLK_EOH))
3317 goto full;
3318
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003319 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003320 return 1;
3321
3322full:
3323 htx_reset(htx);
3324 si_rx_room_blk(si);
3325 return 0;
3326}
William Lallemand74c24fb2016-11-21 17:18:36 +01003327
Simon Horman05ee2132017-01-04 09:37:25 +01003328
William Lallemand74c24fb2016-11-21 17:18:36 +01003329/* This I/O handler runs as an applet embedded in a stream interface. It is
3330 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3331 * appctx->st0 contains the operation in progress (dump, done). The handler
3332 * automatically unregisters itself once transfer is complete.
3333 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003334static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003335{
3336 struct stream_interface *si = appctx->owner;
3337 struct stream *s = si_strm(si);
3338 struct channel *req = si_oc(si);
3339 struct channel *res = si_ic(si);
3340 struct htx *req_htx, *res_htx;
3341
3342 res_htx = htx_from_buf(&res->buf);
3343
3344 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3345 goto out;
3346
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003347 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003348 if (!b_size(&res->buf)) {
3349 si_rx_room_blk(si);
3350 goto out;
3351 }
3352
3353 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003354 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3355 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003356
3357 /* all states are processed in sequence */
3358 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003359 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003360 if (s->txn->meth == HTTP_METH_HEAD)
3361 appctx->st0 = STAT_HTTP_DONE;
3362 else
3363 appctx->st0 = STAT_HTTP_DUMP;
3364 }
3365 }
3366
3367 if (appctx->st0 == STAT_HTTP_DUMP) {
3368 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3369 appctx->st0 = STAT_HTTP_DONE;
3370 }
3371
3372 if (appctx->st0 == STAT_HTTP_POST) {
3373 if (stats_process_http_post(si))
3374 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003375 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003376 appctx->st0 = STAT_HTTP_DONE;
3377 }
3378
3379 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003380 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003381 appctx->st0 = STAT_HTTP_DONE;
3382 }
3383
3384 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003385 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003386 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003387 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3388 si_rx_room_blk(si);
3389 goto out;
3390 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003391 channel_add_input(&s->res, 1);
3392 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003393 }
3394
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003395 if (appctx->st0 == STAT_HTTP_END) {
3396 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003397 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003398 si_shutr(si);
3399 }
3400
3401 /* eat the whole request */
3402 if (co_data(req)) {
3403 req_htx = htx_from_buf(&req->buf);
3404 co_htx_skip(req, req_htx, co_data(req));
3405 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003406 }
3407 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003408
Christopher Fauletef779222018-10-31 08:47:01 +01003409 out:
3410 /* we have left the request in the buffer for the case where we
3411 * process a POST, and this automatically re-enables activity on
3412 * read. It's better to indicate that we want to stop reading when
3413 * we're sending, so that we know there's at most one direction
3414 * deciding to wake the applet up. It saves it from looping when
3415 * emitting large blocks into small TCP windows.
3416 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003417 htx_to_buf(res_htx, &res->buf);
3418 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003419 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003420}
3421
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003422/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003423static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003424 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003425{
3426 int field;
3427
3428 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3429 if (!field_format(info, field))
3430 continue;
3431
Willy Tarreaueaa55372019-10-09 07:39:11 +02003432 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003433 return 0;
3434 if (!stats_emit_raw_data_field(out, &info[field]))
3435 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003436 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3437 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003438 if (!chunk_strcat(out, "\n"))
3439 return 0;
3440 }
3441 return 1;
3442}
3443
3444/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003445static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003446 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003447{
3448 int field;
3449
3450 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3451 if (!field_format(info, field))
3452 continue;
3453
Willy Tarreaueaa55372019-10-09 07:39:11 +02003454 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 +01003455 return 0;
3456 if (!stats_emit_field_tags(out, &info[field], ':'))
3457 return 0;
3458 if (!stats_emit_typed_data_field(out, &info[field]))
3459 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003460 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3461 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003462 if (!chunk_strcat(out, "\n"))
3463 return 0;
3464 }
3465 return 1;
3466}
3467
3468/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003469 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003470 * INF_TOTAL_FIELDS. If this length is less then this value, the
3471 * function returns 0, otherwise, it returns 1.
3472 */
3473int stats_fill_info(struct field *info, int len)
3474{
3475 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003476 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003477
3478#ifdef USE_OPENSSL
3479 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3480 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3481 int ssl_reuse = 0;
3482
3483 if (ssl_key_rate < ssl_sess_rate) {
3484 /* count the ssl reuse ratio and avoid overflows in both directions */
3485 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3486 }
3487#endif
3488
3489 if (len < INF_TOTAL_FIELDS)
3490 return 0;
3491
3492 chunk_reset(out);
3493 memset(info, 0, sizeof(*info) * len);
3494
3495 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003496 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3497 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003498
Yves Lafon95317282018-02-26 11:10:37 +01003499 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003500 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3501 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3502 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3503
3504 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3505 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3506
3507 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3508 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3509 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3510 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3511 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3512 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3513 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3514 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3515 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3516 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3517 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3518 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3519#ifdef USE_OPENSSL
3520 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3521 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3522 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3523#endif
3524 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3525 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3526 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3527 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3528 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3529 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3530 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3531 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3532 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3533
3534#ifdef USE_OPENSSL
3535 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3536 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3537 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3538 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3539 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3540 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3541 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3542 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3543 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3544 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3545#endif
3546 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3547 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3548 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3549#ifdef USE_ZLIB
3550 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3551 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3552#endif
3553 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003554 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003555 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003556 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3557 if (global.desc)
3558 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003559 info[INF_STOPPING] = mkf_u32(0, stopping);
3560 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003561 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003562 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003563 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003564 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003565 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003566 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003567 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003568 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003569 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003570 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 +02003571 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003572 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003573
3574 return 1;
3575}
3576
3577/* This function dumps information onto the stream interface's read buffer.
3578 * It returns 0 as long as it does not complete, non-zero upon completion.
3579 * No state is used.
3580 */
3581static int stats_dump_info_to_buffer(struct stream_interface *si)
3582{
3583 struct appctx *appctx = __objt_appctx(si->end);
3584
3585 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3586 return 0;
3587
3588 chunk_reset(&trash);
3589
3590 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003591 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003592 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003593 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003594 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003595 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003596
Willy Tarreau06d80a92017-10-19 14:32:15 +02003597 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003598 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003599 return 0;
3600 }
3601
3602 return 1;
3603}
3604
Simon Horman6f6bb382017-01-04 09:37:26 +01003605/* This function dumps the schema onto the stream interface's read buffer.
3606 * It returns 0 as long as it does not complete, non-zero upon completion.
3607 * No state is used.
3608 *
3609 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3610 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3611 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003612static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003613{
3614
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003615 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003616
3617 chunk_strcat(out,
3618 "{"
3619 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3620 "\"oneOf\":["
3621 "{"
3622 "\"title\":\"Info\","
3623 "\"type\":\"array\","
3624 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003625 "\"title\":\"InfoItem\","
3626 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003627 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003628 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3629 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3630 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3631 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3632 "},"
3633 "\"required\":[\"field\",\"processNum\",\"tags\","
3634 "\"value\"]"
3635 "}"
3636 "},"
3637 "{"
3638 "\"title\":\"Stat\","
3639 "\"type\":\"array\","
3640 "\"items\":{"
3641 "\"title\":\"InfoItem\","
3642 "\"type\":\"object\","
3643 "\"properties\":{"
3644 "\"objType\":{"
3645 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3646 "\"Server\",\"Unknown\"]"
3647 "},"
3648 "\"proxyId\":{"
3649 "\"type\":\"integer\","
3650 "\"minimum\":0"
3651 "},"
3652 "\"id\":{"
3653 "\"type\":\"integer\","
3654 "\"minimum\":0"
3655 "},"
3656 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3657 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3658 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3659 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3660 "},"
3661 "\"required\":[\"objType\",\"proxyId\",\"id\","
3662 "\"field\",\"processNum\",\"tags\","
3663 "\"value\"]"
3664 "}"
3665 "},"
3666 "{"
3667 "\"title\":\"Error\","
3668 "\"type\":\"object\","
3669 "\"properties\":{"
3670 "\"errorStr\":{"
3671 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003672 "}"
3673 "},"
3674 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01003675 "}"
3676 "],"
3677 "\"definitions\":{"
3678 "\"field\":{"
3679 "\"type\":\"object\","
3680 "\"pos\":{"
3681 "\"type\":\"integer\","
3682 "\"minimum\":0"
3683 "},"
3684 "\"name\":{"
3685 "\"type\":\"string\""
3686 "},"
3687 "\"required\":[\"pos\",\"name\"]"
3688 "},"
3689 "\"processNum\":{"
3690 "\"type\":\"integer\","
3691 "\"minimum\":1"
3692 "},"
3693 "\"tags\":{"
3694 "\"type\":\"object\","
3695 "\"origin\":{"
3696 "\"type\":\"string\","
3697 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3698 "\"Config\",\"Product\",\"Unknown\"]"
3699 "},"
3700 "\"nature\":{"
3701 "\"type\":\"string\","
3702 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3703 "\"Rate\",\"Counter\",\"Duration\","
3704 "\"Age\",\"Time\",\"Name\",\"Output\","
3705 "\"Avg\", \"Unknown\"]"
3706 "},"
3707 "\"scope\":{"
3708 "\"type\":\"string\","
3709 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3710 "\"System\",\"Unknown\"]"
3711 "},"
3712 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3713 "},"
3714 "\"typedValue\":{"
3715 "\"type\":\"object\","
3716 "\"oneOf\":["
3717 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3718 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3719 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3720 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3721 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3722 "],"
3723 "\"definitions\":{"
3724 "\"s32Value\":{"
3725 "\"properties\":{"
3726 "\"type\":{"
3727 "\"type\":\"string\","
3728 "\"enum\":[\"s32\"]"
3729 "},"
3730 "\"value\":{"
3731 "\"type\":\"integer\","
3732 "\"minimum\":-2147483648,"
3733 "\"maximum\":2147483647"
3734 "}"
3735 "},"
3736 "\"required\":[\"type\",\"value\"]"
3737 "},"
3738 "\"s64Value\":{"
3739 "\"properties\":{"
3740 "\"type\":{"
3741 "\"type\":\"string\","
3742 "\"enum\":[\"s64\"]"
3743 "},"
3744 "\"value\":{"
3745 "\"type\":\"integer\","
3746 "\"minimum\":-9007199254740991,"
3747 "\"maximum\":9007199254740991"
3748 "}"
3749 "},"
3750 "\"required\":[\"type\",\"value\"]"
3751 "},"
3752 "\"u32Value\":{"
3753 "\"properties\":{"
3754 "\"type\":{"
3755 "\"type\":\"string\","
3756 "\"enum\":[\"u32\"]"
3757 "},"
3758 "\"value\":{"
3759 "\"type\":\"integer\","
3760 "\"minimum\":0,"
3761 "\"maximum\":4294967295"
3762 "}"
3763 "},"
3764 "\"required\":[\"type\",\"value\"]"
3765 "},"
3766 "\"u64Value\":{"
3767 "\"properties\":{"
3768 "\"type\":{"
3769 "\"type\":\"string\","
3770 "\"enum\":[\"u64\"]"
3771 "},"
3772 "\"value\":{"
3773 "\"type\":\"integer\","
3774 "\"minimum\":0,"
3775 "\"maximum\":9007199254740991"
3776 "}"
3777 "},"
3778 "\"required\":[\"type\",\"value\"]"
3779 "},"
3780 "\"strValue\":{"
3781 "\"properties\":{"
3782 "\"type\":{"
3783 "\"type\":\"string\","
3784 "\"enum\":[\"str\"]"
3785 "},"
3786 "\"value\":{\"type\":\"string\"}"
3787 "},"
3788 "\"required\":[\"type\",\"value\"]"
3789 "},"
3790 "\"unknownValue\":{"
3791 "\"properties\":{"
3792 "\"type\":{"
3793 "\"type\":\"integer\","
3794 "\"minimum\":0"
3795 "},"
3796 "\"value\":{"
3797 "\"type\":\"string\","
3798 "\"enum\":[\"unknown\"]"
3799 "}"
3800 "},"
3801 "\"required\":[\"type\",\"value\"]"
3802 "}"
3803 "}"
3804 "}"
3805 "}"
3806 "}");
3807
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003808 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003809 chunk_reset(out);
3810 chunk_appendf(out,
3811 "{\"errorStr\":\"output buffer too short\"}");
3812 }
3813}
3814
3815/* This function dumps the schema onto the stream interface's read buffer.
3816 * It returns 0 as long as it does not complete, non-zero upon completion.
3817 * No state is used.
3818 */
3819static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3820{
3821 chunk_reset(&trash);
3822
3823 stats_dump_json_schema(&trash);
3824
Willy Tarreau06d80a92017-10-19 14:32:15 +02003825 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003826 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003827 return 0;
3828 }
3829
3830 return 1;
3831}
3832
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003833static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003834{
3835 struct proxy *px;
3836 struct server *sv;
3837 struct listener *li;
3838 int clrall = 0;
3839
3840 if (strcmp(args[2], "all") == 0)
3841 clrall = 1;
3842
3843 /* check permissions */
3844 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3845 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3846 return 1;
3847
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003848 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003849 if (clrall) {
3850 memset(&px->be_counters, 0, sizeof(px->be_counters));
3851 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3852 }
3853 else {
3854 px->be_counters.conn_max = 0;
3855 px->be_counters.p.http.rps_max = 0;
3856 px->be_counters.sps_max = 0;
3857 px->be_counters.cps_max = 0;
3858 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003859 px->be_counters.qtime_max = 0;
3860 px->be_counters.ctime_max = 0;
3861 px->be_counters.dtime_max = 0;
3862 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003863
3864 px->fe_counters.conn_max = 0;
3865 px->fe_counters.p.http.rps_max = 0;
3866 px->fe_counters.sps_max = 0;
3867 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003868 }
3869
3870 for (sv = px->srv; sv; sv = sv->next)
3871 if (clrall)
3872 memset(&sv->counters, 0, sizeof(sv->counters));
3873 else {
3874 sv->counters.cur_sess_max = 0;
3875 sv->counters.nbpend_max = 0;
3876 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003877 sv->counters.qtime_max = 0;
3878 sv->counters.ctime_max = 0;
3879 sv->counters.dtime_max = 0;
3880 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003881 }
3882
3883 list_for_each_entry(li, &px->conf.listeners, by_fe)
3884 if (li->counters) {
3885 if (clrall)
3886 memset(li->counters, 0, sizeof(*li->counters));
3887 else
3888 li->counters->conn_max = 0;
3889 }
3890 }
3891
3892 global.cps_max = 0;
3893 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003894 global.ssl_max = 0;
3895 global.ssl_fe_keys_max = 0;
3896 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003897
3898 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003899 return 1;
3900}
3901
3902
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003903static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003904{
Willy Tarreau2f397382019-10-09 11:43:59 +02003905 int arg = 2;
3906
Willy Tarreaud25fc792016-12-16 12:33:47 +01003907 appctx->ctx.stats.scope_str = 0;
3908 appctx->ctx.stats.scope_len = 0;
3909 appctx->ctx.stats.flags = 0;
3910
Willy Tarreau2f397382019-10-09 11:43:59 +02003911 while (*args[arg]) {
3912 if (strcmp(args[arg], "typed") == 0)
3913 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3914 else if (strcmp(args[arg], "json") == 0)
3915 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3916 else if (strcmp(args[arg], "desc") == 0)
3917 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3918 arg++;
3919 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003920 return 0;
3921}
3922
3923
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003924static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003925{
Willy Tarreau2f397382019-10-09 11:43:59 +02003926 int arg = 2;
3927
Willy Tarreaud25fc792016-12-16 12:33:47 +01003928 appctx->ctx.stats.scope_str = 0;
3929 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003930 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3931
3932 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3933 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003934
Willy Tarreau2f397382019-10-09 11:43:59 +02003935 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003936 struct proxy *px;
3937
Willy Tarreau2f397382019-10-09 11:43:59 +02003938 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003939 if (px)
3940 appctx->ctx.stats.iid = px->uuid;
3941 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003942 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003943
Willy Tarreau9d008692019-08-09 11:21:01 +02003944 if (!appctx->ctx.stats.iid)
3945 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003946
Willy Tarreau2b812e22016-11-22 16:18:05 +01003947 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003948 appctx->ctx.stats.type = atoi(args[arg+1]);
3949 appctx->ctx.stats.sid = atoi(args[arg+2]);
3950 arg += 3;
3951 }
3952
3953 while (*args[arg]) {
3954 if (strcmp(args[arg], "typed") == 0)
3955 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3956 else if (strcmp(args[arg], "json") == 0)
3957 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3958 else if (strcmp(args[arg], "desc") == 0)
3959 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3960 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003961 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003962
Willy Tarreau2b812e22016-11-22 16:18:05 +01003963 return 0;
3964}
3965
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003966static int cli_io_handler_dump_info(struct appctx *appctx)
3967{
3968 return stats_dump_info_to_buffer(appctx->owner);
3969}
3970
Willy Tarreau2b812e22016-11-22 16:18:05 +01003971/* This I/O handler runs as an applet embedded in a stream interface. It is
3972 * used to send raw stats over a socket.
3973 */
3974static int cli_io_handler_dump_stat(struct appctx *appctx)
3975{
Christopher Fauletef779222018-10-31 08:47:01 +01003976 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003977}
3978
Simon Horman6f6bb382017-01-04 09:37:26 +01003979static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3980{
3981 return stats_dump_json_schema_to_buffer(appctx->owner);
3982}
3983
Willy Tarreau2b812e22016-11-22 16:18:05 +01003984/* register cli keywords */
3985static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003986 { { "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 +02003987 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3988 { { "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 +01003989 { { "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 +01003990 {{},}
3991}};
3992
Willy Tarreau0108d902018-11-25 19:14:37 +01003993INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3994
William Lallemand74c24fb2016-11-21 17:18:36 +01003995struct applet http_stats_applet = {
3996 .obj_type = OBJ_TYPE_APPLET,
3997 .name = "<STATS>", /* used for logging */
3998 .fct = http_stats_io_handler,
3999 .release = NULL,
4000};
4001
William Lallemand74c24fb2016-11-21 17:18:36 +01004002/*
4003 * Local variables:
4004 * c-indent-level: 8
4005 * c-basic-offset: 8
4006 * End:
4007 */