blob: 1a453c39849a67f3ccb0091078fef4ae4a6eee23 [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" },
147 [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 +0200148 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100149};
150
Willy Tarreaueaa55372019-10-09 07:39:11 +0200151const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200152 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
153 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
154 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
155 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
156 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
157 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
158 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
159 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
160 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
161 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
162 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
163 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
164 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
165 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
166 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
167 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
168 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
169 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
170 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
171 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
172 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
173 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
174 [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" },
175 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
176 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
177 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
178 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
179 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
180 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
181 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
182 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
183 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
184 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
185 [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)" },
186 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
187 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
188 [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" },
189 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
190 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
191 [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" },
192 [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" },
193 [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" },
194 [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" },
195 [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" },
196 [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)" },
197 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
198 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
199 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
200 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
201 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
202 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
203 [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" },
204 [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" },
205 [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)" },
206 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
207 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
208 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
209 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
210 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
211 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
212 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
213 [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)" },
214 [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" },
215 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
216 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
217 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
218 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
219 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
220 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
221 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
222 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
223 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
224 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
225 [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" },
226 [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" },
227 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
228 [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" },
229 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
230 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
231 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
232 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
233 [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" },
234 [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" },
235 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
236 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
237 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
238 [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" },
239 [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" },
240 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
241 [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 +0100242 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
243 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
244 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
245 [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 +0100246 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200247 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
248 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
249 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200250 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100251};
252
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100253/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100254static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100255/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100256static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100257
Christopher Faulet6338a082019-09-09 15:50:54 +0200258static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100259
Christopher Fauletef779222018-10-31 08:47:01 +0100260static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
261{
262 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100263 if (chk->data >= channel_htx_recv_max(chn, htx))
264 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200265 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100266 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100267 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100268 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100269 }
270 else {
271 if (ci_putchk(chn, chk) == -1)
272 return 0;
273 }
274 return 1;
275}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100276
Christopher Fauleted7a0662019-01-14 11:07:34 +0100277static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
278{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200279 struct channel *req = si_oc(si);
280 struct htx *htx = htxbuf(&req->buf);
281 struct htx_blk *blk;
282 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100283
Christopher Fauletb7f88902019-07-15 21:56:43 +0200284 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100285 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200286 ALREADY_CHECKED(blk);
287 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
288 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100289}
290
William Lallemand74c24fb2016-11-21 17:18:36 +0100291/*
292 * http_stats_io_handler()
293 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
294 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100295 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100296 * -> stats_dump_html_head() // emits the HTML headers
297 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
298 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
299 * -> stats_dump_html_px_hdr()
300 * -> stats_dump_fe_stats()
301 * -> stats_dump_li_stats()
302 * -> stats_dump_sv_stats()
303 * -> stats_dump_be_stats()
304 * -> stats_dump_html_px_end()
305 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100306 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100307 */
308
309
William Lallemand74c24fb2016-11-21 17:18:36 +0100310/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
311 * for clearing it if needed.
312 * NOTE: Some tools happen to rely on the field position instead of its name,
313 * so please only append new fields at the end, never in the middle.
314 */
315static void stats_dump_csv_header()
316{
317 int field;
318
319 chunk_appendf(&trash, "# ");
320 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200321 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100322
323 chunk_appendf(&trash, "\n");
324}
325
326
327/* Emits a stats field without any surrounding element and properly encoded to
328 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
329 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200330int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100331{
332 switch (field_format(f, 0)) {
333 case FF_EMPTY: return 1;
334 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
335 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
336 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
337 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200338 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100339 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
340 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
341 }
342}
343
344/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
345 * output is supposed to be used on its own line. Returns non-zero on success, 0
346 * if the buffer is full.
347 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200348int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100349{
350 switch (field_format(f, 0)) {
351 case FF_EMPTY: return 1;
352 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
353 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
354 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
355 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200356 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100357 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
358 default: return chunk_appendf(out, "%08x:?", f->type);
359 }
360}
361
Simon Horman05ee2132017-01-04 09:37:25 +0100362/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
363 * the recommendation for interoperable integers in section 6 of RFC 7159.
364 */
365#define JSON_INT_MAX ((1ULL << 53) - 1)
366#define JSON_INT_MIN (0 - JSON_INT_MAX)
367
368/* Emits a stats field value and its type in JSON.
369 * Returns non-zero on success, 0 on error.
370 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200371int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100372{
373 int old_len;
374 char buf[20];
375 const char *type, *value = buf, *quote = "";
376
377 switch (field_format(f, 0)) {
378 case FF_EMPTY: return 1;
379 case FF_S32: type = "\"s32\"";
380 snprintf(buf, sizeof(buf), "%d", f->u.s32);
381 break;
382 case FF_U32: type = "\"u32\"";
383 snprintf(buf, sizeof(buf), "%u", f->u.u32);
384 break;
385 case FF_S64: type = "\"s64\"";
386 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
387 return 0;
388 type = "\"s64\"";
389 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
390 break;
391 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
392 return 0;
393 type = "\"u64\"";
394 snprintf(buf, sizeof(buf), "%llu",
395 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200396 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200397 case FF_FLT: type = "\"flt\"";
398 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100399 break;
400 case FF_STR: type = "\"str\"";
401 value = field_str(f, 0);
402 quote = "\"";
403 break;
404 default: snprintf(buf, sizeof(buf), "%u", f->type);
405 type = buf;
406 value = "unknown";
407 quote = "\"";
408 break;
409 }
410
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200411 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100412 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
413 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200414 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100415}
416
William Lallemand74c24fb2016-11-21 17:18:36 +0100417/* Emits an encoding of the field type on 3 characters followed by a delimiter.
418 * Returns non-zero on success, 0 if the buffer is full.
419 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200420int stats_emit_field_tags(struct buffer *out, const struct field *f,
421 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100422{
423 char origin, nature, scope;
424
425 switch (field_origin(f, 0)) {
426 case FO_METRIC: origin = 'M'; break;
427 case FO_STATUS: origin = 'S'; break;
428 case FO_KEY: origin = 'K'; break;
429 case FO_CONFIG: origin = 'C'; break;
430 case FO_PRODUCT: origin = 'P'; break;
431 default: origin = '?'; break;
432 }
433
434 switch (field_nature(f, 0)) {
435 case FN_GAUGE: nature = 'G'; break;
436 case FN_LIMIT: nature = 'L'; break;
437 case FN_MIN: nature = 'm'; break;
438 case FN_MAX: nature = 'M'; break;
439 case FN_RATE: nature = 'R'; break;
440 case FN_COUNTER: nature = 'C'; break;
441 case FN_DURATION: nature = 'D'; break;
442 case FN_AGE: nature = 'A'; break;
443 case FN_TIME: nature = 'T'; break;
444 case FN_NAME: nature = 'N'; break;
445 case FN_OUTPUT: nature = 'O'; break;
446 case FN_AVG: nature = 'a'; break;
447 default: nature = '?'; break;
448 }
449
450 switch (field_scope(f, 0)) {
451 case FS_PROCESS: scope = 'P'; break;
452 case FS_SERVICE: scope = 'S'; break;
453 case FS_SYSTEM: scope = 's'; break;
454 case FS_CLUSTER: scope = 'C'; break;
455 default: scope = '?'; break;
456 }
457
458 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
459}
460
Simon Horman05ee2132017-01-04 09:37:25 +0100461/* Emits an encoding of the field type as JSON.
462 * Returns non-zero on success, 0 if the buffer is full.
463 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200464int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100465{
466 const char *origin, *nature, *scope;
467 int old_len;
468
469 switch (field_origin(f, 0)) {
470 case FO_METRIC: origin = "Metric"; break;
471 case FO_STATUS: origin = "Status"; break;
472 case FO_KEY: origin = "Key"; break;
473 case FO_CONFIG: origin = "Config"; break;
474 case FO_PRODUCT: origin = "Product"; break;
475 default: origin = "Unknown"; break;
476 }
477
478 switch (field_nature(f, 0)) {
479 case FN_GAUGE: nature = "Gauge"; break;
480 case FN_LIMIT: nature = "Limit"; break;
481 case FN_MIN: nature = "Min"; break;
482 case FN_MAX: nature = "Max"; break;
483 case FN_RATE: nature = "Rate"; break;
484 case FN_COUNTER: nature = "Counter"; break;
485 case FN_DURATION: nature = "Duration"; break;
486 case FN_AGE: nature = "Age"; break;
487 case FN_TIME: nature = "Time"; break;
488 case FN_NAME: nature = "Name"; break;
489 case FN_OUTPUT: nature = "Output"; break;
490 case FN_AVG: nature = "Avg"; break;
491 default: nature = "Unknown"; break;
492 }
493
494 switch (field_scope(f, 0)) {
495 case FS_PROCESS: scope = "Process"; break;
496 case FS_SERVICE: scope = "Service"; break;
497 case FS_SYSTEM: scope = "System"; break;
498 case FS_CLUSTER: scope = "Cluster"; break;
499 default: scope = "Unknown"; break;
500 }
501
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200502 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100503 chunk_appendf(out, "\"tags\":{"
504 "\"origin\":\"%s\","
505 "\"nature\":\"%s\","
506 "\"scope\":\"%s\""
507 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200508 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100509}
William Lallemand74c24fb2016-11-21 17:18:36 +0100510
511/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200512static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200513 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100514{
515 int field;
516
517 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
518 if (!stats_emit_raw_data_field(out, &stats[field]))
519 return 0;
520 if (!chunk_strcat(out, ","))
521 return 0;
522 }
523 chunk_strcat(out, "\n");
524 return 1;
525}
526
527/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200528static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200529 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100530{
531 int field;
532
533 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
534 if (!stats[field].type)
535 continue;
536
537 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
538 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
539 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
540 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
541 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
542 '?',
543 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200544 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100545
546 if (!stats_emit_field_tags(out, &stats[field], ':'))
547 return 0;
548 if (!stats_emit_typed_data_field(out, &stats[field]))
549 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200550 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
551 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100552 if (!chunk_strcat(out, "\n"))
553 return 0;
554 }
555 return 1;
556}
557
Simon Horman05ee2132017-01-04 09:37:25 +0100558/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200559static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200560 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100561{
562 int field;
563 int started = 0;
564
565 if (!chunk_strcat(out, "["))
566 return 0;
567
568 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
569 int old_len;
570
571 if (!field_format(info, field))
572 continue;
573
574 if (started && !chunk_strcat(out, ","))
575 goto err;
576 started = 1;
577
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200578 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100579 chunk_appendf(out,
580 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
581 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200582 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100583 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200584 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100585 goto err;
586
587 if (!stats_emit_json_field_tags(out, &info[field]))
588 goto err;
589
590 if (!stats_emit_json_data_field(out, &info[field]))
591 goto err;
592
593 if (!chunk_strcat(out, "}"))
594 goto err;
595 }
596
597 if (!chunk_strcat(out, "]"))
598 goto err;
599 return 1;
600
601err:
602 chunk_reset(out);
603 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
604 return 0;
605}
606
607/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200608static int stats_dump_fields_json(struct buffer *out,
609 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200610 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100611{
612 int field;
613 int started = 0;
614
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200615 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100616 return 0;
617 if (!chunk_strcat(out, "["))
618 return 0;
619
620 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
621 const char *obj_type;
622 int old_len;
623
624 if (!stats[field].type)
625 continue;
626
627 if (started && !chunk_strcat(out, ","))
628 goto err;
629 started = 1;
630
631 switch (stats[ST_F_TYPE].u.u32) {
632 case STATS_TYPE_FE: obj_type = "Frontend"; break;
633 case STATS_TYPE_BE: obj_type = "Backend"; break;
634 case STATS_TYPE_SO: obj_type = "Listener"; break;
635 case STATS_TYPE_SV: obj_type = "Server"; break;
636 default: obj_type = "Unknown"; break;
637 }
638
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200639 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100640 chunk_appendf(out,
641 "{"
642 "\"objType\":\"%s\","
643 "\"proxyId\":%d,"
644 "\"id\":%d,"
645 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
646 "\"processNum\":%u,",
647 obj_type, stats[ST_F_IID].u.u32,
648 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200649 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200650 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100651 goto err;
652
653 if (!stats_emit_json_field_tags(out, &stats[field]))
654 goto err;
655
656 if (!stats_emit_json_data_field(out, &stats[field]))
657 goto err;
658
659 if (!chunk_strcat(out, "}"))
660 goto err;
661 }
662
663 if (!chunk_strcat(out, "]"))
664 goto err;
665
666 return 1;
667
668err:
669 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200670 if (flags & STAT_STARTED)
671 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100672 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
673 return 0;
674}
675
William Lallemand74c24fb2016-11-21 17:18:36 +0100676/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200677 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200678 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100679 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200680static int stats_dump_fields_html(struct buffer *out,
681 const struct field *stats,
682 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100683{
Willy Tarreau83061a82018-07-13 11:56:34 +0200684 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100685
686 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
687 chunk_appendf(out,
688 /* name, queue */
689 "<tr class=\"frontend\">");
690
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200691 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100692 /* Column sub-heading for Enable or Disable server */
693 chunk_appendf(out, "<td></td>");
694 }
695
696 chunk_appendf(out,
697 "<td class=ac>"
698 "<a name=\"%s/Frontend\"></a>"
699 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
700 "<td colspan=3></td>"
701 "",
702 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
703
704 chunk_appendf(out,
705 /* sessions rate : current */
706 "<td><u>%s<div class=tips><table class=det>"
707 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
708 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
709 "",
710 U2H(stats[ST_F_RATE].u.u32),
711 U2H(stats[ST_F_CONN_RATE].u.u32),
712 U2H(stats[ST_F_RATE].u.u32));
713
714 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
715 chunk_appendf(out,
716 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
717 U2H(stats[ST_F_REQ_RATE].u.u32));
718
719 chunk_appendf(out,
720 "</table></div></u></td>"
721 /* sessions rate : max */
722 "<td><u>%s<div class=tips><table class=det>"
723 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
724 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
725 "",
726 U2H(stats[ST_F_RATE_MAX].u.u32),
727 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
728 U2H(stats[ST_F_RATE_MAX].u.u32));
729
730 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
731 chunk_appendf(out,
732 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
733 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
734
735 chunk_appendf(out,
736 "</table></div></u></td>"
737 /* sessions rate : limit */
738 "<td>%s</td>",
739 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
740
741 chunk_appendf(out,
742 /* sessions: current, max, limit, total */
743 "<td>%s</td><td>%s</td><td>%s</td>"
744 "<td><u>%s<div class=tips><table class=det>"
745 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
746 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
747 "",
748 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
749 U2H(stats[ST_F_STOT].u.u64),
750 U2H(stats[ST_F_CONN_TOT].u.u64),
751 U2H(stats[ST_F_STOT].u.u64));
752
753 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
754 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
755 chunk_appendf(out,
756 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
757 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
758 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
759 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
760 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
761 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
762 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
763 "<tr><th>- other responses:</th><td>%s</td></tr>"
764 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100765 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
766 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200767 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100768 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100769 "",
770 U2H(stats[ST_F_REQ_TOT].u.u64),
771 U2H(stats[ST_F_HRSP_1XX].u.u64),
772 U2H(stats[ST_F_HRSP_2XX].u.u64),
773 U2H(stats[ST_F_COMP_RSP].u.u64),
774 stats[ST_F_HRSP_2XX].u.u64 ?
775 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
776 U2H(stats[ST_F_HRSP_3XX].u.u64),
777 U2H(stats[ST_F_HRSP_4XX].u.u64),
778 U2H(stats[ST_F_HRSP_5XX].u.u64),
779 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200780 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100781 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
782 U2H(stats[ST_F_CACHE_HITS].u.u64),
783 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
784 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100785 U2H(stats[ST_F_WREW].u.u64),
786 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100787 }
788
789 chunk_appendf(out,
790 "</table></div></u></td>"
791 /* sessions: lbtot, lastsess */
792 "<td></td><td></td>"
793 /* bytes : in */
794 "<td>%s</td>"
795 "",
796 U2H(stats[ST_F_BIN].u.u64));
797
798 chunk_appendf(out,
799 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
800 "<td>%s%s<div class=tips><table class=det>"
801 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
802 "<tr><th>Compression in:</th><td>%s</td></tr>"
803 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
804 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
805 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
806 "</table></div>%s</td>",
807 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
808 U2H(stats[ST_F_BOUT].u.u64),
809 U2H(stats[ST_F_BOUT].u.u64),
810 U2H(stats[ST_F_COMP_IN].u.u64),
811 U2H(stats[ST_F_COMP_OUT].u.u64),
812 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
813 U2H(stats[ST_F_COMP_BYP].u.u64),
814 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
815 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,
816 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
817
818 chunk_appendf(out,
819 /* denied: req, resp */
820 "<td>%s</td><td>%s</td>"
821 /* errors : request, connect, response */
822 "<td>%s</td><td></td><td></td>"
823 /* warnings: retries, redispatches */
824 "<td></td><td></td>"
825 /* server status : reflect frontend status */
826 "<td class=ac>%s</td>"
827 /* rest of server: nothing */
828 "<td class=ac colspan=8></td></tr>"
829 "",
830 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
831 U2H(stats[ST_F_EREQ].u.u64),
832 field_str(stats, ST_F_STATUS));
833 }
834 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
835 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200836 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100837 /* Column sub-heading for Enable or Disable server */
838 chunk_appendf(out, "<td></td>");
839 }
840
841 chunk_appendf(out,
842 /* frontend name, listener name */
843 "<td class=ac><a name=\"%s/+%s\"></a>%s"
844 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
845 "",
846 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200847 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100848 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
849
Willy Tarreau708c4162019-10-09 10:19:16 +0200850 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100851 chunk_appendf(out, "<div class=tips>");
852
Willy Tarreau90807112020-02-25 08:16:33 +0100853 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100854 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
855 else if (*field_str(stats, ST_F_ADDR) == '[')
856 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
857 else if (*field_str(stats, ST_F_ADDR))
858 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
859
860 /* id */
861 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
862 }
863
864 chunk_appendf(out,
865 /* queue */
866 "%s</td><td colspan=3></td>"
867 /* sessions rate: current, max, limit */
868 "<td colspan=3>&nbsp;</td>"
869 /* sessions: current, max, limit, total, lbtot, lastsess */
870 "<td>%s</td><td>%s</td><td>%s</td>"
871 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
872 /* bytes: in, out */
873 "<td>%s</td><td>%s</td>"
874 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200875 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100876 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
877 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
878
879 chunk_appendf(out,
880 /* denied: req, resp */
881 "<td>%s</td><td>%s</td>"
882 /* errors: request, connect, response */
883 "<td>%s</td><td></td><td></td>"
884 /* warnings: retries, redispatches */
885 "<td></td><td></td>"
886 /* server status: reflect listener status */
887 "<td class=ac>%s</td>"
888 /* rest of server: nothing */
889 "<td class=ac colspan=8></td></tr>"
890 "",
891 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
892 U2H(stats[ST_F_EREQ].u.u64),
893 field_str(stats, ST_F_STATUS));
894 }
895 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
896 const char *style;
897
898 /* determine the style to use depending on the server's state,
899 * its health and weight. There isn't a 1-to-1 mapping between
900 * state and styles for the cases where the server is (still)
901 * up. The reason is that we don't want to report nolb and
902 * drain with the same color.
903 */
904
905 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
906 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
907 style = "down";
908 }
909 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
910 style = "going_up";
911 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400912 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
913 style = "draining";
914 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100915 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
916 style = "going_down";
917 }
918 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
919 style = "nolb";
920 }
921 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
922 style = "no_check";
923 }
924 else if (!stats[ST_F_CHKFAIL].type ||
925 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
926 /* no check or max health = UP */
927 if (stats[ST_F_WEIGHT].u.u32)
928 style = "up";
929 else
930 style = "draining";
931 }
932 else {
933 style = "going_down";
934 }
935
936 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
937 chunk_appendf(out, "<tr class=\"maintain\">");
938 else
939 chunk_appendf(out,
940 "<tr class=\"%s_%s\">",
941 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
942
943
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200944 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100945 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000946 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
947 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100948 field_str(stats, ST_F_SVNAME));
949
950 chunk_appendf(out,
951 "<td class=ac><a name=\"%s/%s\"></a>%s"
952 "<a class=lfsb href=\"#%s/%s\">%s</a>"
953 "",
954 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200955 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100956 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
957
Willy Tarreau708c4162019-10-09 10:19:16 +0200958 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100959 chunk_appendf(out, "<div class=tips>");
960
Willy Tarreau90807112020-02-25 08:16:33 +0100961 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100962 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
963 else if (*field_str(stats, ST_F_ADDR) == '[')
964 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
965 else if (*field_str(stats, ST_F_ADDR))
966 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
967
968 /* id */
969 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
970
971 /* cookie */
972 if (stats[ST_F_COOKIE].type) {
973 chunk_appendf(out, ", cookie: '");
974 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
975 chunk_htmlencode(out, &src);
976 chunk_appendf(out, "'");
977 }
978
979 chunk_appendf(out, "</div>");
980 }
981
982 chunk_appendf(out,
983 /* queue : current, max, limit */
984 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
985 /* sessions rate : current, max, limit */
986 "<td>%s</td><td>%s</td><td></td>"
987 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200988 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100989 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
990 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
991
992 chunk_appendf(out,
993 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200994 "<td><u>%s<div class=tips>"
995 "<table class=det>"
996 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200997 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200998 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200999 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1000 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001001 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001002 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1003 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1004 "</table></div></u>"
1005 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001006 "<td><u>%s<div class=tips><table class=det>"
1007 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1008 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001009 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001010 U2H(stats[ST_F_SCUR].u.u32),
1011 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1012 U2H(stats[ST_F_SRV_ICUR].u.u32),
1013 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1014 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001015 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001016
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001017 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1018 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1019 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001020 U2H(stats[ST_F_STOT].u.u64),
1021 U2H(stats[ST_F_STOT].u.u64));
1022
1023 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1024 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001025 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001026 "<tr><th>New connections:</th><td>%s</td></tr>"
1027 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001028 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001029 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1030 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1031 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1032 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1033 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1034 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001035 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001036 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001037 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001038 U2H(stats[ST_F_CONNECT].u.u64),
1039 U2H(stats[ST_F_REUSE].u.u64),
1040 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1041 (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 +02001042 U2H(stats[ST_F_REQ_TOT].u.u64),
1043 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1044 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1045 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1046 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1047 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1048 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1049 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1050 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1051 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1052 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1053 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1054 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001055 U2H(stats[ST_F_WREW].u.u64),
1056 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001057 }
1058
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001059 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1060 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1061 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1062 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1063 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001064 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001065 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1066 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1067 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1068 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001069
1070 chunk_appendf(out,
1071 "</table></div></u></td>"
1072 /* sessions: lbtot, last */
1073 "<td>%s</td><td>%s</td>",
1074 U2H(stats[ST_F_LBTOT].u.u64),
1075 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1076
1077 chunk_appendf(out,
1078 /* bytes : in, out */
1079 "<td>%s</td><td>%s</td>"
1080 /* denied: req, resp */
1081 "<td></td><td>%s</td>"
1082 /* errors : request, connect */
1083 "<td></td><td>%s</td>"
1084 /* errors : response */
1085 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1086 /* warnings: retries, redispatches */
1087 "<td>%lld</td><td>%lld</td>"
1088 "",
1089 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1090 U2H(stats[ST_F_DRESP].u.u64),
1091 U2H(stats[ST_F_ECON].u.u64),
1092 U2H(stats[ST_F_ERESP].u.u64),
1093 (long long)stats[ST_F_CLI_ABRT].u.u64,
1094 (long long)stats[ST_F_SRV_ABRT].u.u64,
1095 (long long)stats[ST_F_WRETR].u.u64,
1096 (long long)stats[ST_F_WREDIS].u.u64);
1097
1098 /* status, last change */
1099 chunk_appendf(out, "<td class=ac>");
1100
1101 /* FIXME!!!!
1102 * LASTCHG should contain the last change for *this* server and must be computed
1103 * properly above, as was done below, ie: this server if maint, otherwise ref server
1104 * if tracking. Note that ref is either local or remote depending on tracking.
1105 */
1106
1107
1108 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1109 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1110 }
1111 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1112 chunk_strcat(out, "<i>no check</i>");
1113 }
1114 else {
1115 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1116 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1117 if (stats[ST_F_CHECK_HEALTH].u.u32)
1118 chunk_strcat(out, " &uarr;");
1119 }
1120 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1121 chunk_strcat(out, " &darr;");
1122 }
1123
1124 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1125 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1126 chunk_appendf(out,
1127 "</td><td class=ac><u> %s",
1128 field_str(stats, ST_F_AGENT_STATUS));
1129
1130 if (stats[ST_F_AGENT_CODE].type)
1131 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1132
1133 if (stats[ST_F_AGENT_DURATION].type)
1134 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1135
1136 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1137
1138 if (*field_str(stats, ST_F_LAST_AGT)) {
1139 chunk_appendf(out, ": ");
1140 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1141 chunk_htmlencode(out, &src);
1142 }
1143 chunk_appendf(out, "</div></u>");
1144 }
1145 else if (stats[ST_F_CHECK_STATUS].type) {
1146 chunk_appendf(out,
1147 "</td><td class=ac><u> %s",
1148 field_str(stats, ST_F_CHECK_STATUS));
1149
1150 if (stats[ST_F_CHECK_CODE].type)
1151 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1152
1153 if (stats[ST_F_CHECK_DURATION].type)
1154 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1155
1156 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1157
1158 if (*field_str(stats, ST_F_LAST_CHK)) {
1159 chunk_appendf(out, ": ");
1160 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1161 chunk_htmlencode(out, &src);
1162 }
1163 chunk_appendf(out, "</div></u>");
1164 }
1165 else
1166 chunk_appendf(out, "</td><td>");
1167
1168 chunk_appendf(out,
1169 /* weight */
1170 "</td><td class=ac>%d</td>"
1171 /* act, bck */
1172 "<td class=ac>%s</td><td class=ac>%s</td>"
1173 "",
1174 stats[ST_F_WEIGHT].u.u32,
1175 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1176 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1177
1178 /* check failures: unique, fatal, down time */
1179 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1180 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1181 }
1182 else if (stats[ST_F_CHKFAIL].type) {
1183 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1184
1185 if (stats[ST_F_HANAFAIL].type)
1186 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1187
1188 chunk_appendf(out,
1189 "<div class=tips>Failed Health Checks%s</div></u></td>"
1190 "<td>%lld</td><td>%s</td>"
1191 "",
1192 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1193 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1194 }
1195 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1196 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1197 chunk_appendf(out,
1198 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1199 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1200 }
1201 else
1202 chunk_appendf(out, "<td colspan=3></td>");
1203
1204 /* throttle */
1205 if (stats[ST_F_THROTTLE].type)
1206 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1207 else
1208 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1209 }
1210 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1211 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001212 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001213 /* Column sub-heading for Enable or Disable server */
1214 chunk_appendf(out, "<td></td>");
1215 }
1216 chunk_appendf(out,
1217 "<td class=ac>"
1218 /* name */
1219 "%s<a name=\"%s/Backend\"></a>"
1220 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1221 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001222 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001223 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1224
Willy Tarreau708c4162019-10-09 10:19:16 +02001225 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001226 /* balancing */
1227 chunk_appendf(out, "<div class=tips>balancing: %s",
1228 field_str(stats, ST_F_ALGO));
1229
1230 /* cookie */
1231 if (stats[ST_F_COOKIE].type) {
1232 chunk_appendf(out, ", cookie: '");
1233 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1234 chunk_htmlencode(out, &src);
1235 chunk_appendf(out, "'");
1236 }
1237 chunk_appendf(out, "</div>");
1238 }
1239
1240 chunk_appendf(out,
1241 "%s</td>"
1242 /* queue : current, max */
1243 "<td>%s</td><td>%s</td><td></td>"
1244 /* sessions rate : current, max, limit */
1245 "<td>%s</td><td>%s</td><td></td>"
1246 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001247 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001248 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1249 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1250
1251 chunk_appendf(out,
1252 /* sessions: current, max, limit, total */
1253 "<td>%s</td><td>%s</td><td>%s</td>"
1254 "<td><u>%s<div class=tips><table class=det>"
1255 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1256 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001257 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 +01001258 U2H(stats[ST_F_STOT].u.u64),
1259 U2H(stats[ST_F_STOT].u.u64));
1260
1261 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1262 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1263 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001264 "<tr><th>New connections:</th><td>%s</td></tr>"
1265 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001266 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1267 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1268 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1269 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1270 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1271 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1272 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1273 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001274 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1275 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001276 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001277 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001278 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001279 U2H(stats[ST_F_CONNECT].u.u64),
1280 U2H(stats[ST_F_REUSE].u.u64),
1281 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1282 (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 +01001283 U2H(stats[ST_F_REQ_TOT].u.u64),
1284 U2H(stats[ST_F_HRSP_1XX].u.u64),
1285 U2H(stats[ST_F_HRSP_2XX].u.u64),
1286 U2H(stats[ST_F_COMP_RSP].u.u64),
1287 stats[ST_F_HRSP_2XX].u.u64 ?
1288 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1289 U2H(stats[ST_F_HRSP_3XX].u.u64),
1290 U2H(stats[ST_F_HRSP_4XX].u.u64),
1291 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001292 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001293 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1294 U2H(stats[ST_F_CACHE_HITS].u.u64),
1295 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1296 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001297 U2H(stats[ST_F_WREW].u.u64),
1298 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001299 }
1300
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001301 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1302 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1303 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1304 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1305 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001306 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001307 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1308 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1309 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1310 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001311
1312 chunk_appendf(out,
1313 "</table></div></u></td>"
1314 /* sessions: lbtot, last */
1315 "<td>%s</td><td>%s</td>"
1316 /* bytes: in */
1317 "<td>%s</td>"
1318 "",
1319 U2H(stats[ST_F_LBTOT].u.u64),
1320 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1321 U2H(stats[ST_F_BIN].u.u64));
1322
1323 chunk_appendf(out,
1324 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1325 "<td>%s%s<div class=tips><table class=det>"
1326 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1327 "<tr><th>Compression in:</th><td>%s</td></tr>"
1328 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1329 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1330 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1331 "</table></div>%s</td>",
1332 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1333 U2H(stats[ST_F_BOUT].u.u64),
1334 U2H(stats[ST_F_BOUT].u.u64),
1335 U2H(stats[ST_F_COMP_IN].u.u64),
1336 U2H(stats[ST_F_COMP_OUT].u.u64),
1337 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1338 U2H(stats[ST_F_COMP_BYP].u.u64),
1339 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1340 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,
1341 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1342
1343 chunk_appendf(out,
1344 /* denied: req, resp */
1345 "<td>%s</td><td>%s</td>"
1346 /* errors : request, connect */
1347 "<td></td><td>%s</td>"
1348 /* errors : response */
1349 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1350 /* warnings: retries, redispatches */
1351 "<td>%lld</td><td>%lld</td>"
1352 /* backend status: reflect backend status (up/down): we display UP
1353 * if the backend has known working servers or if it has no server at
1354 * all (eg: for stats). Then we display the total weight, number of
1355 * active and backups. */
1356 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1357 "<td class=ac>%d</td><td class=ac>%d</td>"
1358 "",
1359 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1360 U2H(stats[ST_F_ECON].u.u64),
1361 U2H(stats[ST_F_ERESP].u.u64),
1362 (long long)stats[ST_F_CLI_ABRT].u.u64,
1363 (long long)stats[ST_F_SRV_ABRT].u.u64,
1364 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1365 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1366 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1367 stats[ST_F_WEIGHT].u.u32,
1368 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1369
1370 chunk_appendf(out,
1371 /* rest of backend: nothing, down transitions, total downtime, throttle */
1372 "<td class=ac>&nbsp;</td><td>%d</td>"
1373 "<td>%s</td>"
1374 "<td></td>"
1375 "</tr>",
1376 stats[ST_F_CHKDOWN].u.u32,
1377 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1378 }
1379 return 1;
1380}
1381
Willy Tarreau43241ff2019-10-09 11:27:51 +02001382static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001383{
Simon Horman05ee2132017-01-04 09:37:25 +01001384 int ret;
1385
William Lallemand74c24fb2016-11-21 17:18:36 +01001386 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001387 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001388 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001389 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001390 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001391 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001392 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001393 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001394
1395 if (ret)
1396 appctx->ctx.stats.flags |= STAT_STARTED;
1397
1398 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001399}
1400
1401/* Fill <stats> with the frontend statistics. <stats> is
1402 * preallocated array of length <len>. The length of the array
1403 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1404 * this value, the function returns 0, otherwise, it returns 1.
1405 */
1406int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1407{
1408 if (len < ST_F_TOTAL_FIELDS)
1409 return 0;
1410
1411 memset(stats, 0, sizeof(*stats) * len);
1412
1413 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1414 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1415 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1416 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1417 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1418 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1419 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1420 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1421 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1422 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1423 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1424 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1425 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1426 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1427 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1428 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1429 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1430 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1431 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1432 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1433 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1434 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001435 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001436 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001437
1438 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1439 if (px->mode == PR_MODE_HTTP) {
1440 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1441 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1442 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1443 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1444 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1445 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1446 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001447 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1448 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001449 }
1450
1451 /* requests : req_rate, req_rate_max, req_tot, */
1452 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1453 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1454 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1455
1456 /* compression: in, out, bypassed, responses */
1457 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1458 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1459 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1460 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1461
1462 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1463 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1464 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1465 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1466
1467 return 1;
1468}
1469
1470/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1471 * the state from stream interface <si>. The caller is responsible for clearing
1472 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1473 */
1474static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1475{
1476 struct appctx *appctx = __objt_appctx(si->end);
1477
1478 if (!(px->cap & PR_CAP_FE))
1479 return 0;
1480
1481 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1482 return 0;
1483
1484 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1485 return 0;
1486
Willy Tarreau43241ff2019-10-09 11:27:51 +02001487 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001488}
1489
1490/* Fill <stats> with the listener statistics. <stats> is
1491 * preallocated array of length <len>. The length of the array
1492 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1493 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001494 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001495 */
1496int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1497 struct field *stats, int len)
1498{
Willy Tarreau83061a82018-07-13 11:56:34 +02001499 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001500
1501 if (len < ST_F_TOTAL_FIELDS)
1502 return 0;
1503
1504 if (!l->counters)
1505 return 0;
1506
1507 chunk_reset(out);
1508 memset(stats, 0, sizeof(*stats) * len);
1509
1510 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1511 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1512 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1513 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1514 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1515 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1516 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1517 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1518 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1519 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1520 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1521 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1522 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1523 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001524 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 +01001525 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1526 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1527 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1528 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001529 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001530 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001531
Willy Tarreau708c4162019-10-09 10:19:16 +02001532 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001533 char str[INET6_ADDRSTRLEN];
1534 int port;
1535
1536 port = get_host_port(&l->addr);
1537 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1538 case AF_INET:
1539 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1540 chunk_appendf(out, "%s:%d", str, port);
1541 break;
1542 case AF_INET6:
1543 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1544 chunk_appendf(out, "[%s]:%d", str, port);
1545 break;
1546 case AF_UNIX:
1547 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1548 break;
1549 case -1:
1550 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1551 chunk_strcat(out, strerror(errno));
1552 break;
1553 default: /* address family not supported */
1554 break;
1555 }
1556 }
1557
1558 return 1;
1559}
1560
1561/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001562 * from stream interface <si>. The caller is responsible for clearing the trash
1563 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001564 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001565static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001566{
1567 struct appctx *appctx = __objt_appctx(si->end);
1568
Willy Tarreau43241ff2019-10-09 11:27:51 +02001569 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001570 return 0;
1571
Willy Tarreau43241ff2019-10-09 11:27:51 +02001572 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001573}
1574
1575enum srv_stats_state {
1576 SRV_STATS_STATE_DOWN = 0,
1577 SRV_STATS_STATE_DOWN_AGENT,
1578 SRV_STATS_STATE_GOING_UP,
1579 SRV_STATS_STATE_UP_GOING_DOWN,
1580 SRV_STATS_STATE_UP,
1581 SRV_STATS_STATE_NOLB_GOING_DOWN,
1582 SRV_STATS_STATE_NOLB,
1583 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1584 SRV_STATS_STATE_DRAIN,
1585 SRV_STATS_STATE_DRAIN_AGENT,
1586 SRV_STATS_STATE_NO_CHECK,
1587
1588 SRV_STATS_STATE_COUNT, /* Must be last */
1589};
1590
1591static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1592 [SRV_STATS_STATE_DOWN] = "DOWN",
1593 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1594 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1595 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1596 [SRV_STATS_STATE_UP] = "UP",
1597 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1598 [SRV_STATS_STATE_NOLB] = "NOLB",
1599 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1600 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1601 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1602 [SRV_STATS_STATE_NO_CHECK] = "no check"
1603};
1604
1605/* Fill <stats> with the server statistics. <stats> is
1606 * preallocated array of length <len>. The length of the array
1607 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1608 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001609 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001610 */
1611int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1612 struct field *stats, int len)
1613{
1614 struct server *via, *ref;
1615 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001616 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001617 enum srv_stats_state state;
1618 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001619 long long srv_samples_counter;
1620 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001621
1622 if (len < ST_F_TOTAL_FIELDS)
1623 return 0;
1624
1625 memset(stats, 0, sizeof(*stats) * len);
1626
1627 /* we have "via" which is the tracked server as described in the configuration,
1628 * and "ref" which is the checked server and the end of the chain.
1629 */
1630 via = sv->track ? sv->track : sv;
1631 ref = via;
1632 while (ref->track)
1633 ref = ref->track;
1634
Emeric Brun52a91d32017-08-31 14:41:55 +02001635 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001636 if ((ref->check.state & CHK_ST_ENABLED) &&
1637 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1638 state = SRV_STATS_STATE_UP_GOING_DOWN;
1639 } else {
1640 state = SRV_STATS_STATE_UP;
1641 }
1642
Emeric Brun52a91d32017-08-31 14:41:55 +02001643 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001644 if (ref->agent.state & CHK_ST_ENABLED)
1645 state = SRV_STATS_STATE_DRAIN_AGENT;
1646 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1647 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1648 else
1649 state = SRV_STATS_STATE_DRAIN;
1650 }
1651
1652 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1653 state = SRV_STATS_STATE_NO_CHECK;
1654 }
1655 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001656 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001657 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1658 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1659 state = SRV_STATS_STATE_NOLB;
1660 } else {
1661 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1662 }
1663 }
1664 else { /* stopped */
1665 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1666 state = SRV_STATS_STATE_DOWN_AGENT;
1667 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1668 state = SRV_STATS_STATE_DOWN; /* DOWN */
1669 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1670 state = SRV_STATS_STATE_GOING_UP;
1671 } else {
1672 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1673 }
1674 }
1675
1676 chunk_reset(out);
1677
1678 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1679 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1680 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1681 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1682 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1683 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1684 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1685
1686 if (sv->maxconn)
1687 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1688
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001689 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1690 if (sv->max_idle_conns != -1)
1691 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1692
William Lallemand74c24fb2016-11-21 17:18:36 +01001693 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1694 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1695 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001696 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001697 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1698 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1699 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1700 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001701 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001702 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001703 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1704 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001705
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001706 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1707 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1708 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001709 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001710
William Lallemand74c24fb2016-11-21 17:18:36 +01001711 /* status */
1712 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001713 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001714 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001715 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001716 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001717 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001718 chunk_appendf(out, "MAINT");
1719 else
1720 chunk_appendf(out,
1721 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001722 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1723 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001724
1725 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1726 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001727 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 +01001728 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1729 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1730
1731 /* check failures: unique, fatal; last change, total downtime */
1732 if (sv->check.state & CHK_ST_ENABLED) {
1733 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1734 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1735 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1736 }
1737
1738 if (sv->maxqueue)
1739 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1740
1741 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1742 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1743 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1744
Emeric Brun52a91d32017-08-31 14:41:55 +02001745 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001746 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1747
1748 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1749
1750 if (sv->track) {
1751 char *fld_track = chunk_newstr(out);
1752
1753 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1754 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1755 }
1756
1757 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1758 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1759 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1760
1761 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1762 const char *fld_chksts;
1763
1764 fld_chksts = chunk_newstr(out);
1765 chunk_strcat(out, "* "); // for check in progress
1766 chunk_strcat(out, get_check_status_info(sv->check.status));
1767 if (!(sv->check.state & CHK_ST_INPROGRESS))
1768 fld_chksts += 2; // skip "* "
1769 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1770
1771 if (sv->check.status >= HCHK_STATUS_L57DATA)
1772 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1773
1774 if (sv->check.status >= HCHK_STATUS_CHECKED)
1775 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1776
1777 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1778 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1779 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1780 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1781 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1782 }
1783
1784 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1785 const char *fld_chksts;
1786
1787 fld_chksts = chunk_newstr(out);
1788 chunk_strcat(out, "* "); // for check in progress
1789 chunk_strcat(out, get_check_status_info(sv->agent.status));
1790 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1791 fld_chksts += 2; // skip "* "
1792 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1793
1794 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1795 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1796
1797 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1798 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1799
1800 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1801 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1802 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1803 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1804 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1805 }
1806
1807 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1808 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001809 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001810 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1811 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1812 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1813 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1814 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1815 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1816 }
1817
1818 if (ref->observe)
1819 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1820
1821 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1822 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1823 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1824
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001825 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1826 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1827 srv_samples_window = srv_samples_counter;
1828
1829 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1830 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1831 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1832 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001833
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001834 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1835 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1836 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1837 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1838
Willy Tarreau708c4162019-10-09 10:19:16 +02001839 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001840 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1841 case AF_INET:
1842 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001843 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001844 break;
1845 case AF_INET6:
1846 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001847 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001848 break;
1849 case AF_UNIX:
1850 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1851 break;
1852 case -1:
1853 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1854 chunk_strcat(out, strerror(errno));
1855 break;
1856 default: /* address family not supported */
1857 break;
1858 }
1859
1860 if (sv->cookie)
1861 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1862 }
1863
1864 return 1;
1865}
1866
1867/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001868 * from stream interface <si>, and server state <state>. The caller is
1869 * responsible for clearing the trash if needed. Returns non-zero if it emits
1870 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001871 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001872static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001873{
1874 struct appctx *appctx = __objt_appctx(si->end);
1875
Willy Tarreau43241ff2019-10-09 11:27:51 +02001876 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001877 return 0;
1878
Willy Tarreau43241ff2019-10-09 11:27:51 +02001879 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001880}
1881
1882/* Fill <stats> with the backend statistics. <stats> is
1883 * preallocated array of length <len>. The length of the array
1884 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1885 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001886 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001887 */
1888int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1889{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001890 long long be_samples_counter;
1891 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1892
William Lallemand74c24fb2016-11-21 17:18:36 +01001893 if (len < ST_F_TOTAL_FIELDS)
1894 return 0;
1895
1896 memset(stats, 0, sizeof(*stats) * len);
1897
1898 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1899 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1900 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1901 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1902 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001903 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001904 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1905 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1906 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1907 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1908 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1909 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1910 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1911 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1912 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1913 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1914 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001915 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001916 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001917 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1918 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001919 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1920 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1921 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1922 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1923 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1924 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1925 if (px->srv)
1926 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1927
1928 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1929 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1930 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1931 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1932 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1933 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1934 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1935
Willy Tarreau708c4162019-10-09 10:19:16 +02001936 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001937 if (px->cookie_name)
1938 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1939 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1940 }
1941
1942 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1943 if (px->mode == PR_MODE_HTTP) {
1944 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1945 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1946 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1947 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1948 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1949 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1950 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001951 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1952 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001953 }
1954
1955 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1956 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1957
1958 /* compression: in, out, bypassed, responses */
1959 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1960 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1961 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1962 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1963 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1964
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001965 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1966 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1967 be_samples_window = be_samples_counter;
1968
1969 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1970 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1971 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1972 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 +01001973
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001974 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1975 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1976 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1977 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1978
William Lallemand74c24fb2016-11-21 17:18:36 +01001979 return 1;
1980}
1981
1982/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001983 * interface <si>. The caller is responsible for clearing the trash if needed.
1984 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001985 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001986static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001987{
1988 struct appctx *appctx = __objt_appctx(si->end);
1989
1990 if (!(px->cap & PR_CAP_BE))
1991 return 0;
1992
1993 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1994 return 0;
1995
Willy Tarreau43241ff2019-10-09 11:27:51 +02001996 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001997 return 0;
1998
Willy Tarreau43241ff2019-10-09 11:27:51 +02001999 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002000}
2001
2002/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2003 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2004 * for clearing the trash if needed.
2005 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002006static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002007{
2008 struct appctx *appctx = __objt_appctx(si->end);
2009 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2010
2011 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2012 /* A form to enable/disable this proxy servers */
2013
2014 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2015 scope_txt[0] = 0;
2016 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002017 const char *scope_ptr = stats_scope_ptr(appctx, si);
2018
William Lallemand74c24fb2016-11-21 17:18:36 +01002019 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002020 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002021 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2022 }
2023
2024 chunk_appendf(&trash,
2025 "<form method=\"post\">");
2026 }
2027
2028 /* print a new table */
2029 chunk_appendf(&trash,
2030 "<table class=\"tbl\" width=\"100%%\">\n"
2031 "<tr class=\"titre\">"
2032 "<th class=\"pxname\" width=\"10%%\">");
2033
2034 chunk_appendf(&trash,
2035 "<a name=\"%s\"></a>%s"
2036 "<a class=px href=\"#%s\">%s</a>",
2037 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002038 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002039 px->id, px->id);
2040
Willy Tarreau676c29e2019-10-09 10:50:01 +02002041 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002042 /* cap, mode, id */
2043 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2044 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2045 px->uuid);
2046 chunk_appendf(&trash, "</div>");
2047 }
2048
2049 chunk_appendf(&trash,
2050 "%s</th>"
2051 "<th class=\"%s\" width=\"90%%\">%s</th>"
2052 "</tr>\n"
2053 "</table>\n"
2054 "<table class=\"tbl\" width=\"100%%\">\n"
2055 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002056 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002057 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2058
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002059 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002060 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002061 if ((px->cap & PR_CAP_BE) && px->srv)
2062 chunk_appendf(&trash,
2063 "<th rowspan=2 width=1><input type=\"checkbox\" "
2064 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2065 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2066 px->id,
2067 px->id);
2068 else
2069 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002070 }
2071
2072 chunk_appendf(&trash,
2073 "<th rowspan=2></th>"
2074 "<th colspan=3>Queue</th>"
2075 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2076 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2077 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2078 "<th colspan=9>Server</th>"
2079 "</tr>\n"
2080 "<tr class=\"titre\">"
2081 "<th>Cur</th><th>Max</th><th>Limit</th>"
2082 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2083 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2084 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2085 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2086 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2087 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2088 "<th>Thrtle</th>\n"
2089 "</tr>");
2090}
2091
2092/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2093 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2094 */
2095static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2096{
2097 struct appctx *appctx = __objt_appctx(si->end);
2098 chunk_appendf(&trash, "</table>");
2099
2100 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2101 /* close the form used to enable/disable this proxy servers */
2102 chunk_appendf(&trash,
2103 "Choose the action to perform on the checked servers : "
2104 "<select name=action>"
2105 "<option value=\"\"></option>"
2106 "<option value=\"ready\">Set state to READY</option>"
2107 "<option value=\"drain\">Set state to DRAIN</option>"
2108 "<option value=\"maint\">Set state to MAINT</option>"
2109 "<option value=\"dhlth\">Health: disable checks</option>"
2110 "<option value=\"ehlth\">Health: enable checks</option>"
2111 "<option value=\"hrunn\">Health: force UP</option>"
2112 "<option value=\"hnolb\">Health: force NOLB</option>"
2113 "<option value=\"hdown\">Health: force DOWN</option>"
2114 "<option value=\"dagent\">Agent: disable checks</option>"
2115 "<option value=\"eagent\">Agent: enable checks</option>"
2116 "<option value=\"arunn\">Agent: force UP</option>"
2117 "<option value=\"adown\">Agent: force DOWN</option>"
2118 "<option value=\"shutdown\">Kill Sessions</option>"
2119 "</select>"
2120 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2121 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2122 "</form>",
2123 px->uuid);
2124 }
2125
2126 chunk_appendf(&trash, "<p>\n");
2127}
2128
2129/*
2130 * Dumps statistics for a proxy. The output is sent to the stream interface's
2131 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2132 * buffer space, or non-zero if everything completed. This function is used
2133 * both by the CLI and the HTTP entry points, and is able to dump the output
2134 * in HTML or CSV formats. If the later, <uri> must be NULL.
2135 */
Christopher Fauletef779222018-10-31 08:47:01 +01002136int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2137 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002138{
2139 struct appctx *appctx = __objt_appctx(si->end);
2140 struct stream *s = si_strm(si);
2141 struct channel *rep = si_ic(si);
2142 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2143 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002144
2145 chunk_reset(&trash);
2146
2147 switch (appctx->ctx.stats.px_st) {
2148 case STAT_PX_ST_INIT:
2149 /* we are on a new proxy */
2150 if (uri && uri->scope) {
2151 /* we have a limited scope, we have to check the proxy name */
2152 struct stat_scope *scope;
2153 int len;
2154
2155 len = strlen(px->id);
2156 scope = uri->scope;
2157
2158 while (scope) {
2159 /* match exact proxy name */
2160 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2161 break;
2162
2163 /* match '.' which means 'self' proxy */
2164 if (!strcmp(scope->px_id, ".") && px == s->be)
2165 break;
2166 scope = scope->next;
2167 }
2168
2169 /* proxy name not found : don't dump anything */
2170 if (scope == NULL)
2171 return 1;
2172 }
2173
2174 /* if the user has requested a limited output and the proxy
2175 * name does not match, skip it.
2176 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002177 if (appctx->ctx.stats.scope_len) {
2178 const char *scope_ptr = stats_scope_ptr(appctx, si);
2179
2180 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2181 return 1;
2182 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002183
2184 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2185 (appctx->ctx.stats.iid != -1) &&
2186 (px->uuid != appctx->ctx.stats.iid))
2187 return 1;
2188
2189 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2190 /* fall through */
2191
2192 case STAT_PX_ST_TH:
2193 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002194 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002195 if (!stats_putchk(rep, htx, &trash))
2196 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002197 }
2198
2199 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2200 /* fall through */
2201
2202 case STAT_PX_ST_FE:
2203 /* print the frontend */
2204 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002205 if (!stats_putchk(rep, htx, &trash))
2206 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002207 }
2208
2209 appctx->ctx.stats.l = px->conf.listeners.n;
2210 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2211 /* fall through */
2212
2213 case STAT_PX_ST_LI:
2214 /* stats.l has been initialized above */
2215 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002216 if (htx) {
2217 if (htx_almost_full(htx))
2218 goto full;
2219 }
2220 else {
2221 if (buffer_almost_full(&rep->buf))
2222 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002223 }
2224
2225 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2226 if (!l->counters)
2227 continue;
2228
2229 if (appctx->ctx.stats.flags & STAT_BOUND) {
2230 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2231 break;
2232
2233 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2234 continue;
2235 }
2236
2237 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002238 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002239 if (!stats_putchk(rep, htx, &trash))
2240 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002241 }
2242 }
2243
2244 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2245 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2246 /* fall through */
2247
2248 case STAT_PX_ST_SV:
2249 /* stats.sv has been initialized above */
2250 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002251 if (htx) {
2252 if (htx_almost_full(htx))
2253 goto full;
2254 }
2255 else {
2256 if (buffer_almost_full(&rep->buf))
2257 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002258 }
2259
2260 sv = appctx->ctx.stats.sv;
2261
2262 if (appctx->ctx.stats.flags & STAT_BOUND) {
2263 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2264 break;
2265
2266 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2267 continue;
2268 }
2269
2270 svs = sv;
2271 while (svs->track)
2272 svs = svs->track;
2273
2274 /* do not report servers which are DOWN and not changing state */
2275 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002276 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2277 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002278 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2279 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2280 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2281 continue;
2282 }
2283
Willy Tarreau43241ff2019-10-09 11:27:51 +02002284 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002285 if (!stats_putchk(rep, htx, &trash))
2286 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002287 }
2288 } /* for sv */
2289
2290 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2291 /* fall through */
2292
2293 case STAT_PX_ST_BE:
2294 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002295 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002296 if (!stats_putchk(rep, htx, &trash))
2297 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002298 }
2299
2300 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2301 /* fall through */
2302
2303 case STAT_PX_ST_END:
2304 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2305 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002306 if (!stats_putchk(rep, htx, &trash))
2307 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002308 }
2309
2310 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2311 /* fall through */
2312
2313 case STAT_PX_ST_FIN:
2314 return 1;
2315
2316 default:
2317 /* unknown state, we should put an abort() here ! */
2318 return 1;
2319 }
Christopher Fauletef779222018-10-31 08:47:01 +01002320
2321 full:
2322 si_rx_room_blk(si);
2323 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002324}
2325
2326/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2327 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2328 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002329static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002330{
2331 /* WARNING! This must fit in the first buffer !!! */
2332 chunk_appendf(&trash,
2333 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2334 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2335 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2336 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2337 "<style type=\"text/css\"><!--\n"
2338 "body {"
2339 " font-family: arial, helvetica, sans-serif;"
2340 " font-size: 12px;"
2341 " font-weight: normal;"
2342 " color: black;"
2343 " background: white;"
2344 "}\n"
2345 "th,td {"
2346 " font-size: 10px;"
2347 "}\n"
2348 "h1 {"
2349 " font-size: x-large;"
2350 " margin-bottom: 0.5em;"
2351 "}\n"
2352 "h2 {"
2353 " font-family: helvetica, arial;"
2354 " font-size: x-large;"
2355 " font-weight: bold;"
2356 " font-style: italic;"
2357 " color: #6020a0;"
2358 " margin-top: 0em;"
2359 " margin-bottom: 0em;"
2360 "}\n"
2361 "h3 {"
2362 " font-family: helvetica, arial;"
2363 " font-size: 16px;"
2364 " font-weight: bold;"
2365 " color: #b00040;"
2366 " background: #e8e8d0;"
2367 " margin-top: 0em;"
2368 " margin-bottom: 0em;"
2369 "}\n"
2370 "li {"
2371 " margin-top: 0.25em;"
2372 " margin-right: 2em;"
2373 "}\n"
2374 ".hr {margin-top: 0.25em;"
2375 " border-color: black;"
2376 " border-bottom-style: solid;"
2377 "}\n"
2378 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2379 ".total {background: #20D0D0;color: #ffff80;}\n"
2380 ".frontend {background: #e8e8d0;}\n"
2381 ".socket {background: #d0d0d0;}\n"
2382 ".backend {background: #e8e8d0;}\n"
2383 ".active_down {background: #ff9090;}\n"
2384 ".active_going_up {background: #ffd020;}\n"
2385 ".active_going_down {background: #ffffa0;}\n"
2386 ".active_up {background: #c0ffc0;}\n"
2387 ".active_nolb {background: #20a0ff;}\n"
2388 ".active_draining {background: #20a0FF;}\n"
2389 ".active_no_check {background: #e0e0e0;}\n"
2390 ".backup_down {background: #ff9090;}\n"
2391 ".backup_going_up {background: #ff80ff;}\n"
2392 ".backup_going_down {background: #c060ff;}\n"
2393 ".backup_up {background: #b0d0ff;}\n"
2394 ".backup_nolb {background: #90b0e0;}\n"
2395 ".backup_draining {background: #cc9900;}\n"
2396 ".backup_no_check {background: #e0e0e0;}\n"
2397 ".maintain {background: #c07820;}\n"
2398 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2399 "\n"
2400 "a.px:link {color: #ffff40; text-decoration: none;}"
2401 "a.px:visited {color: #ffff40; text-decoration: none;}"
2402 "a.px:hover {color: #ffffff; text-decoration: none;}"
2403 "a.lfsb:link {color: #000000; text-decoration: none;}"
2404 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2405 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2406 "\n"
2407 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2408 "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"
2409 "table.tbl td.ac { text-align: center;}\n"
2410 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2411 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2412 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2413 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2414 "\n"
2415 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2416 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2417 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2418 "table.det { border-collapse: collapse; border-style: none; }\n"
2419 "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"
2420 "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"
2421 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2422 "div.tips {\n"
2423 " display:block;\n"
2424 " visibility:hidden;\n"
2425 " z-index:2147483647;\n"
2426 " position:absolute;\n"
2427 " padding:2px 4px 3px;\n"
2428 " background:#f0f060; color:#000000;\n"
2429 " border:1px solid #7040c0;\n"
2430 " white-space:nowrap;\n"
2431 " font-style:normal;font-size:11px;font-weight:normal;\n"
2432 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2433 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2434 "}\n"
2435 "u:hover div.tips {visibility:visible;}\n"
2436 "-->\n"
2437 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002438 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002439 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002440 );
2441}
2442
2443/* Dumps the HTML stats information block to the trash for and uses the state from
2444 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2445 * for clearing the trash if needed.
2446 */
2447static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2448{
2449 struct appctx *appctx = __objt_appctx(si->end);
2450 unsigned int up = (now.tv_sec - start_date.tv_sec);
2451 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002452 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002453 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2454
2455 /* Turn the bytes per second to bits per second and take care of the
2456 * usual ethernet overhead in order to help figure how far we are from
2457 * interface saturation since it's the only case which usually matters.
2458 * For this we count the total size of an Ethernet frame on the wire
2459 * including preamble and IFG (1538) for the largest TCP segment it
2460 * transports (1448 with TCP timestamps). This is not valid for smaller
2461 * packets (under-estimated), but it gives a reasonably accurate
2462 * estimation of how far we are from uplink saturation.
2463 */
2464 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002465
2466 /* WARNING! this has to fit the first packet too.
2467 * We are around 3.5 kB, add adding entries will
2468 * become tricky if we want to support 4kB buffers !
2469 */
2470 chunk_appendf(&trash,
2471 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2472 PRODUCT_NAME "%s</a></h1>\n"
2473 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2474 "<hr width=\"100%%\" class=\"hr\">\n"
2475 "<h3>&gt; General process information</h3>\n"
2476 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002477 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002478 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2479 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2480 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002481 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002482 "Running tasks: %d/%d; idle = %d %%<br>\n"
2483 "</td><td align=\"center\" nowrap>\n"
2484 "<table class=\"lgd\"><tr>\n"
2485 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2486 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2487 "</tr><tr>\n"
2488 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2489 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2490 "</tr><tr>\n"
2491 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2492 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2493 "</tr><tr>\n"
2494 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2495 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2496 "</tr><tr>\n"
2497 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2498 "</tr><tr>\n"
2499 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2500 "</tr></table>\n"
2501 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2502 "</td>"
2503 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2504 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2505 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002506 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2507 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2508 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2509 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2510 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002511 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002512 up / 86400, (up % 86400) / 3600,
2513 (up % 3600) / 60, (up % 60),
2514 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2515 global.rlimit_memmax ? " MB" : "",
2516 global.rlimit_nofile,
2517 global.maxsock, global.maxconn, global.maxpipes,
2518 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002519 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2520 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002521 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002522 );
2523
2524 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002525 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002526 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2527
2528 chunk_appendf(&trash,
2529 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2530 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2531 STAT_SCOPE_TXT_MAXLEN);
2532
2533 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2534 scope_txt[0] = 0;
2535 if (appctx->ctx.stats.scope_len) {
2536 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002537 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002538 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2539 }
2540
2541 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2542 chunk_appendf(&trash,
2543 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2544 uri->uri_prefix,
2545 "",
2546 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2547 scope_txt);
2548 else
2549 chunk_appendf(&trash,
2550 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2551 uri->uri_prefix,
2552 ";up",
2553 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2554 scope_txt);
2555
2556 if (uri->refresh > 0) {
2557 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2558 chunk_appendf(&trash,
2559 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2560 uri->uri_prefix,
2561 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2562 "",
2563 scope_txt);
2564 else
2565 chunk_appendf(&trash,
2566 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2567 uri->uri_prefix,
2568 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2569 ";norefresh",
2570 scope_txt);
2571 }
2572
2573 chunk_appendf(&trash,
2574 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2575 uri->uri_prefix,
2576 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2577 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2578 scope_txt);
2579
2580 chunk_appendf(&trash,
2581 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2582 uri->uri_prefix,
2583 (uri->refresh > 0) ? ";norefresh" : "",
2584 scope_txt);
2585
2586 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002587 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2588 uri->uri_prefix,
2589 (uri->refresh > 0) ? ";norefresh" : "",
2590 scope_txt, uri->uri_prefix);
2591
2592 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002593 "</ul></td>"
2594 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2595 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2596 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2597 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2598 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2599 "</ul>"
2600 "</td>"
2601 "</tr></table>\n"
2602 ""
2603 );
2604
2605 if (appctx->ctx.stats.st_code) {
2606 switch (appctx->ctx.stats.st_code) {
2607 case STAT_STATUS_DONE:
2608 chunk_appendf(&trash,
2609 "<p><div class=active_up>"
2610 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2611 "Action processed successfully."
2612 "</div>\n", uri->uri_prefix,
2613 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2614 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2615 scope_txt);
2616 break;
2617 case STAT_STATUS_NONE:
2618 chunk_appendf(&trash,
2619 "<p><div class=active_going_down>"
2620 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2621 "Nothing has changed."
2622 "</div>\n", uri->uri_prefix,
2623 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2624 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2625 scope_txt);
2626 break;
2627 case STAT_STATUS_PART:
2628 chunk_appendf(&trash,
2629 "<p><div class=active_going_down>"
2630 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2631 "Action partially processed.<br>"
2632 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2633 "</div>\n", uri->uri_prefix,
2634 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2635 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2636 scope_txt);
2637 break;
2638 case STAT_STATUS_ERRP:
2639 chunk_appendf(&trash,
2640 "<p><div class=active_down>"
2641 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2642 "Action not processed because of invalid parameters."
2643 "<ul>"
2644 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002645 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002646 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2647 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2648 "</ul>"
2649 "</div>\n", uri->uri_prefix,
2650 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2651 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2652 scope_txt);
2653 break;
2654 case STAT_STATUS_EXCD:
2655 chunk_appendf(&trash,
2656 "<p><div class=active_down>"
2657 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2658 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2659 "You should retry with less servers at a time.</b>"
2660 "</div>\n", uri->uri_prefix,
2661 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2662 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2663 scope_txt);
2664 break;
2665 case STAT_STATUS_DENY:
2666 chunk_appendf(&trash,
2667 "<p><div class=active_down>"
2668 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2669 "<b>Action denied.</b>"
2670 "</div>\n", uri->uri_prefix,
2671 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2672 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2673 scope_txt);
2674 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002675 case STAT_STATUS_IVAL:
2676 chunk_appendf(&trash,
2677 "<p><div class=active_down>"
2678 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2679 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2680 "</div>\n", uri->uri_prefix,
2681 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2682 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2683 scope_txt);
2684 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002685 default:
2686 chunk_appendf(&trash,
2687 "<p><div class=active_no_check>"
2688 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2689 "Unexpected result."
2690 "</div>\n", uri->uri_prefix,
2691 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2692 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2693 scope_txt);
2694 }
2695 chunk_appendf(&trash, "<p>\n");
2696 }
2697}
2698
2699/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2700 * for clearing the trash if needed.
2701 */
2702static void stats_dump_html_end()
2703{
2704 chunk_appendf(&trash, "</body></html>\n");
2705}
2706
Simon Horman05ee2132017-01-04 09:37:25 +01002707/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2708 * for clearing it if needed.
2709 */
2710static void stats_dump_json_header()
2711{
2712 chunk_strcat(&trash, "[");
2713}
2714
2715
2716/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2717 * for clearing the trash if needed.
2718 */
2719static void stats_dump_json_end()
2720{
2721 chunk_strcat(&trash, "]");
2722}
2723
William Lallemand74c24fb2016-11-21 17:18:36 +01002724/* This function dumps statistics onto the stream interface's read buffer in
2725 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2726 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2727 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2728 * and the stream must be closed, or -1 in case of any error. This function is
2729 * used by both the CLI and the HTTP handlers.
2730 */
Christopher Fauletef779222018-10-31 08:47:01 +01002731static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2732 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002733{
2734 struct appctx *appctx = __objt_appctx(si->end);
2735 struct channel *rep = si_ic(si);
2736 struct proxy *px;
2737
2738 chunk_reset(&trash);
2739
2740 switch (appctx->st2) {
2741 case STAT_ST_INIT:
2742 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2743 /* fall through */
2744
2745 case STAT_ST_HEAD:
2746 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002747 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002748 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2749 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002750 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002751 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002752 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2753 stats_dump_csv_header();
2754
Christopher Fauletef779222018-10-31 08:47:01 +01002755 if (!stats_putchk(rep, htx, &trash))
2756 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002757
Christopher Faulet6338a082019-09-09 15:50:54 +02002758 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2759 appctx->st2 = STAT_ST_FIN;
2760 return 1;
2761 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002762 appctx->st2 = STAT_ST_INFO;
2763 /* fall through */
2764
2765 case STAT_ST_INFO:
2766 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2767 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002768 if (!stats_putchk(rep, htx, &trash))
2769 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002770 }
2771
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002772 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002773 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2774 appctx->st2 = STAT_ST_LIST;
2775 /* fall through */
2776
2777 case STAT_ST_LIST:
2778 /* dump proxies */
2779 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002780 if (htx) {
2781 if (htx_almost_full(htx))
2782 goto full;
2783 }
2784 else {
2785 if (buffer_almost_full(&rep->buf))
2786 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002787 }
2788
2789 px = appctx->ctx.stats.px;
2790 /* skip the disabled proxies, global frontend and non-networked ones */
2791 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002792 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002793 return 0;
2794
2795 appctx->ctx.stats.px = px->next;
2796 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2797 }
2798 /* here, we just have reached the last proxy */
2799
2800 appctx->st2 = STAT_ST_END;
2801 /* fall through */
2802
2803 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002804 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2805 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2806 stats_dump_html_end();
2807 else
2808 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002809 if (!stats_putchk(rep, htx, &trash))
2810 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002811 }
2812
2813 appctx->st2 = STAT_ST_FIN;
2814 /* fall through */
2815
2816 case STAT_ST_FIN:
2817 return 1;
2818
2819 default:
2820 /* unknown state ! */
2821 appctx->st2 = STAT_ST_FIN;
2822 return -1;
2823 }
Christopher Fauletef779222018-10-31 08:47:01 +01002824
2825 full:
2826 si_rx_room_blk(si);
2827 return 0;
2828
William Lallemand74c24fb2016-11-21 17:18:36 +01002829}
2830
2831/* We reached the stats page through a POST request. The appctx is
2832 * expected to have already been allocated by the caller.
2833 * Parse the posted data and enable/disable servers if necessary.
2834 * Returns 1 if request was parsed or zero if it needs more data.
2835 */
2836static int stats_process_http_post(struct stream_interface *si)
2837{
2838 struct stream *s = si_strm(si);
2839 struct appctx *appctx = objt_appctx(si->end);
2840
2841 struct proxy *px = NULL;
2842 struct server *sv = NULL;
2843
2844 char key[LINESIZE];
2845 int action = ST_ADM_ACTION_NONE;
2846 int reprocess = 0;
2847
2848 int total_servers = 0;
2849 int altered_servers = 0;
2850
2851 char *first_param, *cur_param, *next_param, *end_params;
2852 char *st_cur_param = NULL;
2853 char *st_next_param = NULL;
2854
Christopher Fauleta3618372018-12-13 21:59:56 +01002855 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002856
Christopher Fauletb7f88902019-07-15 21:56:43 +02002857 struct htx *htx = htxbuf(&s->req.buf);
2858 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002859
Christopher Fauletb7f88902019-07-15 21:56:43 +02002860 /* we need more data */
2861 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2862 /* check if we can receive more */
2863 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2864 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2865 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002866 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002867 goto wait;
2868 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002869
Christopher Fauletb7f88902019-07-15 21:56:43 +02002870 /* The request was fully received. Copy data */
2871 blk = htx_get_head_blk(htx);
2872 while (blk) {
2873 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002874
Christopher Fauletb7f88902019-07-15 21:56:43 +02002875 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2876 break;
2877 if (type == HTX_BLK_DATA) {
2878 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002879
Christopher Fauletb7f88902019-07-15 21:56:43 +02002880 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002881 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2882 goto out;
2883 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002884 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002885 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002886 }
2887
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002888 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002889 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002890 cur_param = next_param = end_params;
2891 *end_params = '\0';
2892
2893 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2894
2895 /*
2896 * Parse the parameters in reverse order to only store the last value.
2897 * From the html form, the backend and the action are at the end.
2898 */
2899 while (cur_param > first_param) {
2900 char *value;
2901 int poffset, plen;
2902
2903 cur_param--;
2904
2905 if ((*cur_param == '&') || (cur_param == first_param)) {
2906 reprocess_servers:
2907 /* Parse the key */
2908 poffset = (cur_param != first_param ? 1 : 0);
2909 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2910 if ((plen > 0) && (plen <= sizeof(key))) {
2911 strncpy(key, cur_param + poffset, plen);
2912 key[plen - 1] = '\0';
2913 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002914 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002915 goto out;
2916 }
2917
2918 /* Parse the value */
2919 value = key;
2920 while (*value != '\0' && *value != '=') {
2921 value++;
2922 }
2923 if (*value == '=') {
2924 /* Ok, a value is found, we can mark the end of the key */
2925 *value++ = '\0';
2926 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002927 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002928 break;
2929
2930 /* Now we can check the key to see what to do */
2931 if (!px && (strcmp(key, "b") == 0)) {
2932 if ((px = proxy_be_by_name(value)) == NULL) {
2933 /* the backend name is unknown or ambiguous (duplicate names) */
2934 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2935 goto out;
2936 }
2937 }
2938 else if (!action && (strcmp(key, "action") == 0)) {
2939 if (strcmp(value, "ready") == 0) {
2940 action = ST_ADM_ACTION_READY;
2941 }
2942 else if (strcmp(value, "drain") == 0) {
2943 action = ST_ADM_ACTION_DRAIN;
2944 }
2945 else if (strcmp(value, "maint") == 0) {
2946 action = ST_ADM_ACTION_MAINT;
2947 }
2948 else if (strcmp(value, "shutdown") == 0) {
2949 action = ST_ADM_ACTION_SHUTDOWN;
2950 }
2951 else if (strcmp(value, "dhlth") == 0) {
2952 action = ST_ADM_ACTION_DHLTH;
2953 }
2954 else if (strcmp(value, "ehlth") == 0) {
2955 action = ST_ADM_ACTION_EHLTH;
2956 }
2957 else if (strcmp(value, "hrunn") == 0) {
2958 action = ST_ADM_ACTION_HRUNN;
2959 }
2960 else if (strcmp(value, "hnolb") == 0) {
2961 action = ST_ADM_ACTION_HNOLB;
2962 }
2963 else if (strcmp(value, "hdown") == 0) {
2964 action = ST_ADM_ACTION_HDOWN;
2965 }
2966 else if (strcmp(value, "dagent") == 0) {
2967 action = ST_ADM_ACTION_DAGENT;
2968 }
2969 else if (strcmp(value, "eagent") == 0) {
2970 action = ST_ADM_ACTION_EAGENT;
2971 }
2972 else if (strcmp(value, "arunn") == 0) {
2973 action = ST_ADM_ACTION_ARUNN;
2974 }
2975 else if (strcmp(value, "adown") == 0) {
2976 action = ST_ADM_ACTION_ADOWN;
2977 }
2978 /* else these are the old supported methods */
2979 else if (strcmp(value, "disable") == 0) {
2980 action = ST_ADM_ACTION_DISABLE;
2981 }
2982 else if (strcmp(value, "enable") == 0) {
2983 action = ST_ADM_ACTION_ENABLE;
2984 }
2985 else if (strcmp(value, "stop") == 0) {
2986 action = ST_ADM_ACTION_STOP;
2987 }
2988 else if (strcmp(value, "start") == 0) {
2989 action = ST_ADM_ACTION_START;
2990 }
2991 else {
2992 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2993 goto out;
2994 }
2995 }
2996 else if (strcmp(key, "s") == 0) {
2997 if (!(px && action)) {
2998 /*
2999 * Indicates that we'll need to reprocess the parameters
3000 * as soon as backend and action are known
3001 */
3002 if (!reprocess) {
3003 st_cur_param = cur_param;
3004 st_next_param = next_param;
3005 }
3006 reprocess = 1;
3007 }
3008 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003009 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003010 switch (action) {
3011 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003012 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003013 altered_servers++;
3014 total_servers++;
3015 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3016 }
3017 break;
3018 case ST_ADM_ACTION_ENABLE:
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_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3023 }
3024 break;
3025 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003026 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003027 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3028 altered_servers++;
3029 total_servers++;
3030 }
3031 break;
3032 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003033 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003034 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3035 altered_servers++;
3036 total_servers++;
3037 }
3038 break;
3039 case ST_ADM_ACTION_DHLTH:
3040 if (sv->check.state & CHK_ST_CONFIGURED) {
3041 sv->check.state &= ~CHK_ST_ENABLED;
3042 altered_servers++;
3043 total_servers++;
3044 }
3045 break;
3046 case ST_ADM_ACTION_EHLTH:
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_HRUNN:
3054 if (!(sv->track)) {
3055 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003056 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003057 altered_servers++;
3058 total_servers++;
3059 }
3060 break;
3061 case ST_ADM_ACTION_HNOLB:
3062 if (!(sv->track)) {
3063 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003064 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003065 altered_servers++;
3066 total_servers++;
3067 }
3068 break;
3069 case ST_ADM_ACTION_HDOWN:
3070 if (!(sv->track)) {
3071 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003072 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003073 altered_servers++;
3074 total_servers++;
3075 }
3076 break;
3077 case ST_ADM_ACTION_DAGENT:
3078 if (sv->agent.state & CHK_ST_CONFIGURED) {
3079 sv->agent.state &= ~CHK_ST_ENABLED;
3080 altered_servers++;
3081 total_servers++;
3082 }
3083 break;
3084 case ST_ADM_ACTION_EAGENT:
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_ARUNN:
3092 if (sv->agent.state & CHK_ST_ENABLED) {
3093 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003094 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003095 altered_servers++;
3096 total_servers++;
3097 }
3098 break;
3099 case ST_ADM_ACTION_ADOWN:
3100 if (sv->agent.state & CHK_ST_ENABLED) {
3101 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003102 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003103 altered_servers++;
3104 total_servers++;
3105 }
3106 break;
3107 case ST_ADM_ACTION_READY:
3108 srv_adm_set_ready(sv);
3109 altered_servers++;
3110 total_servers++;
3111 break;
3112 case ST_ADM_ACTION_DRAIN:
3113 srv_adm_set_drain(sv);
3114 altered_servers++;
3115 total_servers++;
3116 break;
3117 case ST_ADM_ACTION_MAINT:
3118 srv_adm_set_maint(sv);
3119 altered_servers++;
3120 total_servers++;
3121 break;
3122 case ST_ADM_ACTION_SHUTDOWN:
3123 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003124 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003125 altered_servers++;
3126 total_servers++;
3127 }
3128 break;
3129 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003130 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003131 } else {
3132 /* the server name is unknown or ambiguous (duplicate names) */
3133 total_servers++;
3134 }
3135 }
3136 if (reprocess && px && action) {
3137 /* Now, we know the backend and the action chosen by the user.
3138 * We can safely restart from the first server parameter
3139 * to reprocess them
3140 */
3141 cur_param = st_cur_param;
3142 next_param = st_next_param;
3143 reprocess = 0;
3144 goto reprocess_servers;
3145 }
3146
3147 next_param = cur_param;
3148 }
3149 }
3150
3151 if (total_servers == 0) {
3152 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3153 }
3154 else if (altered_servers == 0) {
3155 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3156 }
3157 else if (altered_servers == total_servers) {
3158 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3159 }
3160 else {
3161 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3162 }
3163 out:
3164 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003165 wait:
3166 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3167 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003168}
3169
3170
Christopher Fauletb7f88902019-07-15 21:56:43 +02003171static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003172{
3173 struct stream *s = si_strm(si);
3174 struct uri_auth *uri = s->be->uri_auth;
3175 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003176 struct htx_sl *sl;
3177 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003178
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003179 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);
3180 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3181 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003182 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003183 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003184
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003185 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003186 goto full;
3187 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3188 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3189 goto full;
3190 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003191 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3192 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3193 goto full;
3194 }
Christopher Fauletef779222018-10-31 08:47:01 +01003195 else {
3196 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3197 goto full;
3198 }
3199
3200 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3201 const char *refresh = U2A(uri->refresh);
3202 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3203 goto full;
3204 }
3205
3206 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3207 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3208 goto full;
3209 }
3210
3211 if (!htx_add_endof(htx, HTX_BLK_EOH))
3212 goto full;
3213
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003214 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003215 return 1;
3216
3217 full:
3218 htx_reset(htx);
3219 si_rx_room_blk(si);
3220 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003221}
3222
Christopher Fauletef779222018-10-31 08:47:01 +01003223
Christopher Fauletb7f88902019-07-15 21:56:43 +02003224static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003225{
3226 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3227 struct stream *s = si_strm(si);
3228 struct uri_auth *uri = s->be->uri_auth;
3229 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003230 struct htx_sl *sl;
3231 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003232
3233 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3234 scope_txt[0] = 0;
3235 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003236 const char *scope_ptr = stats_scope_ptr(appctx, si);
3237
Christopher Fauletef779222018-10-31 08:47:01 +01003238 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003239 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003240 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3241 }
3242
3243 /* We don't want to land on the posted stats page because a refresh will
3244 * repost the data. We don't want this to happen on accident so we redirect
3245 * the browse to the stats page with a GET.
3246 */
3247 chunk_printf(&trash, "%s;st=%s%s%s%s",
3248 uri->uri_prefix,
3249 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3250 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3251 stat_status_codes[appctx->ctx.stats.st_code]) ?
3252 stat_status_codes[appctx->ctx.stats.st_code] :
3253 stat_status_codes[STAT_STATUS_UNKN],
3254 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3255 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3256 scope_txt);
3257
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003258 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3259 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3260 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003261 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003262 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003263
3264 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003265 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3266 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3267 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3268 goto full;
3269
3270 if (!htx_add_endof(htx, HTX_BLK_EOH))
3271 goto full;
3272
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003273 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003274 return 1;
3275
3276full:
3277 htx_reset(htx);
3278 si_rx_room_blk(si);
3279 return 0;
3280}
William Lallemand74c24fb2016-11-21 17:18:36 +01003281
Simon Horman05ee2132017-01-04 09:37:25 +01003282
William Lallemand74c24fb2016-11-21 17:18:36 +01003283/* This I/O handler runs as an applet embedded in a stream interface. It is
3284 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3285 * appctx->st0 contains the operation in progress (dump, done). The handler
3286 * automatically unregisters itself once transfer is complete.
3287 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003288static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003289{
3290 struct stream_interface *si = appctx->owner;
3291 struct stream *s = si_strm(si);
3292 struct channel *req = si_oc(si);
3293 struct channel *res = si_ic(si);
3294 struct htx *req_htx, *res_htx;
3295
3296 res_htx = htx_from_buf(&res->buf);
3297
3298 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3299 goto out;
3300
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003301 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003302 if (!b_size(&res->buf)) {
3303 si_rx_room_blk(si);
3304 goto out;
3305 }
3306
3307 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003308 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3309 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003310
3311 /* all states are processed in sequence */
3312 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003313 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003314 if (s->txn->meth == HTTP_METH_HEAD)
3315 appctx->st0 = STAT_HTTP_DONE;
3316 else
3317 appctx->st0 = STAT_HTTP_DUMP;
3318 }
3319 }
3320
3321 if (appctx->st0 == STAT_HTTP_DUMP) {
3322 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3323 appctx->st0 = STAT_HTTP_DONE;
3324 }
3325
3326 if (appctx->st0 == STAT_HTTP_POST) {
3327 if (stats_process_http_post(si))
3328 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003329 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003330 appctx->st0 = STAT_HTTP_DONE;
3331 }
3332
3333 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003334 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003335 appctx->st0 = STAT_HTTP_DONE;
3336 }
3337
3338 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003339 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003340 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3341 si_rx_room_blk(si);
3342 goto out;
3343 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003344 channel_add_input(&s->res, 1);
3345 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003346 }
3347
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003348 if (appctx->st0 == STAT_HTTP_END) {
3349 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003350 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003351 si_shutr(si);
3352 }
3353
3354 /* eat the whole request */
3355 if (co_data(req)) {
3356 req_htx = htx_from_buf(&req->buf);
3357 co_htx_skip(req, req_htx, co_data(req));
3358 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003359 }
3360 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003361
Christopher Fauletef779222018-10-31 08:47:01 +01003362 out:
3363 /* we have left the request in the buffer for the case where we
3364 * process a POST, and this automatically re-enables activity on
3365 * read. It's better to indicate that we want to stop reading when
3366 * we're sending, so that we know there's at most one direction
3367 * deciding to wake the applet up. It saves it from looping when
3368 * emitting large blocks into small TCP windows.
3369 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003370 htx_to_buf(res_htx, &res->buf);
3371 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003372 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003373}
3374
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003375/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003376static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003377 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003378{
3379 int field;
3380
3381 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3382 if (!field_format(info, field))
3383 continue;
3384
Willy Tarreaueaa55372019-10-09 07:39:11 +02003385 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003386 return 0;
3387 if (!stats_emit_raw_data_field(out, &info[field]))
3388 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003389 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3390 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003391 if (!chunk_strcat(out, "\n"))
3392 return 0;
3393 }
3394 return 1;
3395}
3396
3397/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003398static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003399 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003400{
3401 int field;
3402
3403 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3404 if (!field_format(info, field))
3405 continue;
3406
Willy Tarreaueaa55372019-10-09 07:39:11 +02003407 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 +01003408 return 0;
3409 if (!stats_emit_field_tags(out, &info[field], ':'))
3410 return 0;
3411 if (!stats_emit_typed_data_field(out, &info[field]))
3412 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003413 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3414 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003415 if (!chunk_strcat(out, "\n"))
3416 return 0;
3417 }
3418 return 1;
3419}
3420
3421/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003422 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003423 * INF_TOTAL_FIELDS. If this length is less then this value, the
3424 * function returns 0, otherwise, it returns 1.
3425 */
3426int stats_fill_info(struct field *info, int len)
3427{
3428 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003429 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003430
3431#ifdef USE_OPENSSL
3432 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3433 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3434 int ssl_reuse = 0;
3435
3436 if (ssl_key_rate < ssl_sess_rate) {
3437 /* count the ssl reuse ratio and avoid overflows in both directions */
3438 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3439 }
3440#endif
3441
3442 if (len < INF_TOTAL_FIELDS)
3443 return 0;
3444
3445 chunk_reset(out);
3446 memset(info, 0, sizeof(*info) * len);
3447
3448 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003449 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3450 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003451
Yves Lafon95317282018-02-26 11:10:37 +01003452 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003453 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3454 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3455 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3456
3457 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3458 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3459
3460 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3461 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3462 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3463 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3464 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3465 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3466 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3467 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3468 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3469 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3470 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3471 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3472#ifdef USE_OPENSSL
3473 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3474 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3475 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3476#endif
3477 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3478 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3479 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3480 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3481 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3482 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3483 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3484 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3485 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3486
3487#ifdef USE_OPENSSL
3488 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3489 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3490 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3491 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3492 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3493 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3494 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3495 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3496 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3497 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3498#endif
3499 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3500 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3501 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3502#ifdef USE_ZLIB
3503 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3504 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3505#endif
3506 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003507 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003508 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003509 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3510 if (global.desc)
3511 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003512 info[INF_STOPPING] = mkf_u32(0, stopping);
3513 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003514 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003515 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003516 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003517 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003518 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003519 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003520 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003521 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3522 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 +02003523 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003524
3525 return 1;
3526}
3527
3528/* This function dumps information onto the stream interface's read buffer.
3529 * It returns 0 as long as it does not complete, non-zero upon completion.
3530 * No state is used.
3531 */
3532static int stats_dump_info_to_buffer(struct stream_interface *si)
3533{
3534 struct appctx *appctx = __objt_appctx(si->end);
3535
3536 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3537 return 0;
3538
3539 chunk_reset(&trash);
3540
3541 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003542 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003543 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003544 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003545 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003546 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003547
Willy Tarreau06d80a92017-10-19 14:32:15 +02003548 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003549 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003550 return 0;
3551 }
3552
3553 return 1;
3554}
3555
Simon Horman6f6bb382017-01-04 09:37:26 +01003556/* This function dumps the schema onto the stream interface's read buffer.
3557 * It returns 0 as long as it does not complete, non-zero upon completion.
3558 * No state is used.
3559 *
3560 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3561 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3562 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003563static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003564{
3565
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003566 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003567
3568 chunk_strcat(out,
3569 "{"
3570 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3571 "\"oneOf\":["
3572 "{"
3573 "\"title\":\"Info\","
3574 "\"type\":\"array\","
3575 "\"items\":{"
3576 "\"properties\":{"
3577 "\"title\":\"InfoItem\","
3578 "\"type\":\"object\","
3579 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3580 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3581 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3582 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3583 "},"
3584 "\"required\":[\"field\",\"processNum\",\"tags\","
3585 "\"value\"]"
3586 "}"
3587 "},"
3588 "{"
3589 "\"title\":\"Stat\","
3590 "\"type\":\"array\","
3591 "\"items\":{"
3592 "\"title\":\"InfoItem\","
3593 "\"type\":\"object\","
3594 "\"properties\":{"
3595 "\"objType\":{"
3596 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3597 "\"Server\",\"Unknown\"]"
3598 "},"
3599 "\"proxyId\":{"
3600 "\"type\":\"integer\","
3601 "\"minimum\":0"
3602 "},"
3603 "\"id\":{"
3604 "\"type\":\"integer\","
3605 "\"minimum\":0"
3606 "},"
3607 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3608 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3609 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3610 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3611 "},"
3612 "\"required\":[\"objType\",\"proxyId\",\"id\","
3613 "\"field\",\"processNum\",\"tags\","
3614 "\"value\"]"
3615 "}"
3616 "},"
3617 "{"
3618 "\"title\":\"Error\","
3619 "\"type\":\"object\","
3620 "\"properties\":{"
3621 "\"errorStr\":{"
3622 "\"type\":\"string\""
3623 "},"
3624 "\"required\":[\"errorStr\"]"
3625 "}"
3626 "}"
3627 "],"
3628 "\"definitions\":{"
3629 "\"field\":{"
3630 "\"type\":\"object\","
3631 "\"pos\":{"
3632 "\"type\":\"integer\","
3633 "\"minimum\":0"
3634 "},"
3635 "\"name\":{"
3636 "\"type\":\"string\""
3637 "},"
3638 "\"required\":[\"pos\",\"name\"]"
3639 "},"
3640 "\"processNum\":{"
3641 "\"type\":\"integer\","
3642 "\"minimum\":1"
3643 "},"
3644 "\"tags\":{"
3645 "\"type\":\"object\","
3646 "\"origin\":{"
3647 "\"type\":\"string\","
3648 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3649 "\"Config\",\"Product\",\"Unknown\"]"
3650 "},"
3651 "\"nature\":{"
3652 "\"type\":\"string\","
3653 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3654 "\"Rate\",\"Counter\",\"Duration\","
3655 "\"Age\",\"Time\",\"Name\",\"Output\","
3656 "\"Avg\", \"Unknown\"]"
3657 "},"
3658 "\"scope\":{"
3659 "\"type\":\"string\","
3660 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3661 "\"System\",\"Unknown\"]"
3662 "},"
3663 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3664 "},"
3665 "\"typedValue\":{"
3666 "\"type\":\"object\","
3667 "\"oneOf\":["
3668 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3669 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3670 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3671 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3672 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3673 "],"
3674 "\"definitions\":{"
3675 "\"s32Value\":{"
3676 "\"properties\":{"
3677 "\"type\":{"
3678 "\"type\":\"string\","
3679 "\"enum\":[\"s32\"]"
3680 "},"
3681 "\"value\":{"
3682 "\"type\":\"integer\","
3683 "\"minimum\":-2147483648,"
3684 "\"maximum\":2147483647"
3685 "}"
3686 "},"
3687 "\"required\":[\"type\",\"value\"]"
3688 "},"
3689 "\"s64Value\":{"
3690 "\"properties\":{"
3691 "\"type\":{"
3692 "\"type\":\"string\","
3693 "\"enum\":[\"s64\"]"
3694 "},"
3695 "\"value\":{"
3696 "\"type\":\"integer\","
3697 "\"minimum\":-9007199254740991,"
3698 "\"maximum\":9007199254740991"
3699 "}"
3700 "},"
3701 "\"required\":[\"type\",\"value\"]"
3702 "},"
3703 "\"u32Value\":{"
3704 "\"properties\":{"
3705 "\"type\":{"
3706 "\"type\":\"string\","
3707 "\"enum\":[\"u32\"]"
3708 "},"
3709 "\"value\":{"
3710 "\"type\":\"integer\","
3711 "\"minimum\":0,"
3712 "\"maximum\":4294967295"
3713 "}"
3714 "},"
3715 "\"required\":[\"type\",\"value\"]"
3716 "},"
3717 "\"u64Value\":{"
3718 "\"properties\":{"
3719 "\"type\":{"
3720 "\"type\":\"string\","
3721 "\"enum\":[\"u64\"]"
3722 "},"
3723 "\"value\":{"
3724 "\"type\":\"integer\","
3725 "\"minimum\":0,"
3726 "\"maximum\":9007199254740991"
3727 "}"
3728 "},"
3729 "\"required\":[\"type\",\"value\"]"
3730 "},"
3731 "\"strValue\":{"
3732 "\"properties\":{"
3733 "\"type\":{"
3734 "\"type\":\"string\","
3735 "\"enum\":[\"str\"]"
3736 "},"
3737 "\"value\":{\"type\":\"string\"}"
3738 "},"
3739 "\"required\":[\"type\",\"value\"]"
3740 "},"
3741 "\"unknownValue\":{"
3742 "\"properties\":{"
3743 "\"type\":{"
3744 "\"type\":\"integer\","
3745 "\"minimum\":0"
3746 "},"
3747 "\"value\":{"
3748 "\"type\":\"string\","
3749 "\"enum\":[\"unknown\"]"
3750 "}"
3751 "},"
3752 "\"required\":[\"type\",\"value\"]"
3753 "}"
3754 "}"
3755 "}"
3756 "}"
3757 "}");
3758
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003759 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003760 chunk_reset(out);
3761 chunk_appendf(out,
3762 "{\"errorStr\":\"output buffer too short\"}");
3763 }
3764}
3765
3766/* This function dumps the schema onto the stream interface's read buffer.
3767 * It returns 0 as long as it does not complete, non-zero upon completion.
3768 * No state is used.
3769 */
3770static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3771{
3772 chunk_reset(&trash);
3773
3774 stats_dump_json_schema(&trash);
3775
Willy Tarreau06d80a92017-10-19 14:32:15 +02003776 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003777 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003778 return 0;
3779 }
3780
3781 return 1;
3782}
3783
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003784static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003785{
3786 struct proxy *px;
3787 struct server *sv;
3788 struct listener *li;
3789 int clrall = 0;
3790
3791 if (strcmp(args[2], "all") == 0)
3792 clrall = 1;
3793
3794 /* check permissions */
3795 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3796 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3797 return 1;
3798
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003799 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003800 if (clrall) {
3801 memset(&px->be_counters, 0, sizeof(px->be_counters));
3802 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3803 }
3804 else {
3805 px->be_counters.conn_max = 0;
3806 px->be_counters.p.http.rps_max = 0;
3807 px->be_counters.sps_max = 0;
3808 px->be_counters.cps_max = 0;
3809 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003810 px->be_counters.qtime_max = 0;
3811 px->be_counters.ctime_max = 0;
3812 px->be_counters.dtime_max = 0;
3813 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003814
3815 px->fe_counters.conn_max = 0;
3816 px->fe_counters.p.http.rps_max = 0;
3817 px->fe_counters.sps_max = 0;
3818 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003819 }
3820
3821 for (sv = px->srv; sv; sv = sv->next)
3822 if (clrall)
3823 memset(&sv->counters, 0, sizeof(sv->counters));
3824 else {
3825 sv->counters.cur_sess_max = 0;
3826 sv->counters.nbpend_max = 0;
3827 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003828 sv->counters.qtime_max = 0;
3829 sv->counters.ctime_max = 0;
3830 sv->counters.dtime_max = 0;
3831 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003832 }
3833
3834 list_for_each_entry(li, &px->conf.listeners, by_fe)
3835 if (li->counters) {
3836 if (clrall)
3837 memset(li->counters, 0, sizeof(*li->counters));
3838 else
3839 li->counters->conn_max = 0;
3840 }
3841 }
3842
3843 global.cps_max = 0;
3844 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003845 global.ssl_max = 0;
3846 global.ssl_fe_keys_max = 0;
3847 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003848
3849 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003850 return 1;
3851}
3852
3853
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003854static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003855{
Willy Tarreau2f397382019-10-09 11:43:59 +02003856 int arg = 2;
3857
Willy Tarreaud25fc792016-12-16 12:33:47 +01003858 appctx->ctx.stats.scope_str = 0;
3859 appctx->ctx.stats.scope_len = 0;
3860 appctx->ctx.stats.flags = 0;
3861
Willy Tarreau2f397382019-10-09 11:43:59 +02003862 while (*args[arg]) {
3863 if (strcmp(args[arg], "typed") == 0)
3864 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3865 else if (strcmp(args[arg], "json") == 0)
3866 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3867 else if (strcmp(args[arg], "desc") == 0)
3868 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3869 arg++;
3870 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003871 return 0;
3872}
3873
3874
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003875static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003876{
Willy Tarreau2f397382019-10-09 11:43:59 +02003877 int arg = 2;
3878
Willy Tarreaud25fc792016-12-16 12:33:47 +01003879 appctx->ctx.stats.scope_str = 0;
3880 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003881 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3882
3883 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3884 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003885
Willy Tarreau2f397382019-10-09 11:43:59 +02003886 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003887 struct proxy *px;
3888
Willy Tarreau2f397382019-10-09 11:43:59 +02003889 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003890 if (px)
3891 appctx->ctx.stats.iid = px->uuid;
3892 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003893 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003894
Willy Tarreau9d008692019-08-09 11:21:01 +02003895 if (!appctx->ctx.stats.iid)
3896 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003897
Willy Tarreau2b812e22016-11-22 16:18:05 +01003898 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003899 appctx->ctx.stats.type = atoi(args[arg+1]);
3900 appctx->ctx.stats.sid = atoi(args[arg+2]);
3901 arg += 3;
3902 }
3903
3904 while (*args[arg]) {
3905 if (strcmp(args[arg], "typed") == 0)
3906 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3907 else if (strcmp(args[arg], "json") == 0)
3908 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3909 else if (strcmp(args[arg], "desc") == 0)
3910 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3911 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003912 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003913
Willy Tarreau2b812e22016-11-22 16:18:05 +01003914 return 0;
3915}
3916
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003917static int cli_io_handler_dump_info(struct appctx *appctx)
3918{
3919 return stats_dump_info_to_buffer(appctx->owner);
3920}
3921
Willy Tarreau2b812e22016-11-22 16:18:05 +01003922/* This I/O handler runs as an applet embedded in a stream interface. It is
3923 * used to send raw stats over a socket.
3924 */
3925static int cli_io_handler_dump_stat(struct appctx *appctx)
3926{
Christopher Fauletef779222018-10-31 08:47:01 +01003927 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003928}
3929
Simon Horman6f6bb382017-01-04 09:37:26 +01003930static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3931{
3932 return stats_dump_json_schema_to_buffer(appctx->owner);
3933}
3934
Willy Tarreau2b812e22016-11-22 16:18:05 +01003935/* register cli keywords */
3936static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003937 { { "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 +02003938 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3939 { { "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 +01003940 { { "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 +01003941 {{},}
3942}};
3943
Willy Tarreau0108d902018-11-25 19:14:37 +01003944INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3945
William Lallemand74c24fb2016-11-21 17:18:36 +01003946struct applet http_stats_applet = {
3947 .obj_type = OBJ_TYPE_APPLET,
3948 .name = "<STATS>", /* used for logging */
3949 .fct = http_stats_io_handler,
3950 .release = NULL,
3951};
3952
William Lallemand74c24fb2016-11-21 17:18:36 +01003953/*
3954 * Local variables:
3955 * c-indent-level: 8
3956 * c-basic-offset: 8
3957 * End:
3958 */