blob: 6ba3f62cde98107c36572acff87d0794ee133624 [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
612/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200613static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200614 const struct field *stats, size_t stats_count,
615 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100616{
617 int field;
618 int started = 0;
619
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200620 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100621 return 0;
622 if (!chunk_strcat(out, "["))
623 return 0;
624
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200625 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100626 const char *obj_type;
627 int old_len;
628
629 if (!stats[field].type)
630 continue;
631
632 if (started && !chunk_strcat(out, ","))
633 goto err;
634 started = 1;
635
636 switch (stats[ST_F_TYPE].u.u32) {
637 case STATS_TYPE_FE: obj_type = "Frontend"; break;
638 case STATS_TYPE_BE: obj_type = "Backend"; break;
639 case STATS_TYPE_SO: obj_type = "Listener"; break;
640 case STATS_TYPE_SV: obj_type = "Server"; break;
641 default: obj_type = "Unknown"; break;
642 }
643
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200644 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100645 chunk_appendf(out,
646 "{"
647 "\"objType\":\"%s\","
648 "\"proxyId\":%d,"
649 "\"id\":%d,"
650 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
651 "\"processNum\":%u,",
652 obj_type, stats[ST_F_IID].u.u32,
653 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200654 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200655 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100656 goto err;
657
658 if (!stats_emit_json_field_tags(out, &stats[field]))
659 goto err;
660
661 if (!stats_emit_json_data_field(out, &stats[field]))
662 goto err;
663
664 if (!chunk_strcat(out, "}"))
665 goto err;
666 }
667
668 if (!chunk_strcat(out, "]"))
669 goto err;
670
671 return 1;
672
673err:
674 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200675 if (flags & STAT_STARTED)
676 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100677 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
678 return 0;
679}
680
William Lallemand74c24fb2016-11-21 17:18:36 +0100681/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200682 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200683 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100684 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200685static int stats_dump_fields_html(struct buffer *out,
686 const struct field *stats,
687 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100688{
Willy Tarreau83061a82018-07-13 11:56:34 +0200689 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100690
691 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
692 chunk_appendf(out,
693 /* name, queue */
694 "<tr class=\"frontend\">");
695
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200696 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100697 /* Column sub-heading for Enable or Disable server */
698 chunk_appendf(out, "<td></td>");
699 }
700
701 chunk_appendf(out,
702 "<td class=ac>"
703 "<a name=\"%s/Frontend\"></a>"
704 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
705 "<td colspan=3></td>"
706 "",
707 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
708
709 chunk_appendf(out,
710 /* sessions rate : current */
711 "<td><u>%s<div class=tips><table class=det>"
712 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
713 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
714 "",
715 U2H(stats[ST_F_RATE].u.u32),
716 U2H(stats[ST_F_CONN_RATE].u.u32),
717 U2H(stats[ST_F_RATE].u.u32));
718
719 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
720 chunk_appendf(out,
721 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
722 U2H(stats[ST_F_REQ_RATE].u.u32));
723
724 chunk_appendf(out,
725 "</table></div></u></td>"
726 /* sessions rate : max */
727 "<td><u>%s<div class=tips><table class=det>"
728 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
729 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
730 "",
731 U2H(stats[ST_F_RATE_MAX].u.u32),
732 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
733 U2H(stats[ST_F_RATE_MAX].u.u32));
734
735 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
736 chunk_appendf(out,
737 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
738 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
739
740 chunk_appendf(out,
741 "</table></div></u></td>"
742 /* sessions rate : limit */
743 "<td>%s</td>",
744 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
745
746 chunk_appendf(out,
747 /* sessions: current, max, limit, total */
748 "<td>%s</td><td>%s</td><td>%s</td>"
749 "<td><u>%s<div class=tips><table class=det>"
750 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
751 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
752 "",
753 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
754 U2H(stats[ST_F_STOT].u.u64),
755 U2H(stats[ST_F_CONN_TOT].u.u64),
756 U2H(stats[ST_F_STOT].u.u64));
757
758 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
759 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
760 chunk_appendf(out,
761 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
762 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
763 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
764 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
765 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
766 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
767 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
768 "<tr><th>- other responses:</th><td>%s</td></tr>"
769 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100770 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
771 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200772 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100773 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100774 "",
775 U2H(stats[ST_F_REQ_TOT].u.u64),
776 U2H(stats[ST_F_HRSP_1XX].u.u64),
777 U2H(stats[ST_F_HRSP_2XX].u.u64),
778 U2H(stats[ST_F_COMP_RSP].u.u64),
779 stats[ST_F_HRSP_2XX].u.u64 ?
780 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
781 U2H(stats[ST_F_HRSP_3XX].u.u64),
782 U2H(stats[ST_F_HRSP_4XX].u.u64),
783 U2H(stats[ST_F_HRSP_5XX].u.u64),
784 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200785 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100786 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
787 U2H(stats[ST_F_CACHE_HITS].u.u64),
788 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
789 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100790 U2H(stats[ST_F_WREW].u.u64),
791 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100792 }
793
794 chunk_appendf(out,
795 "</table></div></u></td>"
796 /* sessions: lbtot, lastsess */
797 "<td></td><td></td>"
798 /* bytes : in */
799 "<td>%s</td>"
800 "",
801 U2H(stats[ST_F_BIN].u.u64));
802
803 chunk_appendf(out,
804 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
805 "<td>%s%s<div class=tips><table class=det>"
806 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
807 "<tr><th>Compression in:</th><td>%s</td></tr>"
808 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
809 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
810 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
811 "</table></div>%s</td>",
812 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
813 U2H(stats[ST_F_BOUT].u.u64),
814 U2H(stats[ST_F_BOUT].u.u64),
815 U2H(stats[ST_F_COMP_IN].u.u64),
816 U2H(stats[ST_F_COMP_OUT].u.u64),
817 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
818 U2H(stats[ST_F_COMP_BYP].u.u64),
819 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
820 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,
821 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
822
823 chunk_appendf(out,
824 /* denied: req, resp */
825 "<td>%s</td><td>%s</td>"
826 /* errors : request, connect, response */
827 "<td>%s</td><td></td><td></td>"
828 /* warnings: retries, redispatches */
829 "<td></td><td></td>"
830 /* server status : reflect frontend status */
831 "<td class=ac>%s</td>"
832 /* rest of server: nothing */
833 "<td class=ac colspan=8></td></tr>"
834 "",
835 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
836 U2H(stats[ST_F_EREQ].u.u64),
837 field_str(stats, ST_F_STATUS));
838 }
839 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
840 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200841 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100842 /* Column sub-heading for Enable or Disable server */
843 chunk_appendf(out, "<td></td>");
844 }
845
846 chunk_appendf(out,
847 /* frontend name, listener name */
848 "<td class=ac><a name=\"%s/+%s\"></a>%s"
849 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
850 "",
851 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200852 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100853 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
854
Willy Tarreau708c4162019-10-09 10:19:16 +0200855 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100856 chunk_appendf(out, "<div class=tips>");
857
Willy Tarreau90807112020-02-25 08:16:33 +0100858 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100859 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
860 else if (*field_str(stats, ST_F_ADDR) == '[')
861 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
862 else if (*field_str(stats, ST_F_ADDR))
863 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
864
865 /* id */
866 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
867 }
868
869 chunk_appendf(out,
870 /* queue */
871 "%s</td><td colspan=3></td>"
872 /* sessions rate: current, max, limit */
873 "<td colspan=3>&nbsp;</td>"
874 /* sessions: current, max, limit, total, lbtot, lastsess */
875 "<td>%s</td><td>%s</td><td>%s</td>"
876 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
877 /* bytes: in, out */
878 "<td>%s</td><td>%s</td>"
879 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200880 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100881 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
882 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
883
884 chunk_appendf(out,
885 /* denied: req, resp */
886 "<td>%s</td><td>%s</td>"
887 /* errors: request, connect, response */
888 "<td>%s</td><td></td><td></td>"
889 /* warnings: retries, redispatches */
890 "<td></td><td></td>"
891 /* server status: reflect listener status */
892 "<td class=ac>%s</td>"
893 /* rest of server: nothing */
894 "<td class=ac colspan=8></td></tr>"
895 "",
896 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
897 U2H(stats[ST_F_EREQ].u.u64),
898 field_str(stats, ST_F_STATUS));
899 }
900 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
901 const char *style;
902
903 /* determine the style to use depending on the server's state,
904 * its health and weight. There isn't a 1-to-1 mapping between
905 * state and styles for the cases where the server is (still)
906 * up. The reason is that we don't want to report nolb and
907 * drain with the same color.
908 */
909
910 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
911 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
912 style = "down";
913 }
914 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
915 style = "going_up";
916 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400917 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
918 style = "draining";
919 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100920 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
921 style = "going_down";
922 }
923 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
924 style = "nolb";
925 }
926 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
927 style = "no_check";
928 }
929 else if (!stats[ST_F_CHKFAIL].type ||
930 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
931 /* no check or max health = UP */
932 if (stats[ST_F_WEIGHT].u.u32)
933 style = "up";
934 else
935 style = "draining";
936 }
937 else {
938 style = "going_down";
939 }
940
Willy Tarreau7b524852020-08-11 10:26:36 +0200941 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +0100942 chunk_appendf(out, "<tr class=\"maintain\">");
943 else
944 chunk_appendf(out,
945 "<tr class=\"%s_%s\">",
946 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
947
948
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200949 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100950 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000951 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
952 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100953 field_str(stats, ST_F_SVNAME));
954
955 chunk_appendf(out,
956 "<td class=ac><a name=\"%s/%s\"></a>%s"
957 "<a class=lfsb href=\"#%s/%s\">%s</a>"
958 "",
959 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200960 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100961 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
962
Willy Tarreau708c4162019-10-09 10:19:16 +0200963 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100964 chunk_appendf(out, "<div class=tips>");
965
Willy Tarreau90807112020-02-25 08:16:33 +0100966 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100967 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
968 else if (*field_str(stats, ST_F_ADDR) == '[')
969 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
970 else if (*field_str(stats, ST_F_ADDR))
971 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
972
973 /* id */
974 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
975
976 /* cookie */
977 if (stats[ST_F_COOKIE].type) {
978 chunk_appendf(out, ", cookie: '");
979 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
980 chunk_htmlencode(out, &src);
981 chunk_appendf(out, "'");
982 }
983
984 chunk_appendf(out, "</div>");
985 }
986
987 chunk_appendf(out,
988 /* queue : current, max, limit */
989 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
990 /* sessions rate : current, max, limit */
991 "<td>%s</td><td>%s</td><td></td>"
992 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200993 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100994 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
995 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
996
997 chunk_appendf(out,
998 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200999 "<td><u>%s<div class=tips>"
1000 "<table class=det>"
1001 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001002 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001003 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001004 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1005 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001006 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001007 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1008 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1009 "</table></div></u>"
1010 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001011 "<td><u>%s<div class=tips><table class=det>"
1012 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1013 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001014 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001015 U2H(stats[ST_F_SCUR].u.u32),
1016 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1017 U2H(stats[ST_F_SRV_ICUR].u.u32),
1018 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1019 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001020 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001021
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001022 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1023 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1024 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001025 U2H(stats[ST_F_STOT].u.u64),
1026 U2H(stats[ST_F_STOT].u.u64));
1027
1028 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1029 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001030 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001031 "<tr><th>New connections:</th><td>%s</td></tr>"
1032 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001033 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001034 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1035 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1036 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1037 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1038 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1039 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001040 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001041 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001042 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001043 U2H(stats[ST_F_CONNECT].u.u64),
1044 U2H(stats[ST_F_REUSE].u.u64),
1045 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1046 (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 +02001047 U2H(stats[ST_F_REQ_TOT].u.u64),
1048 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1049 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1050 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1051 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1052 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1053 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1054 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1055 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1056 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1057 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1058 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1059 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001060 U2H(stats[ST_F_WREW].u.u64),
1061 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001062 }
1063
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001064 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1065 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1066 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1067 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1068 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001069 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001070 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1071 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1072 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1073 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001074
1075 chunk_appendf(out,
1076 "</table></div></u></td>"
1077 /* sessions: lbtot, last */
1078 "<td>%s</td><td>%s</td>",
1079 U2H(stats[ST_F_LBTOT].u.u64),
1080 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1081
1082 chunk_appendf(out,
1083 /* bytes : in, out */
1084 "<td>%s</td><td>%s</td>"
1085 /* denied: req, resp */
1086 "<td></td><td>%s</td>"
1087 /* errors : request, connect */
1088 "<td></td><td>%s</td>"
1089 /* errors : response */
1090 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1091 /* warnings: retries, redispatches */
1092 "<td>%lld</td><td>%lld</td>"
1093 "",
1094 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1095 U2H(stats[ST_F_DRESP].u.u64),
1096 U2H(stats[ST_F_ECON].u.u64),
1097 U2H(stats[ST_F_ERESP].u.u64),
1098 (long long)stats[ST_F_CLI_ABRT].u.u64,
1099 (long long)stats[ST_F_SRV_ABRT].u.u64,
1100 (long long)stats[ST_F_WRETR].u.u64,
1101 (long long)stats[ST_F_WREDIS].u.u64);
1102
1103 /* status, last change */
1104 chunk_appendf(out, "<td class=ac>");
1105
1106 /* FIXME!!!!
1107 * LASTCHG should contain the last change for *this* server and must be computed
1108 * properly above, as was done below, ie: this server if maint, otherwise ref server
1109 * if tracking. Note that ref is either local or remote depending on tracking.
1110 */
1111
1112
Willy Tarreau7b524852020-08-11 10:26:36 +02001113 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001114 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1115 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001116 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001117 chunk_strcat(out, "<i>no check</i>");
1118 }
1119 else {
1120 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 +02001121 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001122 if (stats[ST_F_CHECK_HEALTH].u.u32)
1123 chunk_strcat(out, " &uarr;");
1124 }
1125 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1126 chunk_strcat(out, " &darr;");
1127 }
1128
Willy Tarreau7b524852020-08-11 10:26:36 +02001129 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001130 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1131 chunk_appendf(out,
1132 "</td><td class=ac><u> %s",
1133 field_str(stats, ST_F_AGENT_STATUS));
1134
1135 if (stats[ST_F_AGENT_CODE].type)
1136 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1137
1138 if (stats[ST_F_AGENT_DURATION].type)
1139 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1140
1141 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1142
1143 if (*field_str(stats, ST_F_LAST_AGT)) {
1144 chunk_appendf(out, ": ");
1145 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1146 chunk_htmlencode(out, &src);
1147 }
1148 chunk_appendf(out, "</div></u>");
1149 }
1150 else if (stats[ST_F_CHECK_STATUS].type) {
1151 chunk_appendf(out,
1152 "</td><td class=ac><u> %s",
1153 field_str(stats, ST_F_CHECK_STATUS));
1154
1155 if (stats[ST_F_CHECK_CODE].type)
1156 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1157
1158 if (stats[ST_F_CHECK_DURATION].type)
1159 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1160
1161 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1162
1163 if (*field_str(stats, ST_F_LAST_CHK)) {
1164 chunk_appendf(out, ": ");
1165 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1166 chunk_htmlencode(out, &src);
1167 }
1168 chunk_appendf(out, "</div></u>");
1169 }
1170 else
1171 chunk_appendf(out, "</td><td>");
1172
1173 chunk_appendf(out,
1174 /* weight */
1175 "</td><td class=ac>%d</td>"
1176 /* act, bck */
1177 "<td class=ac>%s</td><td class=ac>%s</td>"
1178 "",
1179 stats[ST_F_WEIGHT].u.u32,
1180 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1181 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1182
1183 /* check failures: unique, fatal, down time */
1184 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1185 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1186 }
1187 else if (stats[ST_F_CHKFAIL].type) {
1188 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1189
1190 if (stats[ST_F_HANAFAIL].type)
1191 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1192
1193 chunk_appendf(out,
1194 "<div class=tips>Failed Health Checks%s</div></u></td>"
1195 "<td>%lld</td><td>%s</td>"
1196 "",
1197 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1198 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1199 }
1200 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1201 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1202 chunk_appendf(out,
1203 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1204 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1205 }
1206 else
1207 chunk_appendf(out, "<td colspan=3></td>");
1208
1209 /* throttle */
1210 if (stats[ST_F_THROTTLE].type)
1211 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1212 else
1213 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1214 }
1215 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1216 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001217 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001218 /* Column sub-heading for Enable or Disable server */
1219 chunk_appendf(out, "<td></td>");
1220 }
1221 chunk_appendf(out,
1222 "<td class=ac>"
1223 /* name */
1224 "%s<a name=\"%s/Backend\"></a>"
1225 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1226 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001227 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001228 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1229
Willy Tarreau708c4162019-10-09 10:19:16 +02001230 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001231 /* balancing */
1232 chunk_appendf(out, "<div class=tips>balancing: %s",
1233 field_str(stats, ST_F_ALGO));
1234
1235 /* cookie */
1236 if (stats[ST_F_COOKIE].type) {
1237 chunk_appendf(out, ", cookie: '");
1238 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1239 chunk_htmlencode(out, &src);
1240 chunk_appendf(out, "'");
1241 }
1242 chunk_appendf(out, "</div>");
1243 }
1244
1245 chunk_appendf(out,
1246 "%s</td>"
1247 /* queue : current, max */
1248 "<td>%s</td><td>%s</td><td></td>"
1249 /* sessions rate : current, max, limit */
1250 "<td>%s</td><td>%s</td><td></td>"
1251 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001252 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001253 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1254 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1255
1256 chunk_appendf(out,
1257 /* sessions: current, max, limit, total */
1258 "<td>%s</td><td>%s</td><td>%s</td>"
1259 "<td><u>%s<div class=tips><table class=det>"
1260 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1261 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001262 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 +01001263 U2H(stats[ST_F_STOT].u.u64),
1264 U2H(stats[ST_F_STOT].u.u64));
1265
1266 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1267 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1268 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001269 "<tr><th>New connections:</th><td>%s</td></tr>"
1270 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001271 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1272 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1273 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1274 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1275 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1276 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1277 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1278 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001279 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1280 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001281 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001282 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001283 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001284 U2H(stats[ST_F_CONNECT].u.u64),
1285 U2H(stats[ST_F_REUSE].u.u64),
1286 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1287 (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 +01001288 U2H(stats[ST_F_REQ_TOT].u.u64),
1289 U2H(stats[ST_F_HRSP_1XX].u.u64),
1290 U2H(stats[ST_F_HRSP_2XX].u.u64),
1291 U2H(stats[ST_F_COMP_RSP].u.u64),
1292 stats[ST_F_HRSP_2XX].u.u64 ?
1293 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1294 U2H(stats[ST_F_HRSP_3XX].u.u64),
1295 U2H(stats[ST_F_HRSP_4XX].u.u64),
1296 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001297 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001298 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1299 U2H(stats[ST_F_CACHE_HITS].u.u64),
1300 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1301 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001302 U2H(stats[ST_F_WREW].u.u64),
1303 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001304 }
1305
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001306 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1307 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1308 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1309 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1310 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001311 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001312 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1313 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1314 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1315 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001316
1317 chunk_appendf(out,
1318 "</table></div></u></td>"
1319 /* sessions: lbtot, last */
1320 "<td>%s</td><td>%s</td>"
1321 /* bytes: in */
1322 "<td>%s</td>"
1323 "",
1324 U2H(stats[ST_F_LBTOT].u.u64),
1325 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1326 U2H(stats[ST_F_BIN].u.u64));
1327
1328 chunk_appendf(out,
1329 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1330 "<td>%s%s<div class=tips><table class=det>"
1331 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1332 "<tr><th>Compression in:</th><td>%s</td></tr>"
1333 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1334 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1335 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1336 "</table></div>%s</td>",
1337 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1338 U2H(stats[ST_F_BOUT].u.u64),
1339 U2H(stats[ST_F_BOUT].u.u64),
1340 U2H(stats[ST_F_COMP_IN].u.u64),
1341 U2H(stats[ST_F_COMP_OUT].u.u64),
1342 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1343 U2H(stats[ST_F_COMP_BYP].u.u64),
1344 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1345 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,
1346 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1347
1348 chunk_appendf(out,
1349 /* denied: req, resp */
1350 "<td>%s</td><td>%s</td>"
1351 /* errors : request, connect */
1352 "<td></td><td>%s</td>"
1353 /* errors : response */
1354 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1355 /* warnings: retries, redispatches */
1356 "<td>%lld</td><td>%lld</td>"
1357 /* backend status: reflect backend status (up/down): we display UP
1358 * if the backend has known working servers or if it has no server at
1359 * all (eg: for stats). Then we display the total weight, number of
1360 * active and backups. */
1361 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1362 "<td class=ac>%d</td><td class=ac>%d</td>"
1363 "",
1364 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1365 U2H(stats[ST_F_ECON].u.u64),
1366 U2H(stats[ST_F_ERESP].u.u64),
1367 (long long)stats[ST_F_CLI_ABRT].u.u64,
1368 (long long)stats[ST_F_SRV_ABRT].u.u64,
1369 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1370 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1371 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1372 stats[ST_F_WEIGHT].u.u32,
1373 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1374
1375 chunk_appendf(out,
1376 /* rest of backend: nothing, down transitions, total downtime, throttle */
1377 "<td class=ac>&nbsp;</td><td>%d</td>"
1378 "<td>%s</td>"
1379 "<td></td>"
1380 "</tr>",
1381 stats[ST_F_CHKDOWN].u.u32,
1382 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1383 }
1384 return 1;
1385}
1386
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001387int stats_dump_one_line(const struct field *stats, size_t stats_count,
1388 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001389{
Simon Horman05ee2132017-01-04 09:37:25 +01001390 int ret;
1391
William Lallemand74c24fb2016-11-21 17:18:36 +01001392 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001393 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001394 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001395 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001396 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001397 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001398 else
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001399 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001400
1401 if (ret)
1402 appctx->ctx.stats.flags |= STAT_STARTED;
1403
1404 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001405}
1406
1407/* Fill <stats> with the frontend statistics. <stats> is
1408 * preallocated array of length <len>. The length of the array
1409 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1410 * this value, the function returns 0, otherwise, it returns 1.
1411 */
1412int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1413{
1414 if (len < ST_F_TOTAL_FIELDS)
1415 return 0;
1416
1417 memset(stats, 0, sizeof(*stats) * len);
1418
1419 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1420 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1421 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1422 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1423 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1424 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1425 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1426 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1427 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1428 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1429 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1430 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1431 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1432 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1433 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1434 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1435 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1436 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1437 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1438 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1439 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1440 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001441 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001442 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001443
1444 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1445 if (px->mode == PR_MODE_HTTP) {
1446 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1447 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1448 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1449 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1450 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1451 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1452 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001453 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1454 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001455 }
1456
1457 /* requests : req_rate, req_rate_max, req_tot, */
1458 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1459 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1460 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1461
1462 /* compression: in, out, bypassed, responses */
1463 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1464 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1465 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1466 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1467
1468 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1469 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1470 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1471 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1472
1473 return 1;
1474}
1475
1476/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1477 * the state from stream interface <si>. The caller is responsible for clearing
1478 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1479 */
1480static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1481{
1482 struct appctx *appctx = __objt_appctx(si->end);
1483
1484 if (!(px->cap & PR_CAP_FE))
1485 return 0;
1486
1487 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1488 return 0;
1489
1490 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1491 return 0;
1492
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001493 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001494}
1495
1496/* Fill <stats> with the listener statistics. <stats> is
1497 * preallocated array of length <len>. The length of the array
1498 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1499 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001500 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001501 */
1502int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1503 struct field *stats, int len)
1504{
Willy Tarreau83061a82018-07-13 11:56:34 +02001505 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001506
1507 if (len < ST_F_TOTAL_FIELDS)
1508 return 0;
1509
1510 if (!l->counters)
1511 return 0;
1512
1513 chunk_reset(out);
1514 memset(stats, 0, sizeof(*stats) * len);
1515
1516 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1517 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1518 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1519 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1520 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1521 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1522 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1523 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1524 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1525 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1526 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1527 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1528 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1529 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001530 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 +01001531 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1532 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1533 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1534 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001535 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001536 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001537
Willy Tarreau708c4162019-10-09 10:19:16 +02001538 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001539 char str[INET6_ADDRSTRLEN];
1540 int port;
1541
Willy Tarreau37159062020-08-27 07:48:42 +02001542 port = get_host_port(&l->rx.addr);
1543 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001544 case AF_INET:
1545 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1546 chunk_appendf(out, "%s:%d", str, port);
1547 break;
1548 case AF_INET6:
1549 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1550 chunk_appendf(out, "[%s]:%d", str, port);
1551 break;
1552 case AF_UNIX:
1553 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1554 break;
1555 case -1:
1556 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1557 chunk_strcat(out, strerror(errno));
1558 break;
1559 default: /* address family not supported */
1560 break;
1561 }
1562 }
1563
1564 return 1;
1565}
1566
1567/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001568 * from stream interface <si>. The caller is responsible for clearing the trash
1569 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001570 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001571static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001572{
1573 struct appctx *appctx = __objt_appctx(si->end);
1574
Willy Tarreau43241ff2019-10-09 11:27:51 +02001575 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001576 return 0;
1577
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001578 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001579}
1580
1581enum srv_stats_state {
1582 SRV_STATS_STATE_DOWN = 0,
1583 SRV_STATS_STATE_DOWN_AGENT,
1584 SRV_STATS_STATE_GOING_UP,
1585 SRV_STATS_STATE_UP_GOING_DOWN,
1586 SRV_STATS_STATE_UP,
1587 SRV_STATS_STATE_NOLB_GOING_DOWN,
1588 SRV_STATS_STATE_NOLB,
1589 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1590 SRV_STATS_STATE_DRAIN,
1591 SRV_STATS_STATE_DRAIN_AGENT,
1592 SRV_STATS_STATE_NO_CHECK,
1593
1594 SRV_STATS_STATE_COUNT, /* Must be last */
1595};
1596
1597static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1598 [SRV_STATS_STATE_DOWN] = "DOWN",
1599 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1600 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1601 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1602 [SRV_STATS_STATE_UP] = "UP",
1603 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1604 [SRV_STATS_STATE_NOLB] = "NOLB",
1605 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1606 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1607 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1608 [SRV_STATS_STATE_NO_CHECK] = "no check"
1609};
1610
1611/* Fill <stats> with the server statistics. <stats> is
1612 * preallocated array of length <len>. The length of the array
1613 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1614 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001615 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001616 */
1617int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1618 struct field *stats, int len)
1619{
1620 struct server *via, *ref;
1621 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001622 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001623 enum srv_stats_state state;
1624 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001625 long long srv_samples_counter;
1626 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001627
1628 if (len < ST_F_TOTAL_FIELDS)
1629 return 0;
1630
1631 memset(stats, 0, sizeof(*stats) * len);
1632
1633 /* we have "via" which is the tracked server as described in the configuration,
1634 * and "ref" which is the checked server and the end of the chain.
1635 */
1636 via = sv->track ? sv->track : sv;
1637 ref = via;
1638 while (ref->track)
1639 ref = ref->track;
1640
Emeric Brun52a91d32017-08-31 14:41:55 +02001641 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001642 if ((ref->check.state & CHK_ST_ENABLED) &&
1643 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1644 state = SRV_STATS_STATE_UP_GOING_DOWN;
1645 } else {
1646 state = SRV_STATS_STATE_UP;
1647 }
1648
Emeric Brun52a91d32017-08-31 14:41:55 +02001649 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001650 if (ref->agent.state & CHK_ST_ENABLED)
1651 state = SRV_STATS_STATE_DRAIN_AGENT;
1652 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1653 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1654 else
1655 state = SRV_STATS_STATE_DRAIN;
1656 }
1657
1658 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1659 state = SRV_STATS_STATE_NO_CHECK;
1660 }
1661 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001662 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001663 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1664 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1665 state = SRV_STATS_STATE_NOLB;
1666 } else {
1667 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1668 }
1669 }
1670 else { /* stopped */
1671 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1672 state = SRV_STATS_STATE_DOWN_AGENT;
1673 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1674 state = SRV_STATS_STATE_DOWN; /* DOWN */
1675 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1676 state = SRV_STATS_STATE_GOING_UP;
1677 } else {
1678 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1679 }
1680 }
1681
1682 chunk_reset(out);
1683
1684 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1685 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1686 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1687 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1688 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1689 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1690 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1691
1692 if (sv->maxconn)
1693 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1694
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001695 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1696 if (sv->max_idle_conns != -1)
1697 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1698
William Lallemand74c24fb2016-11-21 17:18:36 +01001699 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1700 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1701 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001702 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001703 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1704 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1705 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1706 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001707 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001708 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001709 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1710 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001711
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001712 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1713 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1714 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001715 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001716
William Lallemand74c24fb2016-11-21 17:18:36 +01001717 /* status */
1718 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001719 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001720 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001721 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001722 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001723 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001724 chunk_appendf(out, "MAINT");
1725 else
1726 chunk_appendf(out,
1727 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001728 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1729 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001730
1731 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1732 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001733 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 +01001734 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1735 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1736
1737 /* check failures: unique, fatal; last change, total downtime */
1738 if (sv->check.state & CHK_ST_ENABLED) {
1739 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1740 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1741 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1742 }
1743
1744 if (sv->maxqueue)
1745 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1746
1747 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1748 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1749 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1750
Emeric Brun52a91d32017-08-31 14:41:55 +02001751 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001752 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1753
1754 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1755
1756 if (sv->track) {
1757 char *fld_track = chunk_newstr(out);
1758
1759 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1760 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1761 }
1762
1763 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1764 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1765 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1766
1767 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1768 const char *fld_chksts;
1769
1770 fld_chksts = chunk_newstr(out);
1771 chunk_strcat(out, "* "); // for check in progress
1772 chunk_strcat(out, get_check_status_info(sv->check.status));
1773 if (!(sv->check.state & CHK_ST_INPROGRESS))
1774 fld_chksts += 2; // skip "* "
1775 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1776
1777 if (sv->check.status >= HCHK_STATUS_L57DATA)
1778 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1779
1780 if (sv->check.status >= HCHK_STATUS_CHECKED)
1781 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1782
1783 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1784 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1785 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1786 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1787 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1788 }
1789
1790 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1791 const char *fld_chksts;
1792
1793 fld_chksts = chunk_newstr(out);
1794 chunk_strcat(out, "* "); // for check in progress
1795 chunk_strcat(out, get_check_status_info(sv->agent.status));
1796 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1797 fld_chksts += 2; // skip "* "
1798 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1799
1800 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1801 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1802
1803 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1804 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1805
1806 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1807 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1808 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1809 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1810 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1811 }
1812
1813 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1814 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001815 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001816 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1817 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1818 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1819 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1820 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1821 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1822 }
1823
1824 if (ref->observe)
1825 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1826
1827 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1828 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1829 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1830
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001831 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1832 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1833 srv_samples_window = srv_samples_counter;
1834
1835 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1836 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1837 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1838 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001839
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001840 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1841 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1842 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1843 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1844
Willy Tarreau708c4162019-10-09 10:19:16 +02001845 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001846 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1847 case AF_INET:
1848 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001849 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001850 break;
1851 case AF_INET6:
1852 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001853 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001854 break;
1855 case AF_UNIX:
1856 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1857 break;
1858 case -1:
1859 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1860 chunk_strcat(out, strerror(errno));
1861 break;
1862 default: /* address family not supported */
1863 break;
1864 }
1865
1866 if (sv->cookie)
1867 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1868 }
1869
1870 return 1;
1871}
1872
1873/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001874 * from stream interface <si>, and server state <state>. The caller is
1875 * responsible for clearing the trash if needed. Returns non-zero if it emits
1876 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001877 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001878static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001879{
1880 struct appctx *appctx = __objt_appctx(si->end);
1881
Willy Tarreau43241ff2019-10-09 11:27:51 +02001882 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001883 return 0;
1884
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001885 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001886}
1887
1888/* Fill <stats> with the backend statistics. <stats> is
1889 * preallocated array of length <len>. The length of the array
1890 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1891 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001892 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001893 */
1894int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1895{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001896 long long be_samples_counter;
1897 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1898
William Lallemand74c24fb2016-11-21 17:18:36 +01001899 if (len < ST_F_TOTAL_FIELDS)
1900 return 0;
1901
1902 memset(stats, 0, sizeof(*stats) * len);
1903
1904 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1905 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1906 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1907 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1908 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001909 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001910 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1911 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1912 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1913 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1914 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1915 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1916 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1917 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1918 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1919 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1920 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001921 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001922 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001923 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1924 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001925 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1926 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1927 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1928 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1929 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1930 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1931 if (px->srv)
1932 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1933
1934 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1935 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1936 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1937 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1938 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1939 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1940 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1941
Willy Tarreau708c4162019-10-09 10:19:16 +02001942 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001943 if (px->cookie_name)
1944 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1945 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1946 }
1947
1948 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1949 if (px->mode == PR_MODE_HTTP) {
1950 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1951 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1952 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1953 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1954 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1955 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1956 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001957 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1958 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001959 }
1960
1961 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1962 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1963
1964 /* compression: in, out, bypassed, responses */
1965 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1966 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1967 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1968 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1969 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1970
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001971 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1972 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1973 be_samples_window = be_samples_counter;
1974
1975 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1976 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1977 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1978 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 +01001979
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001980 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1981 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1982 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1983 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1984
William Lallemand74c24fb2016-11-21 17:18:36 +01001985 return 1;
1986}
1987
1988/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001989 * interface <si>. The caller is responsible for clearing the trash if needed.
1990 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001991 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001992static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001993{
1994 struct appctx *appctx = __objt_appctx(si->end);
1995
1996 if (!(px->cap & PR_CAP_BE))
1997 return 0;
1998
1999 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2000 return 0;
2001
Willy Tarreau43241ff2019-10-09 11:27:51 +02002002 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002003 return 0;
2004
Amaury Denoyelle97323c92020-10-02 18:32:01 +02002005 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002006}
2007
2008/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2009 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2010 * for clearing the trash if needed.
2011 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002012static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002013{
2014 struct appctx *appctx = __objt_appctx(si->end);
2015 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2016
2017 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2018 /* A form to enable/disable this proxy servers */
2019
2020 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2021 scope_txt[0] = 0;
2022 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002023 const char *scope_ptr = stats_scope_ptr(appctx, si);
2024
William Lallemand74c24fb2016-11-21 17:18:36 +01002025 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002026 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002027 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2028 }
2029
2030 chunk_appendf(&trash,
2031 "<form method=\"post\">");
2032 }
2033
2034 /* print a new table */
2035 chunk_appendf(&trash,
2036 "<table class=\"tbl\" width=\"100%%\">\n"
2037 "<tr class=\"titre\">"
2038 "<th class=\"pxname\" width=\"10%%\">");
2039
2040 chunk_appendf(&trash,
2041 "<a name=\"%s\"></a>%s"
2042 "<a class=px href=\"#%s\">%s</a>",
2043 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002044 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002045 px->id, px->id);
2046
Willy Tarreau676c29e2019-10-09 10:50:01 +02002047 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002048 /* cap, mode, id */
2049 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2050 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2051 px->uuid);
2052 chunk_appendf(&trash, "</div>");
2053 }
2054
2055 chunk_appendf(&trash,
2056 "%s</th>"
2057 "<th class=\"%s\" width=\"90%%\">%s</th>"
2058 "</tr>\n"
2059 "</table>\n"
2060 "<table class=\"tbl\" width=\"100%%\">\n"
2061 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002062 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002063 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2064
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002065 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002066 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002067 if ((px->cap & PR_CAP_BE) && px->srv)
2068 chunk_appendf(&trash,
2069 "<th rowspan=2 width=1><input type=\"checkbox\" "
2070 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2071 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2072 px->id,
2073 px->id);
2074 else
2075 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002076 }
2077
2078 chunk_appendf(&trash,
2079 "<th rowspan=2></th>"
2080 "<th colspan=3>Queue</th>"
2081 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2082 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2083 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2084 "<th colspan=9>Server</th>"
2085 "</tr>\n"
2086 "<tr class=\"titre\">"
2087 "<th>Cur</th><th>Max</th><th>Limit</th>"
2088 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2089 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2090 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2091 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2092 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2093 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2094 "<th>Thrtle</th>\n"
2095 "</tr>");
2096}
2097
2098/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2099 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2100 */
2101static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2102{
2103 struct appctx *appctx = __objt_appctx(si->end);
2104 chunk_appendf(&trash, "</table>");
2105
2106 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2107 /* close the form used to enable/disable this proxy servers */
2108 chunk_appendf(&trash,
2109 "Choose the action to perform on the checked servers : "
2110 "<select name=action>"
2111 "<option value=\"\"></option>"
2112 "<option value=\"ready\">Set state to READY</option>"
2113 "<option value=\"drain\">Set state to DRAIN</option>"
2114 "<option value=\"maint\">Set state to MAINT</option>"
2115 "<option value=\"dhlth\">Health: disable checks</option>"
2116 "<option value=\"ehlth\">Health: enable checks</option>"
2117 "<option value=\"hrunn\">Health: force UP</option>"
2118 "<option value=\"hnolb\">Health: force NOLB</option>"
2119 "<option value=\"hdown\">Health: force DOWN</option>"
2120 "<option value=\"dagent\">Agent: disable checks</option>"
2121 "<option value=\"eagent\">Agent: enable checks</option>"
2122 "<option value=\"arunn\">Agent: force UP</option>"
2123 "<option value=\"adown\">Agent: force DOWN</option>"
2124 "<option value=\"shutdown\">Kill Sessions</option>"
2125 "</select>"
2126 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2127 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2128 "</form>",
2129 px->uuid);
2130 }
2131
2132 chunk_appendf(&trash, "<p>\n");
2133}
2134
2135/*
2136 * Dumps statistics for a proxy. The output is sent to the stream interface's
2137 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2138 * buffer space, or non-zero if everything completed. This function is used
2139 * both by the CLI and the HTTP entry points, and is able to dump the output
2140 * in HTML or CSV formats. If the later, <uri> must be NULL.
2141 */
Christopher Fauletef779222018-10-31 08:47:01 +01002142int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2143 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002144{
2145 struct appctx *appctx = __objt_appctx(si->end);
2146 struct stream *s = si_strm(si);
2147 struct channel *rep = si_ic(si);
2148 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2149 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002150
2151 chunk_reset(&trash);
2152
2153 switch (appctx->ctx.stats.px_st) {
2154 case STAT_PX_ST_INIT:
2155 /* we are on a new proxy */
2156 if (uri && uri->scope) {
2157 /* we have a limited scope, we have to check the proxy name */
2158 struct stat_scope *scope;
2159 int len;
2160
2161 len = strlen(px->id);
2162 scope = uri->scope;
2163
2164 while (scope) {
2165 /* match exact proxy name */
2166 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2167 break;
2168
2169 /* match '.' which means 'self' proxy */
2170 if (!strcmp(scope->px_id, ".") && px == s->be)
2171 break;
2172 scope = scope->next;
2173 }
2174
2175 /* proxy name not found : don't dump anything */
2176 if (scope == NULL)
2177 return 1;
2178 }
2179
2180 /* if the user has requested a limited output and the proxy
2181 * name does not match, skip it.
2182 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002183 if (appctx->ctx.stats.scope_len) {
2184 const char *scope_ptr = stats_scope_ptr(appctx, si);
2185
2186 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2187 return 1;
2188 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002189
2190 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2191 (appctx->ctx.stats.iid != -1) &&
2192 (px->uuid != appctx->ctx.stats.iid))
2193 return 1;
2194
2195 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2196 /* fall through */
2197
2198 case STAT_PX_ST_TH:
2199 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002200 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002201 if (!stats_putchk(rep, htx, &trash))
2202 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002203 }
2204
2205 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2206 /* fall through */
2207
2208 case STAT_PX_ST_FE:
2209 /* print the frontend */
2210 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002211 if (!stats_putchk(rep, htx, &trash))
2212 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002213 }
2214
2215 appctx->ctx.stats.l = px->conf.listeners.n;
2216 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2217 /* fall through */
2218
2219 case STAT_PX_ST_LI:
2220 /* stats.l has been initialized above */
2221 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002222 if (htx) {
2223 if (htx_almost_full(htx))
2224 goto full;
2225 }
2226 else {
2227 if (buffer_almost_full(&rep->buf))
2228 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002229 }
2230
2231 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2232 if (!l->counters)
2233 continue;
2234
2235 if (appctx->ctx.stats.flags & STAT_BOUND) {
2236 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2237 break;
2238
2239 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2240 continue;
2241 }
2242
2243 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002244 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002245 if (!stats_putchk(rep, htx, &trash))
2246 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002247 }
2248 }
2249
2250 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2251 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2252 /* fall through */
2253
2254 case STAT_PX_ST_SV:
2255 /* stats.sv has been initialized above */
2256 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002257 if (htx) {
2258 if (htx_almost_full(htx))
2259 goto full;
2260 }
2261 else {
2262 if (buffer_almost_full(&rep->buf))
2263 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002264 }
2265
2266 sv = appctx->ctx.stats.sv;
2267
2268 if (appctx->ctx.stats.flags & STAT_BOUND) {
2269 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2270 break;
2271
2272 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2273 continue;
2274 }
2275
2276 svs = sv;
2277 while (svs->track)
2278 svs = svs->track;
2279
2280 /* do not report servers which are DOWN and not changing state */
2281 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002282 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2283 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002284 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2285 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2286 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2287 continue;
2288 }
2289
Willy Tarreau43241ff2019-10-09 11:27:51 +02002290 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002291 if (!stats_putchk(rep, htx, &trash))
2292 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002293 }
2294 } /* for sv */
2295
2296 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2297 /* fall through */
2298
2299 case STAT_PX_ST_BE:
2300 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002301 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002302 if (!stats_putchk(rep, htx, &trash))
2303 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002304 }
2305
2306 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2307 /* fall through */
2308
2309 case STAT_PX_ST_END:
2310 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2311 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002312 if (!stats_putchk(rep, htx, &trash))
2313 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002314 }
2315
2316 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2317 /* fall through */
2318
2319 case STAT_PX_ST_FIN:
2320 return 1;
2321
2322 default:
2323 /* unknown state, we should put an abort() here ! */
2324 return 1;
2325 }
Christopher Fauletef779222018-10-31 08:47:01 +01002326
2327 full:
2328 si_rx_room_blk(si);
2329 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002330}
2331
2332/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2333 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2334 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002335static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002336{
2337 /* WARNING! This must fit in the first buffer !!! */
2338 chunk_appendf(&trash,
2339 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2340 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2341 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03002342 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002343 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2344 "<style type=\"text/css\"><!--\n"
2345 "body {"
2346 " font-family: arial, helvetica, sans-serif;"
2347 " font-size: 12px;"
2348 " font-weight: normal;"
2349 " color: black;"
2350 " background: white;"
2351 "}\n"
2352 "th,td {"
2353 " font-size: 10px;"
2354 "}\n"
2355 "h1 {"
2356 " font-size: x-large;"
2357 " margin-bottom: 0.5em;"
2358 "}\n"
2359 "h2 {"
2360 " font-family: helvetica, arial;"
2361 " font-size: x-large;"
2362 " font-weight: bold;"
2363 " font-style: italic;"
2364 " color: #6020a0;"
2365 " margin-top: 0em;"
2366 " margin-bottom: 0em;"
2367 "}\n"
2368 "h3 {"
2369 " font-family: helvetica, arial;"
2370 " font-size: 16px;"
2371 " font-weight: bold;"
2372 " color: #b00040;"
2373 " background: #e8e8d0;"
2374 " margin-top: 0em;"
2375 " margin-bottom: 0em;"
2376 "}\n"
2377 "li {"
2378 " margin-top: 0.25em;"
2379 " margin-right: 2em;"
2380 "}\n"
2381 ".hr {margin-top: 0.25em;"
2382 " border-color: black;"
2383 " border-bottom-style: solid;"
2384 "}\n"
2385 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2386 ".total {background: #20D0D0;color: #ffff80;}\n"
2387 ".frontend {background: #e8e8d0;}\n"
2388 ".socket {background: #d0d0d0;}\n"
2389 ".backend {background: #e8e8d0;}\n"
2390 ".active_down {background: #ff9090;}\n"
2391 ".active_going_up {background: #ffd020;}\n"
2392 ".active_going_down {background: #ffffa0;}\n"
2393 ".active_up {background: #c0ffc0;}\n"
2394 ".active_nolb {background: #20a0ff;}\n"
2395 ".active_draining {background: #20a0FF;}\n"
2396 ".active_no_check {background: #e0e0e0;}\n"
2397 ".backup_down {background: #ff9090;}\n"
2398 ".backup_going_up {background: #ff80ff;}\n"
2399 ".backup_going_down {background: #c060ff;}\n"
2400 ".backup_up {background: #b0d0ff;}\n"
2401 ".backup_nolb {background: #90b0e0;}\n"
2402 ".backup_draining {background: #cc9900;}\n"
2403 ".backup_no_check {background: #e0e0e0;}\n"
2404 ".maintain {background: #c07820;}\n"
2405 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2406 "\n"
2407 "a.px:link {color: #ffff40; text-decoration: none;}"
2408 "a.px:visited {color: #ffff40; text-decoration: none;}"
2409 "a.px:hover {color: #ffffff; text-decoration: none;}"
2410 "a.lfsb:link {color: #000000; text-decoration: none;}"
2411 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2412 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2413 "\n"
2414 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2415 "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"
2416 "table.tbl td.ac { text-align: center;}\n"
2417 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2418 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2419 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2420 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2421 "\n"
2422 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2423 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2424 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2425 "table.det { border-collapse: collapse; border-style: none; }\n"
2426 "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"
2427 "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"
2428 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2429 "div.tips {\n"
2430 " display:block;\n"
2431 " visibility:hidden;\n"
2432 " z-index:2147483647;\n"
2433 " position:absolute;\n"
2434 " padding:2px 4px 3px;\n"
2435 " background:#f0f060; color:#000000;\n"
2436 " border:1px solid #7040c0;\n"
2437 " white-space:nowrap;\n"
2438 " font-style:normal;font-size:11px;font-weight:normal;\n"
2439 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2440 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2441 "}\n"
2442 "u:hover div.tips {visibility:visible;}\n"
2443 "-->\n"
2444 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002445 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002446 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002447 );
2448}
2449
2450/* Dumps the HTML stats information block to the trash for and uses the state from
2451 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2452 * for clearing the trash if needed.
2453 */
2454static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2455{
2456 struct appctx *appctx = __objt_appctx(si->end);
2457 unsigned int up = (now.tv_sec - start_date.tv_sec);
2458 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002459 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002460 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2461
2462 /* Turn the bytes per second to bits per second and take care of the
2463 * usual ethernet overhead in order to help figure how far we are from
2464 * interface saturation since it's the only case which usually matters.
2465 * For this we count the total size of an Ethernet frame on the wire
2466 * including preamble and IFG (1538) for the largest TCP segment it
2467 * transports (1448 with TCP timestamps). This is not valid for smaller
2468 * packets (under-estimated), but it gives a reasonably accurate
2469 * estimation of how far we are from uplink saturation.
2470 */
2471 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002472
2473 /* WARNING! this has to fit the first packet too.
2474 * We are around 3.5 kB, add adding entries will
2475 * become tricky if we want to support 4kB buffers !
2476 */
2477 chunk_appendf(&trash,
2478 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2479 PRODUCT_NAME "%s</a></h1>\n"
2480 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2481 "<hr width=\"100%%\" class=\"hr\">\n"
2482 "<h3>&gt; General process information</h3>\n"
2483 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002484 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002485 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2486 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2487 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002488 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002489 "Running tasks: %d/%d; idle = %d %%<br>\n"
2490 "</td><td align=\"center\" nowrap>\n"
2491 "<table class=\"lgd\"><tr>\n"
2492 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2493 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2494 "</tr><tr>\n"
2495 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2496 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2497 "</tr><tr>\n"
2498 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2499 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2500 "</tr><tr>\n"
2501 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2502 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2503 "</tr><tr>\n"
2504 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2505 "</tr><tr>\n"
2506 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2507 "</tr></table>\n"
2508 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2509 "</td>"
2510 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2511 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2512 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002513 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2514 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2515 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2516 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2517 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002518 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002519 up / 86400, (up % 86400) / 3600,
2520 (up % 3600) / 60, (up % 60),
2521 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2522 global.rlimit_memmax ? " MB" : "",
2523 global.rlimit_nofile,
2524 global.maxsock, global.maxconn, global.maxpipes,
2525 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002526 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2527 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002528 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002529 );
2530
2531 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002532 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002533 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2534
2535 chunk_appendf(&trash,
2536 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2537 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2538 STAT_SCOPE_TXT_MAXLEN);
2539
2540 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2541 scope_txt[0] = 0;
2542 if (appctx->ctx.stats.scope_len) {
2543 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002544 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002545 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2546 }
2547
2548 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2549 chunk_appendf(&trash,
2550 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2551 uri->uri_prefix,
2552 "",
2553 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2554 scope_txt);
2555 else
2556 chunk_appendf(&trash,
2557 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2558 uri->uri_prefix,
2559 ";up",
2560 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2561 scope_txt);
2562
2563 if (uri->refresh > 0) {
2564 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2565 chunk_appendf(&trash,
2566 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2567 uri->uri_prefix,
2568 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2569 "",
2570 scope_txt);
2571 else
2572 chunk_appendf(&trash,
2573 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2574 uri->uri_prefix,
2575 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2576 ";norefresh",
2577 scope_txt);
2578 }
2579
2580 chunk_appendf(&trash,
2581 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2582 uri->uri_prefix,
2583 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2584 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2585 scope_txt);
2586
2587 chunk_appendf(&trash,
2588 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2589 uri->uri_prefix,
2590 (uri->refresh > 0) ? ";norefresh" : "",
2591 scope_txt);
2592
2593 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002594 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2595 uri->uri_prefix,
2596 (uri->refresh > 0) ? ";norefresh" : "",
2597 scope_txt, uri->uri_prefix);
2598
2599 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002600 "</ul></td>"
2601 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2602 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2603 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2604 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2605 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2606 "</ul>"
2607 "</td>"
2608 "</tr></table>\n"
2609 ""
2610 );
2611
2612 if (appctx->ctx.stats.st_code) {
2613 switch (appctx->ctx.stats.st_code) {
2614 case STAT_STATUS_DONE:
2615 chunk_appendf(&trash,
2616 "<p><div class=active_up>"
2617 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2618 "Action processed successfully."
2619 "</div>\n", uri->uri_prefix,
2620 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2621 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2622 scope_txt);
2623 break;
2624 case STAT_STATUS_NONE:
2625 chunk_appendf(&trash,
2626 "<p><div class=active_going_down>"
2627 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2628 "Nothing has changed."
2629 "</div>\n", uri->uri_prefix,
2630 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2631 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2632 scope_txt);
2633 break;
2634 case STAT_STATUS_PART:
2635 chunk_appendf(&trash,
2636 "<p><div class=active_going_down>"
2637 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2638 "Action partially processed.<br>"
2639 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2640 "</div>\n", uri->uri_prefix,
2641 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2642 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2643 scope_txt);
2644 break;
2645 case STAT_STATUS_ERRP:
2646 chunk_appendf(&trash,
2647 "<p><div class=active_down>"
2648 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2649 "Action not processed because of invalid parameters."
2650 "<ul>"
2651 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002652 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002653 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2654 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2655 "</ul>"
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_EXCD:
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 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2666 "You should retry with less servers at a time.</b>"
2667 "</div>\n", uri->uri_prefix,
2668 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2669 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2670 scope_txt);
2671 break;
2672 case STAT_STATUS_DENY:
2673 chunk_appendf(&trash,
2674 "<p><div class=active_down>"
2675 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2676 "<b>Action denied.</b>"
2677 "</div>\n", uri->uri_prefix,
2678 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2679 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2680 scope_txt);
2681 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002682 case STAT_STATUS_IVAL:
2683 chunk_appendf(&trash,
2684 "<p><div class=active_down>"
2685 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2686 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2687 "</div>\n", uri->uri_prefix,
2688 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2689 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2690 scope_txt);
2691 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002692 default:
2693 chunk_appendf(&trash,
2694 "<p><div class=active_no_check>"
2695 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2696 "Unexpected result."
2697 "</div>\n", uri->uri_prefix,
2698 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2699 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2700 scope_txt);
2701 }
2702 chunk_appendf(&trash, "<p>\n");
2703 }
2704}
2705
2706/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2707 * for clearing the trash if needed.
2708 */
2709static void stats_dump_html_end()
2710{
2711 chunk_appendf(&trash, "</body></html>\n");
2712}
2713
Simon Horman05ee2132017-01-04 09:37:25 +01002714/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2715 * for clearing it if needed.
2716 */
2717static void stats_dump_json_header()
2718{
2719 chunk_strcat(&trash, "[");
2720}
2721
2722
2723/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2724 * for clearing the trash if needed.
2725 */
2726static void stats_dump_json_end()
2727{
2728 chunk_strcat(&trash, "]");
2729}
2730
William Lallemand74c24fb2016-11-21 17:18:36 +01002731/* This function dumps statistics onto the stream interface's read buffer in
2732 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2733 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2734 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2735 * and the stream must be closed, or -1 in case of any error. This function is
2736 * used by both the CLI and the HTTP handlers.
2737 */
Christopher Fauletef779222018-10-31 08:47:01 +01002738static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2739 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002740{
2741 struct appctx *appctx = __objt_appctx(si->end);
2742 struct channel *rep = si_ic(si);
2743 struct proxy *px;
2744
2745 chunk_reset(&trash);
2746
2747 switch (appctx->st2) {
2748 case STAT_ST_INIT:
2749 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2750 /* fall through */
2751
2752 case STAT_ST_HEAD:
2753 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002754 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002755 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2756 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002757 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002758 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002759 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2760 stats_dump_csv_header();
2761
Christopher Fauletef779222018-10-31 08:47:01 +01002762 if (!stats_putchk(rep, htx, &trash))
2763 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002764
Christopher Faulet6338a082019-09-09 15:50:54 +02002765 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2766 appctx->st2 = STAT_ST_FIN;
2767 return 1;
2768 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002769 appctx->st2 = STAT_ST_INFO;
2770 /* fall through */
2771
2772 case STAT_ST_INFO:
2773 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2774 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002775 if (!stats_putchk(rep, htx, &trash))
2776 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002777 }
2778
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002779 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002780 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2781 appctx->st2 = STAT_ST_LIST;
2782 /* fall through */
2783
2784 case STAT_ST_LIST:
2785 /* dump proxies */
2786 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002787 if (htx) {
2788 if (htx_almost_full(htx))
2789 goto full;
2790 }
2791 else {
2792 if (buffer_almost_full(&rep->buf))
2793 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002794 }
2795
2796 px = appctx->ctx.stats.px;
2797 /* skip the disabled proxies, global frontend and non-networked ones */
2798 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002799 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002800 return 0;
2801
2802 appctx->ctx.stats.px = px->next;
2803 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2804 }
2805 /* here, we just have reached the last proxy */
2806
2807 appctx->st2 = STAT_ST_END;
2808 /* fall through */
2809
2810 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002811 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2812 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2813 stats_dump_html_end();
2814 else
2815 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002816 if (!stats_putchk(rep, htx, &trash))
2817 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002818 }
2819
2820 appctx->st2 = STAT_ST_FIN;
2821 /* fall through */
2822
2823 case STAT_ST_FIN:
2824 return 1;
2825
2826 default:
2827 /* unknown state ! */
2828 appctx->st2 = STAT_ST_FIN;
2829 return -1;
2830 }
Christopher Fauletef779222018-10-31 08:47:01 +01002831
2832 full:
2833 si_rx_room_blk(si);
2834 return 0;
2835
William Lallemand74c24fb2016-11-21 17:18:36 +01002836}
2837
2838/* We reached the stats page through a POST request. The appctx is
2839 * expected to have already been allocated by the caller.
2840 * Parse the posted data and enable/disable servers if necessary.
2841 * Returns 1 if request was parsed or zero if it needs more data.
2842 */
2843static int stats_process_http_post(struct stream_interface *si)
2844{
2845 struct stream *s = si_strm(si);
2846 struct appctx *appctx = objt_appctx(si->end);
2847
2848 struct proxy *px = NULL;
2849 struct server *sv = NULL;
2850
2851 char key[LINESIZE];
2852 int action = ST_ADM_ACTION_NONE;
2853 int reprocess = 0;
2854
2855 int total_servers = 0;
2856 int altered_servers = 0;
2857
2858 char *first_param, *cur_param, *next_param, *end_params;
2859 char *st_cur_param = NULL;
2860 char *st_next_param = NULL;
2861
Christopher Fauleta3618372018-12-13 21:59:56 +01002862 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002863
Christopher Fauletb7f88902019-07-15 21:56:43 +02002864 struct htx *htx = htxbuf(&s->req.buf);
2865 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002866
Christopher Fauletb7f88902019-07-15 21:56:43 +02002867 /* we need more data */
2868 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2869 /* check if we can receive more */
2870 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2871 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2872 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002873 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002874 goto wait;
2875 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002876
Christopher Fauletb7f88902019-07-15 21:56:43 +02002877 /* The request was fully received. Copy data */
2878 blk = htx_get_head_blk(htx);
2879 while (blk) {
2880 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002881
Christopher Fauletb7f88902019-07-15 21:56:43 +02002882 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2883 break;
2884 if (type == HTX_BLK_DATA) {
2885 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002886
Christopher Fauletb7f88902019-07-15 21:56:43 +02002887 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002888 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2889 goto out;
2890 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002891 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002892 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002893 }
2894
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002895 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002896 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002897 cur_param = next_param = end_params;
2898 *end_params = '\0';
2899
2900 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2901
2902 /*
2903 * Parse the parameters in reverse order to only store the last value.
2904 * From the html form, the backend and the action are at the end.
2905 */
2906 while (cur_param > first_param) {
2907 char *value;
2908 int poffset, plen;
2909
2910 cur_param--;
2911
2912 if ((*cur_param == '&') || (cur_param == first_param)) {
2913 reprocess_servers:
2914 /* Parse the key */
2915 poffset = (cur_param != first_param ? 1 : 0);
2916 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2917 if ((plen > 0) && (plen <= sizeof(key))) {
2918 strncpy(key, cur_param + poffset, plen);
2919 key[plen - 1] = '\0';
2920 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002921 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002922 goto out;
2923 }
2924
2925 /* Parse the value */
2926 value = key;
2927 while (*value != '\0' && *value != '=') {
2928 value++;
2929 }
2930 if (*value == '=') {
2931 /* Ok, a value is found, we can mark the end of the key */
2932 *value++ = '\0';
2933 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002934 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002935 break;
2936
2937 /* Now we can check the key to see what to do */
2938 if (!px && (strcmp(key, "b") == 0)) {
2939 if ((px = proxy_be_by_name(value)) == NULL) {
2940 /* the backend name is unknown or ambiguous (duplicate names) */
2941 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2942 goto out;
2943 }
2944 }
2945 else if (!action && (strcmp(key, "action") == 0)) {
2946 if (strcmp(value, "ready") == 0) {
2947 action = ST_ADM_ACTION_READY;
2948 }
2949 else if (strcmp(value, "drain") == 0) {
2950 action = ST_ADM_ACTION_DRAIN;
2951 }
2952 else if (strcmp(value, "maint") == 0) {
2953 action = ST_ADM_ACTION_MAINT;
2954 }
2955 else if (strcmp(value, "shutdown") == 0) {
2956 action = ST_ADM_ACTION_SHUTDOWN;
2957 }
2958 else if (strcmp(value, "dhlth") == 0) {
2959 action = ST_ADM_ACTION_DHLTH;
2960 }
2961 else if (strcmp(value, "ehlth") == 0) {
2962 action = ST_ADM_ACTION_EHLTH;
2963 }
2964 else if (strcmp(value, "hrunn") == 0) {
2965 action = ST_ADM_ACTION_HRUNN;
2966 }
2967 else if (strcmp(value, "hnolb") == 0) {
2968 action = ST_ADM_ACTION_HNOLB;
2969 }
2970 else if (strcmp(value, "hdown") == 0) {
2971 action = ST_ADM_ACTION_HDOWN;
2972 }
2973 else if (strcmp(value, "dagent") == 0) {
2974 action = ST_ADM_ACTION_DAGENT;
2975 }
2976 else if (strcmp(value, "eagent") == 0) {
2977 action = ST_ADM_ACTION_EAGENT;
2978 }
2979 else if (strcmp(value, "arunn") == 0) {
2980 action = ST_ADM_ACTION_ARUNN;
2981 }
2982 else if (strcmp(value, "adown") == 0) {
2983 action = ST_ADM_ACTION_ADOWN;
2984 }
2985 /* else these are the old supported methods */
2986 else if (strcmp(value, "disable") == 0) {
2987 action = ST_ADM_ACTION_DISABLE;
2988 }
2989 else if (strcmp(value, "enable") == 0) {
2990 action = ST_ADM_ACTION_ENABLE;
2991 }
2992 else if (strcmp(value, "stop") == 0) {
2993 action = ST_ADM_ACTION_STOP;
2994 }
2995 else if (strcmp(value, "start") == 0) {
2996 action = ST_ADM_ACTION_START;
2997 }
2998 else {
2999 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3000 goto out;
3001 }
3002 }
3003 else if (strcmp(key, "s") == 0) {
3004 if (!(px && action)) {
3005 /*
3006 * Indicates that we'll need to reprocess the parameters
3007 * as soon as backend and action are known
3008 */
3009 if (!reprocess) {
3010 st_cur_param = cur_param;
3011 st_next_param = next_param;
3012 }
3013 reprocess = 1;
3014 }
3015 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003016 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003017 switch (action) {
3018 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003019 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003020 altered_servers++;
3021 total_servers++;
3022 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3023 }
3024 break;
3025 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003026 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003027 altered_servers++;
3028 total_servers++;
3029 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3030 }
3031 break;
3032 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003033 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003034 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3035 altered_servers++;
3036 total_servers++;
3037 }
3038 break;
3039 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003040 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003041 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3042 altered_servers++;
3043 total_servers++;
3044 }
3045 break;
3046 case ST_ADM_ACTION_DHLTH:
3047 if (sv->check.state & CHK_ST_CONFIGURED) {
3048 sv->check.state &= ~CHK_ST_ENABLED;
3049 altered_servers++;
3050 total_servers++;
3051 }
3052 break;
3053 case ST_ADM_ACTION_EHLTH:
3054 if (sv->check.state & CHK_ST_CONFIGURED) {
3055 sv->check.state |= CHK_ST_ENABLED;
3056 altered_servers++;
3057 total_servers++;
3058 }
3059 break;
3060 case ST_ADM_ACTION_HRUNN:
3061 if (!(sv->track)) {
3062 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003063 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003064 altered_servers++;
3065 total_servers++;
3066 }
3067 break;
3068 case ST_ADM_ACTION_HNOLB:
3069 if (!(sv->track)) {
3070 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003071 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003072 altered_servers++;
3073 total_servers++;
3074 }
3075 break;
3076 case ST_ADM_ACTION_HDOWN:
3077 if (!(sv->track)) {
3078 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003079 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003080 altered_servers++;
3081 total_servers++;
3082 }
3083 break;
3084 case ST_ADM_ACTION_DAGENT:
3085 if (sv->agent.state & CHK_ST_CONFIGURED) {
3086 sv->agent.state &= ~CHK_ST_ENABLED;
3087 altered_servers++;
3088 total_servers++;
3089 }
3090 break;
3091 case ST_ADM_ACTION_EAGENT:
3092 if (sv->agent.state & CHK_ST_CONFIGURED) {
3093 sv->agent.state |= CHK_ST_ENABLED;
3094 altered_servers++;
3095 total_servers++;
3096 }
3097 break;
3098 case ST_ADM_ACTION_ARUNN:
3099 if (sv->agent.state & CHK_ST_ENABLED) {
3100 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003101 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003102 altered_servers++;
3103 total_servers++;
3104 }
3105 break;
3106 case ST_ADM_ACTION_ADOWN:
3107 if (sv->agent.state & CHK_ST_ENABLED) {
3108 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003109 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003110 altered_servers++;
3111 total_servers++;
3112 }
3113 break;
3114 case ST_ADM_ACTION_READY:
3115 srv_adm_set_ready(sv);
3116 altered_servers++;
3117 total_servers++;
3118 break;
3119 case ST_ADM_ACTION_DRAIN:
3120 srv_adm_set_drain(sv);
3121 altered_servers++;
3122 total_servers++;
3123 break;
3124 case ST_ADM_ACTION_MAINT:
3125 srv_adm_set_maint(sv);
3126 altered_servers++;
3127 total_servers++;
3128 break;
3129 case ST_ADM_ACTION_SHUTDOWN:
3130 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003131 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003132 altered_servers++;
3133 total_servers++;
3134 }
3135 break;
3136 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003137 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003138 } else {
3139 /* the server name is unknown or ambiguous (duplicate names) */
3140 total_servers++;
3141 }
3142 }
3143 if (reprocess && px && action) {
3144 /* Now, we know the backend and the action chosen by the user.
3145 * We can safely restart from the first server parameter
3146 * to reprocess them
3147 */
3148 cur_param = st_cur_param;
3149 next_param = st_next_param;
3150 reprocess = 0;
3151 goto reprocess_servers;
3152 }
3153
3154 next_param = cur_param;
3155 }
3156 }
3157
3158 if (total_servers == 0) {
3159 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3160 }
3161 else if (altered_servers == 0) {
3162 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3163 }
3164 else if (altered_servers == total_servers) {
3165 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3166 }
3167 else {
3168 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3169 }
3170 out:
3171 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003172 wait:
3173 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3174 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003175}
3176
3177
Christopher Fauletb7f88902019-07-15 21:56:43 +02003178static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003179{
3180 struct stream *s = si_strm(si);
3181 struct uri_auth *uri = s->be->uri_auth;
3182 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003183 struct htx_sl *sl;
3184 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003185
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003186 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);
3187 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3188 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003189 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003190 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003191
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003192 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003193 goto full;
3194 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3195 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3196 goto full;
3197 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003198 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3199 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3200 goto full;
3201 }
Christopher Fauletef779222018-10-31 08:47:01 +01003202 else {
3203 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3204 goto full;
3205 }
3206
3207 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3208 const char *refresh = U2A(uri->refresh);
3209 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3210 goto full;
3211 }
3212
3213 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3214 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3215 goto full;
3216 }
3217
3218 if (!htx_add_endof(htx, HTX_BLK_EOH))
3219 goto full;
3220
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003221 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003222 return 1;
3223
3224 full:
3225 htx_reset(htx);
3226 si_rx_room_blk(si);
3227 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003228}
3229
Christopher Fauletef779222018-10-31 08:47:01 +01003230
Christopher Fauletb7f88902019-07-15 21:56:43 +02003231static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003232{
3233 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3234 struct stream *s = si_strm(si);
3235 struct uri_auth *uri = s->be->uri_auth;
3236 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003237 struct htx_sl *sl;
3238 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003239
3240 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3241 scope_txt[0] = 0;
3242 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003243 const char *scope_ptr = stats_scope_ptr(appctx, si);
3244
Christopher Fauletef779222018-10-31 08:47:01 +01003245 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003246 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003247 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3248 }
3249
3250 /* We don't want to land on the posted stats page because a refresh will
3251 * repost the data. We don't want this to happen on accident so we redirect
3252 * the browse to the stats page with a GET.
3253 */
3254 chunk_printf(&trash, "%s;st=%s%s%s%s",
3255 uri->uri_prefix,
3256 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3257 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3258 stat_status_codes[appctx->ctx.stats.st_code]) ?
3259 stat_status_codes[appctx->ctx.stats.st_code] :
3260 stat_status_codes[STAT_STATUS_UNKN],
3261 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3262 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3263 scope_txt);
3264
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003265 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3266 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3267 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003268 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003269 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003270
3271 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003272 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3273 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3274 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3275 goto full;
3276
3277 if (!htx_add_endof(htx, HTX_BLK_EOH))
3278 goto full;
3279
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003280 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003281 return 1;
3282
3283full:
3284 htx_reset(htx);
3285 si_rx_room_blk(si);
3286 return 0;
3287}
William Lallemand74c24fb2016-11-21 17:18:36 +01003288
Simon Horman05ee2132017-01-04 09:37:25 +01003289
William Lallemand74c24fb2016-11-21 17:18:36 +01003290/* This I/O handler runs as an applet embedded in a stream interface. It is
3291 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3292 * appctx->st0 contains the operation in progress (dump, done). The handler
3293 * automatically unregisters itself once transfer is complete.
3294 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003295static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003296{
3297 struct stream_interface *si = appctx->owner;
3298 struct stream *s = si_strm(si);
3299 struct channel *req = si_oc(si);
3300 struct channel *res = si_ic(si);
3301 struct htx *req_htx, *res_htx;
3302
3303 res_htx = htx_from_buf(&res->buf);
3304
3305 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3306 goto out;
3307
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003308 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003309 if (!b_size(&res->buf)) {
3310 si_rx_room_blk(si);
3311 goto out;
3312 }
3313
3314 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003315 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3316 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003317
3318 /* all states are processed in sequence */
3319 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003320 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003321 if (s->txn->meth == HTTP_METH_HEAD)
3322 appctx->st0 = STAT_HTTP_DONE;
3323 else
3324 appctx->st0 = STAT_HTTP_DUMP;
3325 }
3326 }
3327
3328 if (appctx->st0 == STAT_HTTP_DUMP) {
3329 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3330 appctx->st0 = STAT_HTTP_DONE;
3331 }
3332
3333 if (appctx->st0 == STAT_HTTP_POST) {
3334 if (stats_process_http_post(si))
3335 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003336 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003337 appctx->st0 = STAT_HTTP_DONE;
3338 }
3339
3340 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003341 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003342 appctx->st0 = STAT_HTTP_DONE;
3343 }
3344
3345 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003346 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003347 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003348 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3349 si_rx_room_blk(si);
3350 goto out;
3351 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003352 channel_add_input(&s->res, 1);
3353 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003354 }
3355
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003356 if (appctx->st0 == STAT_HTTP_END) {
3357 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003358 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003359 si_shutr(si);
3360 }
3361
3362 /* eat the whole request */
3363 if (co_data(req)) {
3364 req_htx = htx_from_buf(&req->buf);
3365 co_htx_skip(req, req_htx, co_data(req));
3366 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003367 }
3368 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003369
Christopher Fauletef779222018-10-31 08:47:01 +01003370 out:
3371 /* we have left the request in the buffer for the case where we
3372 * process a POST, and this automatically re-enables activity on
3373 * read. It's better to indicate that we want to stop reading when
3374 * we're sending, so that we know there's at most one direction
3375 * deciding to wake the applet up. It saves it from looping when
3376 * emitting large blocks into small TCP windows.
3377 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003378 htx_to_buf(res_htx, &res->buf);
3379 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003380 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003381}
3382
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003383/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003384static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003385 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003386{
3387 int field;
3388
3389 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3390 if (!field_format(info, field))
3391 continue;
3392
Willy Tarreaueaa55372019-10-09 07:39:11 +02003393 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003394 return 0;
3395 if (!stats_emit_raw_data_field(out, &info[field]))
3396 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003397 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3398 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003399 if (!chunk_strcat(out, "\n"))
3400 return 0;
3401 }
3402 return 1;
3403}
3404
3405/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003406static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003407 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003408{
3409 int field;
3410
3411 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3412 if (!field_format(info, field))
3413 continue;
3414
Willy Tarreaueaa55372019-10-09 07:39:11 +02003415 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 +01003416 return 0;
3417 if (!stats_emit_field_tags(out, &info[field], ':'))
3418 return 0;
3419 if (!stats_emit_typed_data_field(out, &info[field]))
3420 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003421 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3422 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003423 if (!chunk_strcat(out, "\n"))
3424 return 0;
3425 }
3426 return 1;
3427}
3428
3429/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003430 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003431 * INF_TOTAL_FIELDS. If this length is less then this value, the
3432 * function returns 0, otherwise, it returns 1.
3433 */
3434int stats_fill_info(struct field *info, int len)
3435{
3436 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003437 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003438
3439#ifdef USE_OPENSSL
3440 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3441 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3442 int ssl_reuse = 0;
3443
3444 if (ssl_key_rate < ssl_sess_rate) {
3445 /* count the ssl reuse ratio and avoid overflows in both directions */
3446 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3447 }
3448#endif
3449
3450 if (len < INF_TOTAL_FIELDS)
3451 return 0;
3452
3453 chunk_reset(out);
3454 memset(info, 0, sizeof(*info) * len);
3455
3456 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003457 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3458 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003459
Yves Lafon95317282018-02-26 11:10:37 +01003460 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003461 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3462 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3463 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3464
3465 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3466 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3467
3468 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3469 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3470 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3471 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3472 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3473 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3474 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3475 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3476 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3477 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3478 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3479 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3480#ifdef USE_OPENSSL
3481 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3482 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3483 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3484#endif
3485 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3486 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3487 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3488 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3489 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3490 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3491 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3492 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3493 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3494
3495#ifdef USE_OPENSSL
3496 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3497 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3498 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3499 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3500 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3501 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3502 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3503 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3504 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3505 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3506#endif
3507 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3508 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3509 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3510#ifdef USE_ZLIB
3511 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3512 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3513#endif
3514 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003515 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003516 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003517 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3518 if (global.desc)
3519 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003520 info[INF_STOPPING] = mkf_u32(0, stopping);
3521 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003522 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003523 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003524 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003525 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003526 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003527 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003528 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003529 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003530 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003531 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 +02003532 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003533 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003534
3535 return 1;
3536}
3537
3538/* This function dumps information onto the stream interface's read buffer.
3539 * It returns 0 as long as it does not complete, non-zero upon completion.
3540 * No state is used.
3541 */
3542static int stats_dump_info_to_buffer(struct stream_interface *si)
3543{
3544 struct appctx *appctx = __objt_appctx(si->end);
3545
3546 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3547 return 0;
3548
3549 chunk_reset(&trash);
3550
3551 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003552 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003553 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003554 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003555 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003556 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003557
Willy Tarreau06d80a92017-10-19 14:32:15 +02003558 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003559 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003560 return 0;
3561 }
3562
3563 return 1;
3564}
3565
Simon Horman6f6bb382017-01-04 09:37:26 +01003566/* This function dumps the schema onto the stream interface's read buffer.
3567 * It returns 0 as long as it does not complete, non-zero upon completion.
3568 * No state is used.
3569 *
3570 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3571 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3572 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003573static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003574{
3575
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003576 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003577
3578 chunk_strcat(out,
3579 "{"
3580 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3581 "\"oneOf\":["
3582 "{"
3583 "\"title\":\"Info\","
3584 "\"type\":\"array\","
3585 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003586 "\"title\":\"InfoItem\","
3587 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003588 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003589 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3590 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3591 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3592 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3593 "},"
3594 "\"required\":[\"field\",\"processNum\",\"tags\","
3595 "\"value\"]"
3596 "}"
3597 "},"
3598 "{"
3599 "\"title\":\"Stat\","
3600 "\"type\":\"array\","
3601 "\"items\":{"
3602 "\"title\":\"InfoItem\","
3603 "\"type\":\"object\","
3604 "\"properties\":{"
3605 "\"objType\":{"
3606 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3607 "\"Server\",\"Unknown\"]"
3608 "},"
3609 "\"proxyId\":{"
3610 "\"type\":\"integer\","
3611 "\"minimum\":0"
3612 "},"
3613 "\"id\":{"
3614 "\"type\":\"integer\","
3615 "\"minimum\":0"
3616 "},"
3617 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3618 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3619 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3620 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3621 "},"
3622 "\"required\":[\"objType\",\"proxyId\",\"id\","
3623 "\"field\",\"processNum\",\"tags\","
3624 "\"value\"]"
3625 "}"
3626 "},"
3627 "{"
3628 "\"title\":\"Error\","
3629 "\"type\":\"object\","
3630 "\"properties\":{"
3631 "\"errorStr\":{"
3632 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003633 "}"
3634 "},"
3635 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01003636 "}"
3637 "],"
3638 "\"definitions\":{"
3639 "\"field\":{"
3640 "\"type\":\"object\","
3641 "\"pos\":{"
3642 "\"type\":\"integer\","
3643 "\"minimum\":0"
3644 "},"
3645 "\"name\":{"
3646 "\"type\":\"string\""
3647 "},"
3648 "\"required\":[\"pos\",\"name\"]"
3649 "},"
3650 "\"processNum\":{"
3651 "\"type\":\"integer\","
3652 "\"minimum\":1"
3653 "},"
3654 "\"tags\":{"
3655 "\"type\":\"object\","
3656 "\"origin\":{"
3657 "\"type\":\"string\","
3658 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3659 "\"Config\",\"Product\",\"Unknown\"]"
3660 "},"
3661 "\"nature\":{"
3662 "\"type\":\"string\","
3663 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3664 "\"Rate\",\"Counter\",\"Duration\","
3665 "\"Age\",\"Time\",\"Name\",\"Output\","
3666 "\"Avg\", \"Unknown\"]"
3667 "},"
3668 "\"scope\":{"
3669 "\"type\":\"string\","
3670 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3671 "\"System\",\"Unknown\"]"
3672 "},"
3673 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3674 "},"
3675 "\"typedValue\":{"
3676 "\"type\":\"object\","
3677 "\"oneOf\":["
3678 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3679 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3680 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3681 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3682 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3683 "],"
3684 "\"definitions\":{"
3685 "\"s32Value\":{"
3686 "\"properties\":{"
3687 "\"type\":{"
3688 "\"type\":\"string\","
3689 "\"enum\":[\"s32\"]"
3690 "},"
3691 "\"value\":{"
3692 "\"type\":\"integer\","
3693 "\"minimum\":-2147483648,"
3694 "\"maximum\":2147483647"
3695 "}"
3696 "},"
3697 "\"required\":[\"type\",\"value\"]"
3698 "},"
3699 "\"s64Value\":{"
3700 "\"properties\":{"
3701 "\"type\":{"
3702 "\"type\":\"string\","
3703 "\"enum\":[\"s64\"]"
3704 "},"
3705 "\"value\":{"
3706 "\"type\":\"integer\","
3707 "\"minimum\":-9007199254740991,"
3708 "\"maximum\":9007199254740991"
3709 "}"
3710 "},"
3711 "\"required\":[\"type\",\"value\"]"
3712 "},"
3713 "\"u32Value\":{"
3714 "\"properties\":{"
3715 "\"type\":{"
3716 "\"type\":\"string\","
3717 "\"enum\":[\"u32\"]"
3718 "},"
3719 "\"value\":{"
3720 "\"type\":\"integer\","
3721 "\"minimum\":0,"
3722 "\"maximum\":4294967295"
3723 "}"
3724 "},"
3725 "\"required\":[\"type\",\"value\"]"
3726 "},"
3727 "\"u64Value\":{"
3728 "\"properties\":{"
3729 "\"type\":{"
3730 "\"type\":\"string\","
3731 "\"enum\":[\"u64\"]"
3732 "},"
3733 "\"value\":{"
3734 "\"type\":\"integer\","
3735 "\"minimum\":0,"
3736 "\"maximum\":9007199254740991"
3737 "}"
3738 "},"
3739 "\"required\":[\"type\",\"value\"]"
3740 "},"
3741 "\"strValue\":{"
3742 "\"properties\":{"
3743 "\"type\":{"
3744 "\"type\":\"string\","
3745 "\"enum\":[\"str\"]"
3746 "},"
3747 "\"value\":{\"type\":\"string\"}"
3748 "},"
3749 "\"required\":[\"type\",\"value\"]"
3750 "},"
3751 "\"unknownValue\":{"
3752 "\"properties\":{"
3753 "\"type\":{"
3754 "\"type\":\"integer\","
3755 "\"minimum\":0"
3756 "},"
3757 "\"value\":{"
3758 "\"type\":\"string\","
3759 "\"enum\":[\"unknown\"]"
3760 "}"
3761 "},"
3762 "\"required\":[\"type\",\"value\"]"
3763 "}"
3764 "}"
3765 "}"
3766 "}"
3767 "}");
3768
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003769 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003770 chunk_reset(out);
3771 chunk_appendf(out,
3772 "{\"errorStr\":\"output buffer too short\"}");
3773 }
3774}
3775
3776/* This function dumps the schema onto the stream interface's read buffer.
3777 * It returns 0 as long as it does not complete, non-zero upon completion.
3778 * No state is used.
3779 */
3780static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3781{
3782 chunk_reset(&trash);
3783
3784 stats_dump_json_schema(&trash);
3785
Willy Tarreau06d80a92017-10-19 14:32:15 +02003786 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003787 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003788 return 0;
3789 }
3790
3791 return 1;
3792}
3793
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003794static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003795{
3796 struct proxy *px;
3797 struct server *sv;
3798 struct listener *li;
3799 int clrall = 0;
3800
3801 if (strcmp(args[2], "all") == 0)
3802 clrall = 1;
3803
3804 /* check permissions */
3805 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3806 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3807 return 1;
3808
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003809 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003810 if (clrall) {
3811 memset(&px->be_counters, 0, sizeof(px->be_counters));
3812 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3813 }
3814 else {
3815 px->be_counters.conn_max = 0;
3816 px->be_counters.p.http.rps_max = 0;
3817 px->be_counters.sps_max = 0;
3818 px->be_counters.cps_max = 0;
3819 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003820 px->be_counters.qtime_max = 0;
3821 px->be_counters.ctime_max = 0;
3822 px->be_counters.dtime_max = 0;
3823 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003824
3825 px->fe_counters.conn_max = 0;
3826 px->fe_counters.p.http.rps_max = 0;
3827 px->fe_counters.sps_max = 0;
3828 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003829 }
3830
3831 for (sv = px->srv; sv; sv = sv->next)
3832 if (clrall)
3833 memset(&sv->counters, 0, sizeof(sv->counters));
3834 else {
3835 sv->counters.cur_sess_max = 0;
3836 sv->counters.nbpend_max = 0;
3837 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003838 sv->counters.qtime_max = 0;
3839 sv->counters.ctime_max = 0;
3840 sv->counters.dtime_max = 0;
3841 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003842 }
3843
3844 list_for_each_entry(li, &px->conf.listeners, by_fe)
3845 if (li->counters) {
3846 if (clrall)
3847 memset(li->counters, 0, sizeof(*li->counters));
3848 else
3849 li->counters->conn_max = 0;
3850 }
3851 }
3852
3853 global.cps_max = 0;
3854 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003855 global.ssl_max = 0;
3856 global.ssl_fe_keys_max = 0;
3857 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003858
3859 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003860 return 1;
3861}
3862
3863
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003864static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003865{
Willy Tarreau2f397382019-10-09 11:43:59 +02003866 int arg = 2;
3867
Willy Tarreaud25fc792016-12-16 12:33:47 +01003868 appctx->ctx.stats.scope_str = 0;
3869 appctx->ctx.stats.scope_len = 0;
3870 appctx->ctx.stats.flags = 0;
3871
Willy Tarreau2f397382019-10-09 11:43:59 +02003872 while (*args[arg]) {
3873 if (strcmp(args[arg], "typed") == 0)
3874 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3875 else if (strcmp(args[arg], "json") == 0)
3876 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3877 else if (strcmp(args[arg], "desc") == 0)
3878 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3879 arg++;
3880 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003881 return 0;
3882}
3883
3884
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003885static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003886{
Willy Tarreau2f397382019-10-09 11:43:59 +02003887 int arg = 2;
3888
Willy Tarreaud25fc792016-12-16 12:33:47 +01003889 appctx->ctx.stats.scope_str = 0;
3890 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003891 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3892
3893 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3894 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003895
Willy Tarreau2f397382019-10-09 11:43:59 +02003896 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003897 struct proxy *px;
3898
Willy Tarreau2f397382019-10-09 11:43:59 +02003899 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003900 if (px)
3901 appctx->ctx.stats.iid = px->uuid;
3902 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003903 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003904
Willy Tarreau9d008692019-08-09 11:21:01 +02003905 if (!appctx->ctx.stats.iid)
3906 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003907
Willy Tarreau2b812e22016-11-22 16:18:05 +01003908 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003909 appctx->ctx.stats.type = atoi(args[arg+1]);
3910 appctx->ctx.stats.sid = atoi(args[arg+2]);
3911 arg += 3;
3912 }
3913
3914 while (*args[arg]) {
3915 if (strcmp(args[arg], "typed") == 0)
3916 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3917 else if (strcmp(args[arg], "json") == 0)
3918 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3919 else if (strcmp(args[arg], "desc") == 0)
3920 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3921 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003922 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003923
Willy Tarreau2b812e22016-11-22 16:18:05 +01003924 return 0;
3925}
3926
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003927static int cli_io_handler_dump_info(struct appctx *appctx)
3928{
3929 return stats_dump_info_to_buffer(appctx->owner);
3930}
3931
Willy Tarreau2b812e22016-11-22 16:18:05 +01003932/* This I/O handler runs as an applet embedded in a stream interface. It is
3933 * used to send raw stats over a socket.
3934 */
3935static int cli_io_handler_dump_stat(struct appctx *appctx)
3936{
Christopher Fauletef779222018-10-31 08:47:01 +01003937 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003938}
3939
Simon Horman6f6bb382017-01-04 09:37:26 +01003940static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3941{
3942 return stats_dump_json_schema_to_buffer(appctx->owner);
3943}
3944
Willy Tarreau2b812e22016-11-22 16:18:05 +01003945/* register cli keywords */
3946static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003947 { { "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 +02003948 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3949 { { "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 +01003950 { { "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 +01003951 {{},}
3952}};
3953
Willy Tarreau0108d902018-11-25 19:14:37 +01003954INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3955
William Lallemand74c24fb2016-11-21 17:18:36 +01003956struct applet http_stats_applet = {
3957 .obj_type = OBJ_TYPE_APPLET,
3958 .name = "<STATS>", /* used for logging */
3959 .fct = http_stats_io_handler,
3960 .release = NULL,
3961};
3962
William Lallemand74c24fb2016-11-21 17:18:36 +01003963/*
3964 * Local variables:
3965 * c-indent-level: 8
3966 * c-basic-offset: 8
3967 * End:
3968 */