blob: 83c0e664532e156b6e0583ce6adaa09f385515c5 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020028#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020035#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020036#include <haproxy/debug.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020037#include <haproxy/dns.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020044#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020045#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020046#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020047#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020048#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020049#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020050#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020051#include <haproxy/pipe.h>
52#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020053#include <haproxy/proxy.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020054#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020055#include <haproxy/session.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020056#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020057#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020058#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020059#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020060#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020061#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020062#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020063#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020064#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020065#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066
William Lallemand74c24fb2016-11-21 17:18:36 +010067
Christopher Faulet2b9b6782019-02-27 16:42:58 +010068/* status codes available for the stats admin page (strictly 4 chars length) */
69const char *stat_status_codes[STAT_STATUS_SIZE] = {
70 [STAT_STATUS_DENY] = "DENY",
71 [STAT_STATUS_DONE] = "DONE",
72 [STAT_STATUS_ERRP] = "ERRP",
73 [STAT_STATUS_EXCD] = "EXCD",
74 [STAT_STATUS_NONE] = "NONE",
75 [STAT_STATUS_PART] = "PART",
76 [STAT_STATUS_UNKN] = "UNKN",
77 [STAT_STATUS_IVAL] = "IVAL",
78};
79
Willy Tarreau0baac8c2016-11-22 16:36:53 +010080/* These are the field names for each INF_* field position. Please pay attention
81 * to always use the exact same name except that the strings for new names must
82 * be lower case or CamelCase while the enum entries must be upper case.
83 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020084const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020085 [INF_NAME] = { .name = "Name", .desc = "Product name" },
86 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
87 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
88 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
89 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
90 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
91 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
92 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
93 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
94 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
95 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
96 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
97 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
98 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
99 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
100 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
101 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
102 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
103 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
104 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
105 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
106 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
107 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
108 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
109 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
110 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
111 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
112 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
113 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
114 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
115 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
116 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
117 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
118 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
119 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
120 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
121 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
122 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
123 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
124 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
125 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
126 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
127 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
128 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
129 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
130 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
131 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
132 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
133 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
134 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
135 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
136 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
137 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
138 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
139 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
140 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
141 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
142 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
143 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
144 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
145 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
146 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Christopher Fauletaaa70852020-07-10 13:56:30 +0200147 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicdedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200148 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200149 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200150 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100151};
152
Willy Tarreaueaa55372019-10-09 07:39:11 +0200153const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
155 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
156 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
157 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
158 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
159 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
160 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
161 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
162 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
163 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
164 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
165 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
166 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
167 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
168 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
169 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
170 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
171 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
172 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
173 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
174 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
175 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
176 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
177 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
178 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
179 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
180 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
181 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
182 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
183 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
184 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
185 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
186 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
187 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
188 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
189 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
190 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
191 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
192 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
193 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
194 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
195 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
196 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
197 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
198 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
199 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
200 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
201 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
202 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
203 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
204 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
205 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
206 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
207 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
208 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
209 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
210 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
211 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
212 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
213 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
214 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
215 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
216 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
217 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
218 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
219 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
220 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
221 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
222 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
223 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
224 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
225 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
226 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
227 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
228 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
229 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
230 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
231 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
232 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
233 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
234 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
235 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
236 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
237 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
238 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
239 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
240 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
241 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
242 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
243 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100244 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
245 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
246 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
247 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100248 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200249 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
250 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
251 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200252 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100253};
254
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100255/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100256static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100257/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100258static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100259
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200260/* list of all registered stats module */
261static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
262 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
263};
264
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200265static inline uint8_t stats_get_domain(uint32_t domain)
266{
267 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
268}
269
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200270static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
271{
272 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
273}
274
Christopher Faulet6338a082019-09-09 15:50:54 +0200275static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100276
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200277int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100278{
279 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100280 if (chk->data >= channel_htx_recv_max(chn, htx))
281 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200282 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100283 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100284 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100285 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100286 }
287 else {
288 if (ci_putchk(chn, chk) == -1)
289 return 0;
290 }
291 return 1;
292}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100293
Christopher Fauleted7a0662019-01-14 11:07:34 +0100294static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
295{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200296 struct channel *req = si_oc(si);
297 struct htx *htx = htxbuf(&req->buf);
298 struct htx_blk *blk;
299 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100300
Christopher Fauletb7f88902019-07-15 21:56:43 +0200301 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100302 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200303 ALREADY_CHECKED(blk);
304 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
305 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100306}
307
William Lallemand74c24fb2016-11-21 17:18:36 +0100308/*
309 * http_stats_io_handler()
310 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
311 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100312 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100313 * -> stats_dump_html_head() // emits the HTML headers
314 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
315 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
316 * -> stats_dump_html_px_hdr()
317 * -> stats_dump_fe_stats()
318 * -> stats_dump_li_stats()
319 * -> stats_dump_sv_stats()
320 * -> stats_dump_be_stats()
321 * -> stats_dump_html_px_end()
322 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100323 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100324 */
325
326
William Lallemand74c24fb2016-11-21 17:18:36 +0100327/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
328 * for clearing it if needed.
329 * NOTE: Some tools happen to rely on the field position instead of its name,
330 * so please only append new fields at the end, never in the middle.
331 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200332static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100333{
334 int field;
335
336 chunk_appendf(&trash, "# ");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200337 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200338 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100339
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200340 /* print special delimiter on proxy stats to mark end of
341 static fields */
342 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
343 chunk_appendf(&trash, "-,");
344 }
345 }
346
William Lallemand74c24fb2016-11-21 17:18:36 +0100347 chunk_appendf(&trash, "\n");
348}
349
350
351/* Emits a stats field without any surrounding element and properly encoded to
352 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
353 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200354int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100355{
356 switch (field_format(f, 0)) {
357 case FF_EMPTY: return 1;
358 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
359 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
360 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
361 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200362 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100363 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
364 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
365 }
366}
367
368/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
369 * output is supposed to be used on its own line. Returns non-zero on success, 0
370 * if the buffer is full.
371 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200372int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100373{
374 switch (field_format(f, 0)) {
375 case FF_EMPTY: return 1;
376 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
377 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
378 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
379 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200380 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100381 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
382 default: return chunk_appendf(out, "%08x:?", f->type);
383 }
384}
385
Simon Horman05ee2132017-01-04 09:37:25 +0100386/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
387 * the recommendation for interoperable integers in section 6 of RFC 7159.
388 */
389#define JSON_INT_MAX ((1ULL << 53) - 1)
390#define JSON_INT_MIN (0 - JSON_INT_MAX)
391
392/* Emits a stats field value and its type in JSON.
393 * Returns non-zero on success, 0 on error.
394 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200395int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100396{
397 int old_len;
398 char buf[20];
399 const char *type, *value = buf, *quote = "";
400
401 switch (field_format(f, 0)) {
402 case FF_EMPTY: return 1;
403 case FF_S32: type = "\"s32\"";
404 snprintf(buf, sizeof(buf), "%d", f->u.s32);
405 break;
406 case FF_U32: type = "\"u32\"";
407 snprintf(buf, sizeof(buf), "%u", f->u.u32);
408 break;
409 case FF_S64: type = "\"s64\"";
410 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
411 return 0;
412 type = "\"s64\"";
413 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
414 break;
415 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
416 return 0;
417 type = "\"u64\"";
418 snprintf(buf, sizeof(buf), "%llu",
419 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200420 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200421 case FF_FLT: type = "\"flt\"";
422 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100423 break;
424 case FF_STR: type = "\"str\"";
425 value = field_str(f, 0);
426 quote = "\"";
427 break;
428 default: snprintf(buf, sizeof(buf), "%u", f->type);
429 type = buf;
430 value = "unknown";
431 quote = "\"";
432 break;
433 }
434
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200435 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100436 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
437 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200438 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100439}
440
William Lallemand74c24fb2016-11-21 17:18:36 +0100441/* Emits an encoding of the field type on 3 characters followed by a delimiter.
442 * Returns non-zero on success, 0 if the buffer is full.
443 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200444int stats_emit_field_tags(struct buffer *out, const struct field *f,
445 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100446{
447 char origin, nature, scope;
448
449 switch (field_origin(f, 0)) {
450 case FO_METRIC: origin = 'M'; break;
451 case FO_STATUS: origin = 'S'; break;
452 case FO_KEY: origin = 'K'; break;
453 case FO_CONFIG: origin = 'C'; break;
454 case FO_PRODUCT: origin = 'P'; break;
455 default: origin = '?'; break;
456 }
457
458 switch (field_nature(f, 0)) {
459 case FN_GAUGE: nature = 'G'; break;
460 case FN_LIMIT: nature = 'L'; break;
461 case FN_MIN: nature = 'm'; break;
462 case FN_MAX: nature = 'M'; break;
463 case FN_RATE: nature = 'R'; break;
464 case FN_COUNTER: nature = 'C'; break;
465 case FN_DURATION: nature = 'D'; break;
466 case FN_AGE: nature = 'A'; break;
467 case FN_TIME: nature = 'T'; break;
468 case FN_NAME: nature = 'N'; break;
469 case FN_OUTPUT: nature = 'O'; break;
470 case FN_AVG: nature = 'a'; break;
471 default: nature = '?'; break;
472 }
473
474 switch (field_scope(f, 0)) {
475 case FS_PROCESS: scope = 'P'; break;
476 case FS_SERVICE: scope = 'S'; break;
477 case FS_SYSTEM: scope = 's'; break;
478 case FS_CLUSTER: scope = 'C'; break;
479 default: scope = '?'; break;
480 }
481
482 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
483}
484
Simon Horman05ee2132017-01-04 09:37:25 +0100485/* Emits an encoding of the field type as JSON.
486 * Returns non-zero on success, 0 if the buffer is full.
487 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200488int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100489{
490 const char *origin, *nature, *scope;
491 int old_len;
492
493 switch (field_origin(f, 0)) {
494 case FO_METRIC: origin = "Metric"; break;
495 case FO_STATUS: origin = "Status"; break;
496 case FO_KEY: origin = "Key"; break;
497 case FO_CONFIG: origin = "Config"; break;
498 case FO_PRODUCT: origin = "Product"; break;
499 default: origin = "Unknown"; break;
500 }
501
502 switch (field_nature(f, 0)) {
503 case FN_GAUGE: nature = "Gauge"; break;
504 case FN_LIMIT: nature = "Limit"; break;
505 case FN_MIN: nature = "Min"; break;
506 case FN_MAX: nature = "Max"; break;
507 case FN_RATE: nature = "Rate"; break;
508 case FN_COUNTER: nature = "Counter"; break;
509 case FN_DURATION: nature = "Duration"; break;
510 case FN_AGE: nature = "Age"; break;
511 case FN_TIME: nature = "Time"; break;
512 case FN_NAME: nature = "Name"; break;
513 case FN_OUTPUT: nature = "Output"; break;
514 case FN_AVG: nature = "Avg"; break;
515 default: nature = "Unknown"; break;
516 }
517
518 switch (field_scope(f, 0)) {
519 case FS_PROCESS: scope = "Process"; break;
520 case FS_SERVICE: scope = "Service"; break;
521 case FS_SYSTEM: scope = "System"; break;
522 case FS_CLUSTER: scope = "Cluster"; break;
523 default: scope = "Unknown"; break;
524 }
525
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200526 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100527 chunk_appendf(out, "\"tags\":{"
528 "\"origin\":\"%s\","
529 "\"nature\":\"%s\","
530 "\"scope\":\"%s\""
531 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200532 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100533}
William Lallemand74c24fb2016-11-21 17:18:36 +0100534
535/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200536static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200537 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200538 unsigned int flags,
539 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100540{
541 int field;
542
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200543 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100544 if (!stats_emit_raw_data_field(out, &stats[field]))
545 return 0;
546 if (!chunk_strcat(out, ","))
547 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200548
549 /* print special delimiter on proxy stats to mark end of
550 static fields */
551 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
552 if (!chunk_strcat(out, "-,"))
553 return 0;
554 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100555 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200556
William Lallemand74c24fb2016-11-21 17:18:36 +0100557 chunk_strcat(out, "\n");
558 return 1;
559}
560
561/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200562static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200563 const struct field *stats,
564 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200565 unsigned int flags,
566 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100567{
568 int field;
569
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200570 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100571 if (!stats[field].type)
572 continue;
573
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200574 switch (domain) {
575 case STATS_DOMAIN_PROXY:
576 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
577 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
578 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
579 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
580 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
581 '?',
582 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
583 field,
584 stat_fields[field].name,
585 stats[ST_F_PID].u.u32);
586 break;
587
588 default:
589 break;
590 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100591
592 if (!stats_emit_field_tags(out, &stats[field], ':'))
593 return 0;
594 if (!stats_emit_typed_data_field(out, &stats[field]))
595 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200596 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
597 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100598 if (!chunk_strcat(out, "\n"))
599 return 0;
600 }
601 return 1;
602}
603
Simon Horman05ee2132017-01-04 09:37:25 +0100604/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200605static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200606 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100607{
608 int field;
609 int started = 0;
610
611 if (!chunk_strcat(out, "["))
612 return 0;
613
614 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
615 int old_len;
616
617 if (!field_format(info, field))
618 continue;
619
620 if (started && !chunk_strcat(out, ","))
621 goto err;
622 started = 1;
623
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200624 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100625 chunk_appendf(out,
626 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
627 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200628 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100629 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200630 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100631 goto err;
632
633 if (!stats_emit_json_field_tags(out, &info[field]))
634 goto err;
635
636 if (!stats_emit_json_data_field(out, &info[field]))
637 goto err;
638
639 if (!chunk_strcat(out, "}"))
640 goto err;
641 }
642
643 if (!chunk_strcat(out, "]"))
644 goto err;
645 return 1;
646
647err:
648 chunk_reset(out);
649 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
650 return 0;
651}
652
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200653static void stats_print_proxy_field_json(struct buffer *out,
654 const struct field *stat,
655 const char *name,
656 int pos,
657 uint32_t field_type,
658 uint32_t iid,
659 uint32_t sid,
660 uint32_t pid)
661{
662 const char *obj_type;
663 switch (field_type) {
664 case STATS_TYPE_FE: obj_type = "Frontend"; break;
665 case STATS_TYPE_BE: obj_type = "Backend"; break;
666 case STATS_TYPE_SO: obj_type = "Listener"; break;
667 case STATS_TYPE_SV: obj_type = "Server"; break;
668 default: obj_type = "Unknown"; break;
669 }
670
671 chunk_appendf(out,
672 "{"
673 "\"objType\":\"%s\","
674 "\"proxyId\":%u,"
675 "\"id\":%u,"
676 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
677 "\"processNum\":%u,",
678 obj_type, iid, sid, pos, name, pid);
679}
680
Simon Horman05ee2132017-01-04 09:37:25 +0100681/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200682static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200683 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200684 unsigned int flags,
685 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100686{
687 int field;
688 int started = 0;
689
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200690 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100691 return 0;
692 if (!chunk_strcat(out, "["))
693 return 0;
694
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200695 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100696 int old_len;
697
698 if (!stats[field].type)
699 continue;
700
701 if (started && !chunk_strcat(out, ","))
702 goto err;
703 started = 1;
704
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200705 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200706 if (domain == STATS_DOMAIN_PROXY) {
707 stats_print_proxy_field_json(out, &stats[field],
708 stat_fields[field].name, field,
709 stats[ST_F_TYPE].u.u32,
710 stats[ST_F_IID].u.u32,
711 stats[ST_F_SID].u.u32,
712 stats[ST_F_PID].u.u32);
713 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200714
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200715 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100716 goto err;
717
718 if (!stats_emit_json_field_tags(out, &stats[field]))
719 goto err;
720
721 if (!stats_emit_json_data_field(out, &stats[field]))
722 goto err;
723
724 if (!chunk_strcat(out, "}"))
725 goto err;
726 }
727
728 if (!chunk_strcat(out, "]"))
729 goto err;
730
731 return 1;
732
733err:
734 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200735 if (flags & STAT_STARTED)
736 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100737 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
738 return 0;
739}
740
William Lallemand74c24fb2016-11-21 17:18:36 +0100741/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200742 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200743 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100744 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200745static int stats_dump_fields_html(struct buffer *out,
746 const struct field *stats,
747 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100748{
Willy Tarreau83061a82018-07-13 11:56:34 +0200749 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100750
751 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
752 chunk_appendf(out,
753 /* name, queue */
754 "<tr class=\"frontend\">");
755
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200756 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100757 /* Column sub-heading for Enable or Disable server */
758 chunk_appendf(out, "<td></td>");
759 }
760
761 chunk_appendf(out,
762 "<td class=ac>"
763 "<a name=\"%s/Frontend\"></a>"
764 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
765 "<td colspan=3></td>"
766 "",
767 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
768
769 chunk_appendf(out,
770 /* sessions rate : current */
771 "<td><u>%s<div class=tips><table class=det>"
772 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
773 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
774 "",
775 U2H(stats[ST_F_RATE].u.u32),
776 U2H(stats[ST_F_CONN_RATE].u.u32),
777 U2H(stats[ST_F_RATE].u.u32));
778
779 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
780 chunk_appendf(out,
781 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
782 U2H(stats[ST_F_REQ_RATE].u.u32));
783
784 chunk_appendf(out,
785 "</table></div></u></td>"
786 /* sessions rate : max */
787 "<td><u>%s<div class=tips><table class=det>"
788 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
789 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
790 "",
791 U2H(stats[ST_F_RATE_MAX].u.u32),
792 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
793 U2H(stats[ST_F_RATE_MAX].u.u32));
794
795 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
796 chunk_appendf(out,
797 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
798 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
799
800 chunk_appendf(out,
801 "</table></div></u></td>"
802 /* sessions rate : limit */
803 "<td>%s</td>",
804 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
805
806 chunk_appendf(out,
807 /* sessions: current, max, limit, total */
808 "<td>%s</td><td>%s</td><td>%s</td>"
809 "<td><u>%s<div class=tips><table class=det>"
810 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
811 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
812 "",
813 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
814 U2H(stats[ST_F_STOT].u.u64),
815 U2H(stats[ST_F_CONN_TOT].u.u64),
816 U2H(stats[ST_F_STOT].u.u64));
817
818 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
819 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
820 chunk_appendf(out,
821 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
822 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
823 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
824 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
825 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
826 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
827 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
828 "<tr><th>- other responses:</th><td>%s</td></tr>"
829 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100830 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
831 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200832 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100833 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100834 "",
835 U2H(stats[ST_F_REQ_TOT].u.u64),
836 U2H(stats[ST_F_HRSP_1XX].u.u64),
837 U2H(stats[ST_F_HRSP_2XX].u.u64),
838 U2H(stats[ST_F_COMP_RSP].u.u64),
839 stats[ST_F_HRSP_2XX].u.u64 ?
840 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
841 U2H(stats[ST_F_HRSP_3XX].u.u64),
842 U2H(stats[ST_F_HRSP_4XX].u.u64),
843 U2H(stats[ST_F_HRSP_5XX].u.u64),
844 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200845 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100846 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
847 U2H(stats[ST_F_CACHE_HITS].u.u64),
848 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
849 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100850 U2H(stats[ST_F_WREW].u.u64),
851 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100852 }
853
854 chunk_appendf(out,
855 "</table></div></u></td>"
856 /* sessions: lbtot, lastsess */
857 "<td></td><td></td>"
858 /* bytes : in */
859 "<td>%s</td>"
860 "",
861 U2H(stats[ST_F_BIN].u.u64));
862
863 chunk_appendf(out,
864 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
865 "<td>%s%s<div class=tips><table class=det>"
866 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
867 "<tr><th>Compression in:</th><td>%s</td></tr>"
868 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
869 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
870 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
871 "</table></div>%s</td>",
872 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
873 U2H(stats[ST_F_BOUT].u.u64),
874 U2H(stats[ST_F_BOUT].u.u64),
875 U2H(stats[ST_F_COMP_IN].u.u64),
876 U2H(stats[ST_F_COMP_OUT].u.u64),
877 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
878 U2H(stats[ST_F_COMP_BYP].u.u64),
879 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
880 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,
881 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
882
883 chunk_appendf(out,
884 /* denied: req, resp */
885 "<td>%s</td><td>%s</td>"
886 /* errors : request, connect, response */
887 "<td>%s</td><td></td><td></td>"
888 /* warnings: retries, redispatches */
889 "<td></td><td></td>"
890 /* server status : reflect frontend status */
891 "<td class=ac>%s</td>"
892 /* rest of server: nothing */
893 "<td class=ac colspan=8></td></tr>"
894 "",
895 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
896 U2H(stats[ST_F_EREQ].u.u64),
897 field_str(stats, ST_F_STATUS));
898 }
899 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
900 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200901 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100902 /* Column sub-heading for Enable or Disable server */
903 chunk_appendf(out, "<td></td>");
904 }
905
906 chunk_appendf(out,
907 /* frontend name, listener name */
908 "<td class=ac><a name=\"%s/+%s\"></a>%s"
909 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
910 "",
911 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200912 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100913 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
914
Willy Tarreau708c4162019-10-09 10:19:16 +0200915 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100916 chunk_appendf(out, "<div class=tips>");
917
Willy Tarreau90807112020-02-25 08:16:33 +0100918 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100919 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
920 else if (*field_str(stats, ST_F_ADDR) == '[')
921 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
922 else if (*field_str(stats, ST_F_ADDR))
923 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
924
925 /* id */
926 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
927 }
928
929 chunk_appendf(out,
930 /* queue */
931 "%s</td><td colspan=3></td>"
932 /* sessions rate: current, max, limit */
933 "<td colspan=3>&nbsp;</td>"
934 /* sessions: current, max, limit, total, lbtot, lastsess */
935 "<td>%s</td><td>%s</td><td>%s</td>"
936 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
937 /* bytes: in, out */
938 "<td>%s</td><td>%s</td>"
939 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200940 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100941 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
942 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
943
944 chunk_appendf(out,
945 /* denied: req, resp */
946 "<td>%s</td><td>%s</td>"
947 /* errors: request, connect, response */
948 "<td>%s</td><td></td><td></td>"
949 /* warnings: retries, redispatches */
950 "<td></td><td></td>"
951 /* server status: reflect listener status */
952 "<td class=ac>%s</td>"
953 /* rest of server: nothing */
954 "<td class=ac colspan=8></td></tr>"
955 "",
956 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
957 U2H(stats[ST_F_EREQ].u.u64),
958 field_str(stats, ST_F_STATUS));
959 }
960 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
961 const char *style;
962
963 /* determine the style to use depending on the server's state,
964 * its health and weight. There isn't a 1-to-1 mapping between
965 * state and styles for the cases where the server is (still)
966 * up. The reason is that we don't want to report nolb and
967 * drain with the same color.
968 */
969
970 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
971 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
972 style = "down";
973 }
974 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
975 style = "going_up";
976 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400977 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
978 style = "draining";
979 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100980 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
981 style = "going_down";
982 }
983 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
984 style = "nolb";
985 }
986 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
987 style = "no_check";
988 }
989 else if (!stats[ST_F_CHKFAIL].type ||
990 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
991 /* no check or max health = UP */
992 if (stats[ST_F_WEIGHT].u.u32)
993 style = "up";
994 else
995 style = "draining";
996 }
997 else {
998 style = "going_down";
999 }
1000
Willy Tarreau7b524852020-08-11 10:26:36 +02001001 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001002 chunk_appendf(out, "<tr class=\"maintain\">");
1003 else
1004 chunk_appendf(out,
1005 "<tr class=\"%s_%s\">",
1006 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1007
1008
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001009 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001010 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001011 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1012 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001013 field_str(stats, ST_F_SVNAME));
1014
1015 chunk_appendf(out,
1016 "<td class=ac><a name=\"%s/%s\"></a>%s"
1017 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1018 "",
1019 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001020 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001021 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1022
Willy Tarreau708c4162019-10-09 10:19:16 +02001023 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001024 chunk_appendf(out, "<div class=tips>");
1025
Willy Tarreau90807112020-02-25 08:16:33 +01001026 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001027 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1028 else if (*field_str(stats, ST_F_ADDR) == '[')
1029 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1030 else if (*field_str(stats, ST_F_ADDR))
1031 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1032
1033 /* id */
1034 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1035
1036 /* cookie */
1037 if (stats[ST_F_COOKIE].type) {
1038 chunk_appendf(out, ", cookie: '");
1039 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1040 chunk_htmlencode(out, &src);
1041 chunk_appendf(out, "'");
1042 }
1043
1044 chunk_appendf(out, "</div>");
1045 }
1046
1047 chunk_appendf(out,
1048 /* queue : current, max, limit */
1049 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1050 /* sessions rate : current, max, limit */
1051 "<td>%s</td><td>%s</td><td></td>"
1052 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001053 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001054 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1055 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1056
1057 chunk_appendf(out,
1058 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001059 "<td><u>%s<div class=tips>"
1060 "<table class=det>"
1061 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001062 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001063 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001064 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1065 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001066 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001067 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1068 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1069 "</table></div></u>"
1070 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001071 "<td><u>%s<div class=tips><table class=det>"
1072 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1073 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001074 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001075 U2H(stats[ST_F_SCUR].u.u32),
1076 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1077 U2H(stats[ST_F_SRV_ICUR].u.u32),
1078 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1079 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001080 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001081
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001082 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1083 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1084 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001085 U2H(stats[ST_F_STOT].u.u64),
1086 U2H(stats[ST_F_STOT].u.u64));
1087
1088 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1089 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001090 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001091 "<tr><th>New connections:</th><td>%s</td></tr>"
1092 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001093 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001094 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1095 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1096 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1097 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1098 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1099 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001100 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001101 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001102 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001103 U2H(stats[ST_F_CONNECT].u.u64),
1104 U2H(stats[ST_F_REUSE].u.u64),
1105 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1106 (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 +02001107 U2H(stats[ST_F_REQ_TOT].u.u64),
1108 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1109 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1110 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1111 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1112 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1113 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1114 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1115 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1116 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1117 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1118 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1119 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001120 U2H(stats[ST_F_WREW].u.u64),
1121 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001122 }
1123
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001124 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1125 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1126 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1127 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1128 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001129 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001130 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1131 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1132 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1133 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001134
1135 chunk_appendf(out,
1136 "</table></div></u></td>"
1137 /* sessions: lbtot, last */
1138 "<td>%s</td><td>%s</td>",
1139 U2H(stats[ST_F_LBTOT].u.u64),
1140 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1141
1142 chunk_appendf(out,
1143 /* bytes : in, out */
1144 "<td>%s</td><td>%s</td>"
1145 /* denied: req, resp */
1146 "<td></td><td>%s</td>"
1147 /* errors : request, connect */
1148 "<td></td><td>%s</td>"
1149 /* errors : response */
1150 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1151 /* warnings: retries, redispatches */
1152 "<td>%lld</td><td>%lld</td>"
1153 "",
1154 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1155 U2H(stats[ST_F_DRESP].u.u64),
1156 U2H(stats[ST_F_ECON].u.u64),
1157 U2H(stats[ST_F_ERESP].u.u64),
1158 (long long)stats[ST_F_CLI_ABRT].u.u64,
1159 (long long)stats[ST_F_SRV_ABRT].u.u64,
1160 (long long)stats[ST_F_WRETR].u.u64,
1161 (long long)stats[ST_F_WREDIS].u.u64);
1162
1163 /* status, last change */
1164 chunk_appendf(out, "<td class=ac>");
1165
1166 /* FIXME!!!!
1167 * LASTCHG should contain the last change for *this* server and must be computed
1168 * properly above, as was done below, ie: this server if maint, otherwise ref server
1169 * if tracking. Note that ref is either local or remote depending on tracking.
1170 */
1171
1172
Willy Tarreau7b524852020-08-11 10:26:36 +02001173 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001174 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1175 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001176 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001177 chunk_strcat(out, "<i>no check</i>");
1178 }
1179 else {
1180 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreau7b524852020-08-11 10:26:36 +02001181 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001182 if (stats[ST_F_CHECK_HEALTH].u.u32)
1183 chunk_strcat(out, " &uarr;");
1184 }
1185 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1186 chunk_strcat(out, " &darr;");
1187 }
1188
Willy Tarreau7b524852020-08-11 10:26:36 +02001189 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001190 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1191 chunk_appendf(out,
1192 "</td><td class=ac><u> %s",
1193 field_str(stats, ST_F_AGENT_STATUS));
1194
1195 if (stats[ST_F_AGENT_CODE].type)
1196 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1197
1198 if (stats[ST_F_AGENT_DURATION].type)
1199 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1200
1201 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1202
1203 if (*field_str(stats, ST_F_LAST_AGT)) {
1204 chunk_appendf(out, ": ");
1205 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1206 chunk_htmlencode(out, &src);
1207 }
1208 chunk_appendf(out, "</div></u>");
1209 }
1210 else if (stats[ST_F_CHECK_STATUS].type) {
1211 chunk_appendf(out,
1212 "</td><td class=ac><u> %s",
1213 field_str(stats, ST_F_CHECK_STATUS));
1214
1215 if (stats[ST_F_CHECK_CODE].type)
1216 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1217
1218 if (stats[ST_F_CHECK_DURATION].type)
1219 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1220
1221 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1222
1223 if (*field_str(stats, ST_F_LAST_CHK)) {
1224 chunk_appendf(out, ": ");
1225 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1226 chunk_htmlencode(out, &src);
1227 }
1228 chunk_appendf(out, "</div></u>");
1229 }
1230 else
1231 chunk_appendf(out, "</td><td>");
1232
1233 chunk_appendf(out,
1234 /* weight */
1235 "</td><td class=ac>%d</td>"
1236 /* act, bck */
1237 "<td class=ac>%s</td><td class=ac>%s</td>"
1238 "",
1239 stats[ST_F_WEIGHT].u.u32,
1240 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1241 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1242
1243 /* check failures: unique, fatal, down time */
1244 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1245 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1246 }
1247 else if (stats[ST_F_CHKFAIL].type) {
1248 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1249
1250 if (stats[ST_F_HANAFAIL].type)
1251 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1252
1253 chunk_appendf(out,
1254 "<div class=tips>Failed Health Checks%s</div></u></td>"
1255 "<td>%lld</td><td>%s</td>"
1256 "",
1257 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1258 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1259 }
1260 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1261 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1262 chunk_appendf(out,
1263 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1264 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1265 }
1266 else
1267 chunk_appendf(out, "<td colspan=3></td>");
1268
1269 /* throttle */
1270 if (stats[ST_F_THROTTLE].type)
1271 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1272 else
1273 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1274 }
1275 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1276 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001277 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001278 /* Column sub-heading for Enable or Disable server */
1279 chunk_appendf(out, "<td></td>");
1280 }
1281 chunk_appendf(out,
1282 "<td class=ac>"
1283 /* name */
1284 "%s<a name=\"%s/Backend\"></a>"
1285 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1286 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001287 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001288 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1289
Willy Tarreau708c4162019-10-09 10:19:16 +02001290 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001291 /* balancing */
1292 chunk_appendf(out, "<div class=tips>balancing: %s",
1293 field_str(stats, ST_F_ALGO));
1294
1295 /* cookie */
1296 if (stats[ST_F_COOKIE].type) {
1297 chunk_appendf(out, ", cookie: '");
1298 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1299 chunk_htmlencode(out, &src);
1300 chunk_appendf(out, "'");
1301 }
1302 chunk_appendf(out, "</div>");
1303 }
1304
1305 chunk_appendf(out,
1306 "%s</td>"
1307 /* queue : current, max */
1308 "<td>%s</td><td>%s</td><td></td>"
1309 /* sessions rate : current, max, limit */
1310 "<td>%s</td><td>%s</td><td></td>"
1311 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001312 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001313 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1314 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1315
1316 chunk_appendf(out,
1317 /* sessions: current, max, limit, total */
1318 "<td>%s</td><td>%s</td><td>%s</td>"
1319 "<td><u>%s<div class=tips><table class=det>"
1320 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1321 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001322 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 +01001323 U2H(stats[ST_F_STOT].u.u64),
1324 U2H(stats[ST_F_STOT].u.u64));
1325
1326 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1327 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1328 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001329 "<tr><th>New connections:</th><td>%s</td></tr>"
1330 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001331 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1332 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1333 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1334 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1335 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1336 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1337 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1338 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001339 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1340 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001341 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001342 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001343 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001344 U2H(stats[ST_F_CONNECT].u.u64),
1345 U2H(stats[ST_F_REUSE].u.u64),
1346 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1347 (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 +01001348 U2H(stats[ST_F_REQ_TOT].u.u64),
1349 U2H(stats[ST_F_HRSP_1XX].u.u64),
1350 U2H(stats[ST_F_HRSP_2XX].u.u64),
1351 U2H(stats[ST_F_COMP_RSP].u.u64),
1352 stats[ST_F_HRSP_2XX].u.u64 ?
1353 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1354 U2H(stats[ST_F_HRSP_3XX].u.u64),
1355 U2H(stats[ST_F_HRSP_4XX].u.u64),
1356 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001357 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001358 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1359 U2H(stats[ST_F_CACHE_HITS].u.u64),
1360 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1361 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001362 U2H(stats[ST_F_WREW].u.u64),
1363 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001364 }
1365
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001366 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1367 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1368 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1369 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1370 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001371 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001372 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1373 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1374 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1375 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001376
1377 chunk_appendf(out,
1378 "</table></div></u></td>"
1379 /* sessions: lbtot, last */
1380 "<td>%s</td><td>%s</td>"
1381 /* bytes: in */
1382 "<td>%s</td>"
1383 "",
1384 U2H(stats[ST_F_LBTOT].u.u64),
1385 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1386 U2H(stats[ST_F_BIN].u.u64));
1387
1388 chunk_appendf(out,
1389 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1390 "<td>%s%s<div class=tips><table class=det>"
1391 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1392 "<tr><th>Compression in:</th><td>%s</td></tr>"
1393 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1394 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1395 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1396 "</table></div>%s</td>",
1397 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1398 U2H(stats[ST_F_BOUT].u.u64),
1399 U2H(stats[ST_F_BOUT].u.u64),
1400 U2H(stats[ST_F_COMP_IN].u.u64),
1401 U2H(stats[ST_F_COMP_OUT].u.u64),
1402 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1403 U2H(stats[ST_F_COMP_BYP].u.u64),
1404 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1405 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,
1406 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1407
1408 chunk_appendf(out,
1409 /* denied: req, resp */
1410 "<td>%s</td><td>%s</td>"
1411 /* errors : request, connect */
1412 "<td></td><td>%s</td>"
1413 /* errors : response */
1414 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1415 /* warnings: retries, redispatches */
1416 "<td>%lld</td><td>%lld</td>"
1417 /* backend status: reflect backend status (up/down): we display UP
1418 * if the backend has known working servers or if it has no server at
1419 * all (eg: for stats). Then we display the total weight, number of
1420 * active and backups. */
1421 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1422 "<td class=ac>%d</td><td class=ac>%d</td>"
1423 "",
1424 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1425 U2H(stats[ST_F_ECON].u.u64),
1426 U2H(stats[ST_F_ERESP].u.u64),
1427 (long long)stats[ST_F_CLI_ABRT].u.u64,
1428 (long long)stats[ST_F_SRV_ABRT].u.u64,
1429 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1430 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1431 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1432 stats[ST_F_WEIGHT].u.u32,
1433 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1434
1435 chunk_appendf(out,
1436 /* rest of backend: nothing, down transitions, total downtime, throttle */
1437 "<td class=ac>&nbsp;</td><td>%d</td>"
1438 "<td>%s</td>"
1439 "<td></td>"
1440 "</tr>",
1441 stats[ST_F_CHKDOWN].u.u32,
1442 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1443 }
1444 return 1;
1445}
1446
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001447int stats_dump_one_line(const struct field *stats, size_t stats_count,
1448 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001449{
Simon Horman05ee2132017-01-04 09:37:25 +01001450 int ret;
1451
William Lallemand74c24fb2016-11-21 17:18:36 +01001452 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001453 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001454 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001455 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001456 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001457 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001458 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001459 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001460
1461 if (ret)
1462 appctx->ctx.stats.flags |= STAT_STARTED;
1463
1464 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001465}
1466
1467/* Fill <stats> with the frontend statistics. <stats> is
1468 * preallocated array of length <len>. The length of the array
1469 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1470 * this value, the function returns 0, otherwise, it returns 1.
1471 */
1472int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1473{
1474 if (len < ST_F_TOTAL_FIELDS)
1475 return 0;
1476
1477 memset(stats, 0, sizeof(*stats) * len);
1478
1479 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1480 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1481 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1482 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1483 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1484 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1485 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1486 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1487 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1488 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1489 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1490 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1491 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1492 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1493 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1494 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1495 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1496 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1497 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1498 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1499 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1500 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001501 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001502 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001503
1504 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1505 if (px->mode == PR_MODE_HTTP) {
1506 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1507 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1508 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1509 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1510 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1511 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1512 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001513 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1514 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001515 }
1516
1517 /* requests : req_rate, req_rate_max, req_tot, */
1518 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1519 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1520 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1521
1522 /* compression: in, out, bypassed, responses */
1523 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1524 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1525 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1526 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1527
1528 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1529 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1530 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1531 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1532
1533 return 1;
1534}
1535
1536/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1537 * the state from stream interface <si>. The caller is responsible for clearing
1538 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1539 */
1540static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1541{
1542 struct appctx *appctx = __objt_appctx(si->end);
1543
1544 if (!(px->cap & PR_CAP_FE))
1545 return 0;
1546
1547 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1548 return 0;
1549
1550 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1551 return 0;
1552
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001553 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001554}
1555
1556/* Fill <stats> with the listener statistics. <stats> is
1557 * preallocated array of length <len>. The length of the array
1558 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1559 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001560 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001561 */
1562int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1563 struct field *stats, int len)
1564{
Willy Tarreau83061a82018-07-13 11:56:34 +02001565 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001566
1567 if (len < ST_F_TOTAL_FIELDS)
1568 return 0;
1569
1570 if (!l->counters)
1571 return 0;
1572
1573 chunk_reset(out);
1574 memset(stats, 0, sizeof(*stats) * len);
1575
1576 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1577 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1578 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1579 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1580 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1581 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1582 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1583 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1584 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1585 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1586 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1587 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1588 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1589 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001590 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 +01001591 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1592 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1593 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1594 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001595 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001596 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001597
Willy Tarreau708c4162019-10-09 10:19:16 +02001598 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001599 char str[INET6_ADDRSTRLEN];
1600 int port;
1601
Willy Tarreau37159062020-08-27 07:48:42 +02001602 port = get_host_port(&l->rx.addr);
1603 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001604 case AF_INET:
1605 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1606 chunk_appendf(out, "%s:%d", str, port);
1607 break;
1608 case AF_INET6:
1609 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1610 chunk_appendf(out, "[%s]:%d", str, port);
1611 break;
1612 case AF_UNIX:
1613 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1614 break;
1615 case -1:
1616 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1617 chunk_strcat(out, strerror(errno));
1618 break;
1619 default: /* address family not supported */
1620 break;
1621 }
1622 }
1623
1624 return 1;
1625}
1626
1627/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001628 * from stream interface <si>. The caller is responsible for clearing the trash
1629 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001630 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001631static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001632{
1633 struct appctx *appctx = __objt_appctx(si->end);
1634
Willy Tarreau43241ff2019-10-09 11:27:51 +02001635 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001636 return 0;
1637
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001638 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001639}
1640
1641enum srv_stats_state {
1642 SRV_STATS_STATE_DOWN = 0,
1643 SRV_STATS_STATE_DOWN_AGENT,
1644 SRV_STATS_STATE_GOING_UP,
1645 SRV_STATS_STATE_UP_GOING_DOWN,
1646 SRV_STATS_STATE_UP,
1647 SRV_STATS_STATE_NOLB_GOING_DOWN,
1648 SRV_STATS_STATE_NOLB,
1649 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1650 SRV_STATS_STATE_DRAIN,
1651 SRV_STATS_STATE_DRAIN_AGENT,
1652 SRV_STATS_STATE_NO_CHECK,
1653
1654 SRV_STATS_STATE_COUNT, /* Must be last */
1655};
1656
1657static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1658 [SRV_STATS_STATE_DOWN] = "DOWN",
1659 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1660 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1661 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1662 [SRV_STATS_STATE_UP] = "UP",
1663 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1664 [SRV_STATS_STATE_NOLB] = "NOLB",
1665 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1666 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1667 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1668 [SRV_STATS_STATE_NO_CHECK] = "no check"
1669};
1670
1671/* Fill <stats> with the server statistics. <stats> is
1672 * preallocated array of length <len>. The length of the array
1673 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1674 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001675 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001676 */
1677int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1678 struct field *stats, int len)
1679{
1680 struct server *via, *ref;
1681 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001682 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001683 enum srv_stats_state state;
1684 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001685 long long srv_samples_counter;
1686 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001687
1688 if (len < ST_F_TOTAL_FIELDS)
1689 return 0;
1690
1691 memset(stats, 0, sizeof(*stats) * len);
1692
1693 /* we have "via" which is the tracked server as described in the configuration,
1694 * and "ref" which is the checked server and the end of the chain.
1695 */
1696 via = sv->track ? sv->track : sv;
1697 ref = via;
1698 while (ref->track)
1699 ref = ref->track;
1700
Emeric Brun52a91d32017-08-31 14:41:55 +02001701 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001702 if ((ref->check.state & CHK_ST_ENABLED) &&
1703 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1704 state = SRV_STATS_STATE_UP_GOING_DOWN;
1705 } else {
1706 state = SRV_STATS_STATE_UP;
1707 }
1708
Emeric Brun52a91d32017-08-31 14:41:55 +02001709 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001710 if (ref->agent.state & CHK_ST_ENABLED)
1711 state = SRV_STATS_STATE_DRAIN_AGENT;
1712 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1713 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1714 else
1715 state = SRV_STATS_STATE_DRAIN;
1716 }
1717
1718 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1719 state = SRV_STATS_STATE_NO_CHECK;
1720 }
1721 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001722 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001723 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1724 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1725 state = SRV_STATS_STATE_NOLB;
1726 } else {
1727 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1728 }
1729 }
1730 else { /* stopped */
1731 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1732 state = SRV_STATS_STATE_DOWN_AGENT;
1733 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1734 state = SRV_STATS_STATE_DOWN; /* DOWN */
1735 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1736 state = SRV_STATS_STATE_GOING_UP;
1737 } else {
1738 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1739 }
1740 }
1741
1742 chunk_reset(out);
1743
1744 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1745 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1746 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1747 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1748 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1749 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1750 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1751
1752 if (sv->maxconn)
1753 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1754
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001755 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1756 if (sv->max_idle_conns != -1)
1757 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1758
William Lallemand74c24fb2016-11-21 17:18:36 +01001759 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1760 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1761 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001762 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001763 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1764 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1765 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1766 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001767 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001768 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001769 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1770 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001771
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001772 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1773 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1774 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001775 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001776
William Lallemand74c24fb2016-11-21 17:18:36 +01001777 /* status */
1778 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001779 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001780 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001781 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001782 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001783 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001784 chunk_appendf(out, "MAINT");
1785 else
1786 chunk_appendf(out,
1787 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001788 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1789 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001790
1791 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1792 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001793 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 +01001794 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1795 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1796
1797 /* check failures: unique, fatal; last change, total downtime */
1798 if (sv->check.state & CHK_ST_ENABLED) {
1799 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1800 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1801 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1802 }
1803
1804 if (sv->maxqueue)
1805 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1806
1807 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1808 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1809 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1810
Emeric Brun52a91d32017-08-31 14:41:55 +02001811 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001812 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1813
1814 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1815
1816 if (sv->track) {
1817 char *fld_track = chunk_newstr(out);
1818
1819 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1820 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1821 }
1822
1823 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1824 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1825 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1826
1827 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1828 const char *fld_chksts;
1829
1830 fld_chksts = chunk_newstr(out);
1831 chunk_strcat(out, "* "); // for check in progress
1832 chunk_strcat(out, get_check_status_info(sv->check.status));
1833 if (!(sv->check.state & CHK_ST_INPROGRESS))
1834 fld_chksts += 2; // skip "* "
1835 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1836
1837 if (sv->check.status >= HCHK_STATUS_L57DATA)
1838 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1839
1840 if (sv->check.status >= HCHK_STATUS_CHECKED)
1841 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1842
1843 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1844 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1845 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1846 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1847 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1848 }
1849
1850 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1851 const char *fld_chksts;
1852
1853 fld_chksts = chunk_newstr(out);
1854 chunk_strcat(out, "* "); // for check in progress
1855 chunk_strcat(out, get_check_status_info(sv->agent.status));
1856 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1857 fld_chksts += 2; // skip "* "
1858 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1859
1860 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1861 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1862
1863 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1864 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1865
1866 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1867 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1868 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1869 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1870 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1871 }
1872
1873 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1874 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001875 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001876 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1877 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1878 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1879 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1880 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1881 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1882 }
1883
1884 if (ref->observe)
1885 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1886
1887 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1888 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1889 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1890
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001891 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1892 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1893 srv_samples_window = srv_samples_counter;
1894
1895 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1896 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1897 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1898 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001899
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001900 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1901 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1902 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1903 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1904
Willy Tarreau708c4162019-10-09 10:19:16 +02001905 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001906 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1907 case AF_INET:
1908 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001909 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001910 break;
1911 case AF_INET6:
1912 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001913 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001914 break;
1915 case AF_UNIX:
1916 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1917 break;
1918 case -1:
1919 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1920 chunk_strcat(out, strerror(errno));
1921 break;
1922 default: /* address family not supported */
1923 break;
1924 }
1925
1926 if (sv->cookie)
1927 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1928 }
1929
1930 return 1;
1931}
1932
1933/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001934 * from stream interface <si>, and server state <state>. The caller is
1935 * responsible for clearing the trash if needed. Returns non-zero if it emits
1936 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001937 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001938static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001939{
1940 struct appctx *appctx = __objt_appctx(si->end);
1941
Willy Tarreau43241ff2019-10-09 11:27:51 +02001942 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001943 return 0;
1944
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001945 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001946}
1947
1948/* Fill <stats> with the backend statistics. <stats> is
1949 * preallocated array of length <len>. The length of the array
1950 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1951 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001952 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001953 */
1954int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1955{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001956 long long be_samples_counter;
1957 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1958
William Lallemand74c24fb2016-11-21 17:18:36 +01001959 if (len < ST_F_TOTAL_FIELDS)
1960 return 0;
1961
1962 memset(stats, 0, sizeof(*stats) * len);
1963
1964 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1965 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1966 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1967 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1968 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001969 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001970 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1971 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1972 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1973 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1974 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1975 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1976 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1977 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1978 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1979 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1980 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001981 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001982 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001983 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1984 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001985 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1986 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1987 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1988 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1989 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1990 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1991 if (px->srv)
1992 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1993
1994 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1995 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1996 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1997 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1998 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1999 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2000 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
2001
Willy Tarreau708c4162019-10-09 10:19:16 +02002002 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002003 if (px->cookie_name)
2004 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2005 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2006 }
2007
2008 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2009 if (px->mode == PR_MODE_HTTP) {
2010 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2011 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2012 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2013 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2014 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2015 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2016 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01002017 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2018 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01002019 }
2020
2021 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2022 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2023
2024 /* compression: in, out, bypassed, responses */
2025 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2026 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2027 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2028 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2029 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
2030
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002031 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2032 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2033 be_samples_window = be_samples_counter;
2034
2035 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2036 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2037 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2038 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 +01002039
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002040 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2041 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2042 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2043 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2044
William Lallemand74c24fb2016-11-21 17:18:36 +01002045 return 1;
2046}
2047
2048/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002049 * interface <si>. The caller is responsible for clearing the trash if needed.
2050 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002051 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002052static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002053{
2054 struct appctx *appctx = __objt_appctx(si->end);
2055
2056 if (!(px->cap & PR_CAP_BE))
2057 return 0;
2058
2059 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2060 return 0;
2061
Willy Tarreau43241ff2019-10-09 11:27:51 +02002062 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002063 return 0;
2064
Amaury Denoyelle97323c92020-10-02 18:32:01 +02002065 return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002066}
2067
2068/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2069 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2070 * for clearing the trash if needed.
2071 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002072static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002073{
2074 struct appctx *appctx = __objt_appctx(si->end);
2075 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2076
2077 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2078 /* A form to enable/disable this proxy servers */
2079
2080 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2081 scope_txt[0] = 0;
2082 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002083 const char *scope_ptr = stats_scope_ptr(appctx, si);
2084
William Lallemand74c24fb2016-11-21 17:18:36 +01002085 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002086 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002087 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2088 }
2089
2090 chunk_appendf(&trash,
2091 "<form method=\"post\">");
2092 }
2093
2094 /* print a new table */
2095 chunk_appendf(&trash,
2096 "<table class=\"tbl\" width=\"100%%\">\n"
2097 "<tr class=\"titre\">"
2098 "<th class=\"pxname\" width=\"10%%\">");
2099
2100 chunk_appendf(&trash,
2101 "<a name=\"%s\"></a>%s"
2102 "<a class=px href=\"#%s\">%s</a>",
2103 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002104 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002105 px->id, px->id);
2106
Willy Tarreau676c29e2019-10-09 10:50:01 +02002107 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002108 /* cap, mode, id */
2109 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2110 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2111 px->uuid);
2112 chunk_appendf(&trash, "</div>");
2113 }
2114
2115 chunk_appendf(&trash,
2116 "%s</th>"
2117 "<th class=\"%s\" width=\"90%%\">%s</th>"
2118 "</tr>\n"
2119 "</table>\n"
2120 "<table class=\"tbl\" width=\"100%%\">\n"
2121 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002122 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002123 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2124
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002125 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002126 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002127 if ((px->cap & PR_CAP_BE) && px->srv)
2128 chunk_appendf(&trash,
2129 "<th rowspan=2 width=1><input type=\"checkbox\" "
2130 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2131 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2132 px->id,
2133 px->id);
2134 else
2135 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002136 }
2137
2138 chunk_appendf(&trash,
2139 "<th rowspan=2></th>"
2140 "<th colspan=3>Queue</th>"
2141 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2142 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2143 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2144 "<th colspan=9>Server</th>"
2145 "</tr>\n"
2146 "<tr class=\"titre\">"
2147 "<th>Cur</th><th>Max</th><th>Limit</th>"
2148 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2149 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2150 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2151 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2152 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2153 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2154 "<th>Thrtle</th>\n"
2155 "</tr>");
2156}
2157
2158/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2159 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2160 */
2161static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2162{
2163 struct appctx *appctx = __objt_appctx(si->end);
2164 chunk_appendf(&trash, "</table>");
2165
2166 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2167 /* close the form used to enable/disable this proxy servers */
2168 chunk_appendf(&trash,
2169 "Choose the action to perform on the checked servers : "
2170 "<select name=action>"
2171 "<option value=\"\"></option>"
2172 "<option value=\"ready\">Set state to READY</option>"
2173 "<option value=\"drain\">Set state to DRAIN</option>"
2174 "<option value=\"maint\">Set state to MAINT</option>"
2175 "<option value=\"dhlth\">Health: disable checks</option>"
2176 "<option value=\"ehlth\">Health: enable checks</option>"
2177 "<option value=\"hrunn\">Health: force UP</option>"
2178 "<option value=\"hnolb\">Health: force NOLB</option>"
2179 "<option value=\"hdown\">Health: force DOWN</option>"
2180 "<option value=\"dagent\">Agent: disable checks</option>"
2181 "<option value=\"eagent\">Agent: enable checks</option>"
2182 "<option value=\"arunn\">Agent: force UP</option>"
2183 "<option value=\"adown\">Agent: force DOWN</option>"
2184 "<option value=\"shutdown\">Kill Sessions</option>"
2185 "</select>"
2186 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2187 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2188 "</form>",
2189 px->uuid);
2190 }
2191
2192 chunk_appendf(&trash, "<p>\n");
2193}
2194
2195/*
2196 * Dumps statistics for a proxy. The output is sent to the stream interface's
2197 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2198 * buffer space, or non-zero if everything completed. This function is used
2199 * both by the CLI and the HTTP entry points, and is able to dump the output
2200 * in HTML or CSV formats. If the later, <uri> must be NULL.
2201 */
Christopher Fauletef779222018-10-31 08:47:01 +01002202int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2203 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002204{
2205 struct appctx *appctx = __objt_appctx(si->end);
2206 struct stream *s = si_strm(si);
2207 struct channel *rep = si_ic(si);
2208 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2209 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002210
2211 chunk_reset(&trash);
2212
2213 switch (appctx->ctx.stats.px_st) {
2214 case STAT_PX_ST_INIT:
2215 /* we are on a new proxy */
2216 if (uri && uri->scope) {
2217 /* we have a limited scope, we have to check the proxy name */
2218 struct stat_scope *scope;
2219 int len;
2220
2221 len = strlen(px->id);
2222 scope = uri->scope;
2223
2224 while (scope) {
2225 /* match exact proxy name */
2226 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2227 break;
2228
2229 /* match '.' which means 'self' proxy */
2230 if (!strcmp(scope->px_id, ".") && px == s->be)
2231 break;
2232 scope = scope->next;
2233 }
2234
2235 /* proxy name not found : don't dump anything */
2236 if (scope == NULL)
2237 return 1;
2238 }
2239
2240 /* if the user has requested a limited output and the proxy
2241 * name does not match, skip it.
2242 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002243 if (appctx->ctx.stats.scope_len) {
2244 const char *scope_ptr = stats_scope_ptr(appctx, si);
2245
2246 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2247 return 1;
2248 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002249
2250 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2251 (appctx->ctx.stats.iid != -1) &&
2252 (px->uuid != appctx->ctx.stats.iid))
2253 return 1;
2254
2255 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2256 /* fall through */
2257
2258 case STAT_PX_ST_TH:
2259 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002260 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002261 if (!stats_putchk(rep, htx, &trash))
2262 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002263 }
2264
2265 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2266 /* fall through */
2267
2268 case STAT_PX_ST_FE:
2269 /* print the frontend */
2270 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002271 if (!stats_putchk(rep, htx, &trash))
2272 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002273 }
2274
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002275 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01002276 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2277 /* fall through */
2278
2279 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002280 /* obj2 points to listeners list as initialized above */
2281 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002282 if (htx) {
2283 if (htx_almost_full(htx))
2284 goto full;
2285 }
2286 else {
2287 if (buffer_almost_full(&rep->buf))
2288 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002289 }
2290
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002291 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01002292 if (!l->counters)
2293 continue;
2294
2295 if (appctx->ctx.stats.flags & STAT_BOUND) {
2296 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2297 break;
2298
2299 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2300 continue;
2301 }
2302
2303 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002304 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002305 if (!stats_putchk(rep, htx, &trash))
2306 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002307 }
2308 }
2309
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002310 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01002311 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2312 /* fall through */
2313
2314 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002315 /* obj2 points to servers list as initialized above */
2316 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002317 if (htx) {
2318 if (htx_almost_full(htx))
2319 goto full;
2320 }
2321 else {
2322 if (buffer_almost_full(&rep->buf))
2323 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002324 }
2325
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002326 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01002327
2328 if (appctx->ctx.stats.flags & STAT_BOUND) {
2329 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2330 break;
2331
2332 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2333 continue;
2334 }
2335
2336 svs = sv;
2337 while (svs->track)
2338 svs = svs->track;
2339
2340 /* do not report servers which are DOWN and not changing state */
2341 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002342 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2343 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002344 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2345 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2346 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2347 continue;
2348 }
2349
Willy Tarreau43241ff2019-10-09 11:27:51 +02002350 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002351 if (!stats_putchk(rep, htx, &trash))
2352 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002353 }
2354 } /* for sv */
2355
2356 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2357 /* fall through */
2358
2359 case STAT_PX_ST_BE:
2360 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002361 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002362 if (!stats_putchk(rep, htx, &trash))
2363 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002364 }
2365
2366 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2367 /* fall through */
2368
2369 case STAT_PX_ST_END:
2370 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2371 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002372 if (!stats_putchk(rep, htx, &trash))
2373 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002374 }
2375
2376 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2377 /* fall through */
2378
2379 case STAT_PX_ST_FIN:
2380 return 1;
2381
2382 default:
2383 /* unknown state, we should put an abort() here ! */
2384 return 1;
2385 }
Christopher Fauletef779222018-10-31 08:47:01 +01002386
2387 full:
2388 si_rx_room_blk(si);
2389 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002390}
2391
2392/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2393 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2394 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002395static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002396{
2397 /* WARNING! This must fit in the first buffer !!! */
2398 chunk_appendf(&trash,
2399 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2400 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2401 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03002402 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002403 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2404 "<style type=\"text/css\"><!--\n"
2405 "body {"
2406 " font-family: arial, helvetica, sans-serif;"
2407 " font-size: 12px;"
2408 " font-weight: normal;"
2409 " color: black;"
2410 " background: white;"
2411 "}\n"
2412 "th,td {"
2413 " font-size: 10px;"
2414 "}\n"
2415 "h1 {"
2416 " font-size: x-large;"
2417 " margin-bottom: 0.5em;"
2418 "}\n"
2419 "h2 {"
2420 " font-family: helvetica, arial;"
2421 " font-size: x-large;"
2422 " font-weight: bold;"
2423 " font-style: italic;"
2424 " color: #6020a0;"
2425 " margin-top: 0em;"
2426 " margin-bottom: 0em;"
2427 "}\n"
2428 "h3 {"
2429 " font-family: helvetica, arial;"
2430 " font-size: 16px;"
2431 " font-weight: bold;"
2432 " color: #b00040;"
2433 " background: #e8e8d0;"
2434 " margin-top: 0em;"
2435 " margin-bottom: 0em;"
2436 "}\n"
2437 "li {"
2438 " margin-top: 0.25em;"
2439 " margin-right: 2em;"
2440 "}\n"
2441 ".hr {margin-top: 0.25em;"
2442 " border-color: black;"
2443 " border-bottom-style: solid;"
2444 "}\n"
2445 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2446 ".total {background: #20D0D0;color: #ffff80;}\n"
2447 ".frontend {background: #e8e8d0;}\n"
2448 ".socket {background: #d0d0d0;}\n"
2449 ".backend {background: #e8e8d0;}\n"
2450 ".active_down {background: #ff9090;}\n"
2451 ".active_going_up {background: #ffd020;}\n"
2452 ".active_going_down {background: #ffffa0;}\n"
2453 ".active_up {background: #c0ffc0;}\n"
2454 ".active_nolb {background: #20a0ff;}\n"
2455 ".active_draining {background: #20a0FF;}\n"
2456 ".active_no_check {background: #e0e0e0;}\n"
2457 ".backup_down {background: #ff9090;}\n"
2458 ".backup_going_up {background: #ff80ff;}\n"
2459 ".backup_going_down {background: #c060ff;}\n"
2460 ".backup_up {background: #b0d0ff;}\n"
2461 ".backup_nolb {background: #90b0e0;}\n"
2462 ".backup_draining {background: #cc9900;}\n"
2463 ".backup_no_check {background: #e0e0e0;}\n"
2464 ".maintain {background: #c07820;}\n"
2465 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2466 "\n"
2467 "a.px:link {color: #ffff40; text-decoration: none;}"
2468 "a.px:visited {color: #ffff40; text-decoration: none;}"
2469 "a.px:hover {color: #ffffff; text-decoration: none;}"
2470 "a.lfsb:link {color: #000000; text-decoration: none;}"
2471 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2472 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2473 "\n"
2474 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2475 "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"
2476 "table.tbl td.ac { text-align: center;}\n"
2477 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2478 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2479 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2480 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2481 "\n"
2482 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2483 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2484 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2485 "table.det { border-collapse: collapse; border-style: none; }\n"
2486 "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"
2487 "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"
2488 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2489 "div.tips {\n"
2490 " display:block;\n"
2491 " visibility:hidden;\n"
2492 " z-index:2147483647;\n"
2493 " position:absolute;\n"
2494 " padding:2px 4px 3px;\n"
2495 " background:#f0f060; color:#000000;\n"
2496 " border:1px solid #7040c0;\n"
2497 " white-space:nowrap;\n"
2498 " font-style:normal;font-size:11px;font-weight:normal;\n"
2499 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2500 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2501 "}\n"
2502 "u:hover div.tips {visibility:visible;}\n"
2503 "-->\n"
2504 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002505 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002506 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002507 );
2508}
2509
2510/* Dumps the HTML stats information block to the trash for and uses the state from
2511 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2512 * for clearing the trash if needed.
2513 */
2514static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2515{
2516 struct appctx *appctx = __objt_appctx(si->end);
2517 unsigned int up = (now.tv_sec - start_date.tv_sec);
2518 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002519 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002520 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2521
2522 /* Turn the bytes per second to bits per second and take care of the
2523 * usual ethernet overhead in order to help figure how far we are from
2524 * interface saturation since it's the only case which usually matters.
2525 * For this we count the total size of an Ethernet frame on the wire
2526 * including preamble and IFG (1538) for the largest TCP segment it
2527 * transports (1448 with TCP timestamps). This is not valid for smaller
2528 * packets (under-estimated), but it gives a reasonably accurate
2529 * estimation of how far we are from uplink saturation.
2530 */
2531 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002532
2533 /* WARNING! this has to fit the first packet too.
2534 * We are around 3.5 kB, add adding entries will
2535 * become tricky if we want to support 4kB buffers !
2536 */
2537 chunk_appendf(&trash,
2538 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2539 PRODUCT_NAME "%s</a></h1>\n"
2540 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2541 "<hr width=\"100%%\" class=\"hr\">\n"
2542 "<h3>&gt; General process information</h3>\n"
2543 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002544 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002545 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2546 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2547 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002548 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002549 "Running tasks: %d/%d; idle = %d %%<br>\n"
2550 "</td><td align=\"center\" nowrap>\n"
2551 "<table class=\"lgd\"><tr>\n"
2552 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2553 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2554 "</tr><tr>\n"
2555 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2556 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2557 "</tr><tr>\n"
2558 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2559 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2560 "</tr><tr>\n"
2561 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2562 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2563 "</tr><tr>\n"
2564 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2565 "</tr><tr>\n"
2566 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2567 "</tr></table>\n"
2568 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2569 "</td>"
2570 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2571 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2572 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002573 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2574 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2575 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2576 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2577 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002578 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002579 up / 86400, (up % 86400) / 3600,
2580 (up % 3600) / 60, (up % 60),
2581 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2582 global.rlimit_memmax ? " MB" : "",
2583 global.rlimit_nofile,
2584 global.maxsock, global.maxconn, global.maxpipes,
2585 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002586 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2587 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002588 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002589 );
2590
2591 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002592 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002593 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2594
2595 chunk_appendf(&trash,
2596 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2597 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2598 STAT_SCOPE_TXT_MAXLEN);
2599
2600 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2601 scope_txt[0] = 0;
2602 if (appctx->ctx.stats.scope_len) {
2603 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002604 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002605 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2606 }
2607
2608 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2609 chunk_appendf(&trash,
2610 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2611 uri->uri_prefix,
2612 "",
2613 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2614 scope_txt);
2615 else
2616 chunk_appendf(&trash,
2617 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2618 uri->uri_prefix,
2619 ";up",
2620 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2621 scope_txt);
2622
2623 if (uri->refresh > 0) {
2624 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2625 chunk_appendf(&trash,
2626 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2627 uri->uri_prefix,
2628 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2629 "",
2630 scope_txt);
2631 else
2632 chunk_appendf(&trash,
2633 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2634 uri->uri_prefix,
2635 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2636 ";norefresh",
2637 scope_txt);
2638 }
2639
2640 chunk_appendf(&trash,
2641 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2642 uri->uri_prefix,
2643 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2644 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2645 scope_txt);
2646
2647 chunk_appendf(&trash,
2648 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2649 uri->uri_prefix,
2650 (uri->refresh > 0) ? ";norefresh" : "",
2651 scope_txt);
2652
2653 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002654 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2655 uri->uri_prefix,
2656 (uri->refresh > 0) ? ";norefresh" : "",
2657 scope_txt, uri->uri_prefix);
2658
2659 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002660 "</ul></td>"
2661 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2662 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2663 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2664 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2665 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2666 "</ul>"
2667 "</td>"
2668 "</tr></table>\n"
2669 ""
2670 );
2671
2672 if (appctx->ctx.stats.st_code) {
2673 switch (appctx->ctx.stats.st_code) {
2674 case STAT_STATUS_DONE:
2675 chunk_appendf(&trash,
2676 "<p><div class=active_up>"
2677 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2678 "Action processed successfully."
2679 "</div>\n", uri->uri_prefix,
2680 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2681 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2682 scope_txt);
2683 break;
2684 case STAT_STATUS_NONE:
2685 chunk_appendf(&trash,
2686 "<p><div class=active_going_down>"
2687 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2688 "Nothing has changed."
2689 "</div>\n", uri->uri_prefix,
2690 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2691 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2692 scope_txt);
2693 break;
2694 case STAT_STATUS_PART:
2695 chunk_appendf(&trash,
2696 "<p><div class=active_going_down>"
2697 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2698 "Action partially processed.<br>"
2699 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2700 "</div>\n", uri->uri_prefix,
2701 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2702 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2703 scope_txt);
2704 break;
2705 case STAT_STATUS_ERRP:
2706 chunk_appendf(&trash,
2707 "<p><div class=active_down>"
2708 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2709 "Action not processed because of invalid parameters."
2710 "<ul>"
2711 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002712 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002713 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2714 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2715 "</ul>"
2716 "</div>\n", uri->uri_prefix,
2717 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2718 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2719 scope_txt);
2720 break;
2721 case STAT_STATUS_EXCD:
2722 chunk_appendf(&trash,
2723 "<p><div class=active_down>"
2724 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2725 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2726 "You should retry with less servers at a time.</b>"
2727 "</div>\n", uri->uri_prefix,
2728 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2729 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2730 scope_txt);
2731 break;
2732 case STAT_STATUS_DENY:
2733 chunk_appendf(&trash,
2734 "<p><div class=active_down>"
2735 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2736 "<b>Action denied.</b>"
2737 "</div>\n", uri->uri_prefix,
2738 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2739 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2740 scope_txt);
2741 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002742 case STAT_STATUS_IVAL:
2743 chunk_appendf(&trash,
2744 "<p><div class=active_down>"
2745 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2746 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2747 "</div>\n", uri->uri_prefix,
2748 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2749 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2750 scope_txt);
2751 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002752 default:
2753 chunk_appendf(&trash,
2754 "<p><div class=active_no_check>"
2755 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2756 "Unexpected result."
2757 "</div>\n", uri->uri_prefix,
2758 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2759 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2760 scope_txt);
2761 }
2762 chunk_appendf(&trash, "<p>\n");
2763 }
2764}
2765
2766/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2767 * for clearing the trash if needed.
2768 */
2769static void stats_dump_html_end()
2770{
2771 chunk_appendf(&trash, "</body></html>\n");
2772}
2773
Simon Horman05ee2132017-01-04 09:37:25 +01002774/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2775 * for clearing it if needed.
2776 */
2777static void stats_dump_json_header()
2778{
2779 chunk_strcat(&trash, "[");
2780}
2781
2782
2783/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2784 * for clearing the trash if needed.
2785 */
2786static void stats_dump_json_end()
2787{
2788 chunk_strcat(&trash, "]");
2789}
2790
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002791/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
2792 * a pointer to the current server/listener.
2793 */
2794static int stats_dump_proxies(struct stream_interface *si,
2795 struct htx *htx,
2796 struct uri_auth *uri)
2797{
2798 struct appctx *appctx = __objt_appctx(si->end);
2799 struct channel *rep = si_ic(si);
2800 struct proxy *px;
2801
2802 /* dump proxies */
2803 while (appctx->ctx.stats.obj1) {
2804 if (htx) {
2805 if (htx_almost_full(htx))
2806 goto full;
2807 }
2808 else {
2809 if (buffer_almost_full(&rep->buf))
2810 goto full;
2811 }
2812
2813 px = appctx->ctx.stats.obj1;
2814 /* skip the disabled proxies, global frontend and non-networked ones */
2815 if (px->state != PR_STSTOPPED && px->uuid > 0
2816 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
2817
2818 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
2819 return 0;
2820 }
2821
2822 appctx->ctx.stats.obj1 = px->next;
2823 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2824 }
2825
2826 return 1;
2827
2828 full:
2829 si_rx_room_blk(si);
2830 return 0;
2831}
2832
William Lallemand74c24fb2016-11-21 17:18:36 +01002833/* This function dumps statistics onto the stream interface's read buffer in
2834 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2835 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2836 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2837 * and the stream must be closed, or -1 in case of any error. This function is
2838 * used by both the CLI and the HTTP handlers.
2839 */
Christopher Fauletef779222018-10-31 08:47:01 +01002840static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2841 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002842{
2843 struct appctx *appctx = __objt_appctx(si->end);
2844 struct channel *rep = si_ic(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002845
2846 chunk_reset(&trash);
2847
2848 switch (appctx->st2) {
2849 case STAT_ST_INIT:
2850 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2851 /* fall through */
2852
2853 case STAT_ST_HEAD:
2854 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002855 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002856 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2857 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002858 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002859 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002860 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02002861 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01002862
Christopher Fauletef779222018-10-31 08:47:01 +01002863 if (!stats_putchk(rep, htx, &trash))
2864 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002865
Christopher Faulet6338a082019-09-09 15:50:54 +02002866 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2867 appctx->st2 = STAT_ST_FIN;
2868 return 1;
2869 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002870 appctx->st2 = STAT_ST_INFO;
2871 /* fall through */
2872
2873 case STAT_ST_INFO:
2874 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2875 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002876 if (!stats_putchk(rep, htx, &trash))
2877 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002878 }
2879
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002880 appctx->ctx.stats.obj1 = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002881 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2882 appctx->st2 = STAT_ST_LIST;
2883 /* fall through */
2884
2885 case STAT_ST_LIST:
2886 /* dump proxies */
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002887 if (!stats_dump_proxies(si, htx, uri))
2888 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002889
2890 appctx->st2 = STAT_ST_END;
2891 /* fall through */
2892
2893 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002894 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2895 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2896 stats_dump_html_end();
2897 else
2898 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002899 if (!stats_putchk(rep, htx, &trash))
2900 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002901 }
2902
2903 appctx->st2 = STAT_ST_FIN;
2904 /* fall through */
2905
2906 case STAT_ST_FIN:
2907 return 1;
2908
2909 default:
2910 /* unknown state ! */
2911 appctx->st2 = STAT_ST_FIN;
2912 return -1;
2913 }
Christopher Fauletef779222018-10-31 08:47:01 +01002914
2915 full:
2916 si_rx_room_blk(si);
2917 return 0;
2918
William Lallemand74c24fb2016-11-21 17:18:36 +01002919}
2920
2921/* We reached the stats page through a POST request. The appctx is
2922 * expected to have already been allocated by the caller.
2923 * Parse the posted data and enable/disable servers if necessary.
2924 * Returns 1 if request was parsed or zero if it needs more data.
2925 */
2926static int stats_process_http_post(struct stream_interface *si)
2927{
2928 struct stream *s = si_strm(si);
2929 struct appctx *appctx = objt_appctx(si->end);
2930
2931 struct proxy *px = NULL;
2932 struct server *sv = NULL;
2933
2934 char key[LINESIZE];
2935 int action = ST_ADM_ACTION_NONE;
2936 int reprocess = 0;
2937
2938 int total_servers = 0;
2939 int altered_servers = 0;
2940
2941 char *first_param, *cur_param, *next_param, *end_params;
2942 char *st_cur_param = NULL;
2943 char *st_next_param = NULL;
2944
Christopher Fauleta3618372018-12-13 21:59:56 +01002945 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002946
Christopher Fauletb7f88902019-07-15 21:56:43 +02002947 struct htx *htx = htxbuf(&s->req.buf);
2948 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002949
Christopher Fauletb7f88902019-07-15 21:56:43 +02002950 /* we need more data */
2951 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2952 /* check if we can receive more */
2953 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2954 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2955 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002956 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002957 goto wait;
2958 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002959
Christopher Fauletb7f88902019-07-15 21:56:43 +02002960 /* The request was fully received. Copy data */
2961 blk = htx_get_head_blk(htx);
2962 while (blk) {
2963 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002964
Christopher Fauletb7f88902019-07-15 21:56:43 +02002965 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2966 break;
2967 if (type == HTX_BLK_DATA) {
2968 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002969
Christopher Fauletb7f88902019-07-15 21:56:43 +02002970 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002971 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2972 goto out;
2973 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002974 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002975 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002976 }
2977
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002978 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002979 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002980 cur_param = next_param = end_params;
2981 *end_params = '\0';
2982
2983 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2984
2985 /*
2986 * Parse the parameters in reverse order to only store the last value.
2987 * From the html form, the backend and the action are at the end.
2988 */
2989 while (cur_param > first_param) {
2990 char *value;
2991 int poffset, plen;
2992
2993 cur_param--;
2994
2995 if ((*cur_param == '&') || (cur_param == first_param)) {
2996 reprocess_servers:
2997 /* Parse the key */
2998 poffset = (cur_param != first_param ? 1 : 0);
2999 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3000 if ((plen > 0) && (plen <= sizeof(key))) {
3001 strncpy(key, cur_param + poffset, plen);
3002 key[plen - 1] = '\0';
3003 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003004 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003005 goto out;
3006 }
3007
3008 /* Parse the value */
3009 value = key;
3010 while (*value != '\0' && *value != '=') {
3011 value++;
3012 }
3013 if (*value == '=') {
3014 /* Ok, a value is found, we can mark the end of the key */
3015 *value++ = '\0';
3016 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003017 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003018 break;
3019
3020 /* Now we can check the key to see what to do */
3021 if (!px && (strcmp(key, "b") == 0)) {
3022 if ((px = proxy_be_by_name(value)) == NULL) {
3023 /* the backend name is unknown or ambiguous (duplicate names) */
3024 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3025 goto out;
3026 }
3027 }
3028 else if (!action && (strcmp(key, "action") == 0)) {
3029 if (strcmp(value, "ready") == 0) {
3030 action = ST_ADM_ACTION_READY;
3031 }
3032 else if (strcmp(value, "drain") == 0) {
3033 action = ST_ADM_ACTION_DRAIN;
3034 }
3035 else if (strcmp(value, "maint") == 0) {
3036 action = ST_ADM_ACTION_MAINT;
3037 }
3038 else if (strcmp(value, "shutdown") == 0) {
3039 action = ST_ADM_ACTION_SHUTDOWN;
3040 }
3041 else if (strcmp(value, "dhlth") == 0) {
3042 action = ST_ADM_ACTION_DHLTH;
3043 }
3044 else if (strcmp(value, "ehlth") == 0) {
3045 action = ST_ADM_ACTION_EHLTH;
3046 }
3047 else if (strcmp(value, "hrunn") == 0) {
3048 action = ST_ADM_ACTION_HRUNN;
3049 }
3050 else if (strcmp(value, "hnolb") == 0) {
3051 action = ST_ADM_ACTION_HNOLB;
3052 }
3053 else if (strcmp(value, "hdown") == 0) {
3054 action = ST_ADM_ACTION_HDOWN;
3055 }
3056 else if (strcmp(value, "dagent") == 0) {
3057 action = ST_ADM_ACTION_DAGENT;
3058 }
3059 else if (strcmp(value, "eagent") == 0) {
3060 action = ST_ADM_ACTION_EAGENT;
3061 }
3062 else if (strcmp(value, "arunn") == 0) {
3063 action = ST_ADM_ACTION_ARUNN;
3064 }
3065 else if (strcmp(value, "adown") == 0) {
3066 action = ST_ADM_ACTION_ADOWN;
3067 }
3068 /* else these are the old supported methods */
3069 else if (strcmp(value, "disable") == 0) {
3070 action = ST_ADM_ACTION_DISABLE;
3071 }
3072 else if (strcmp(value, "enable") == 0) {
3073 action = ST_ADM_ACTION_ENABLE;
3074 }
3075 else if (strcmp(value, "stop") == 0) {
3076 action = ST_ADM_ACTION_STOP;
3077 }
3078 else if (strcmp(value, "start") == 0) {
3079 action = ST_ADM_ACTION_START;
3080 }
3081 else {
3082 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3083 goto out;
3084 }
3085 }
3086 else if (strcmp(key, "s") == 0) {
3087 if (!(px && action)) {
3088 /*
3089 * Indicates that we'll need to reprocess the parameters
3090 * as soon as backend and action are known
3091 */
3092 if (!reprocess) {
3093 st_cur_param = cur_param;
3094 st_next_param = next_param;
3095 }
3096 reprocess = 1;
3097 }
3098 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003099 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003100 switch (action) {
3101 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003102 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003103 altered_servers++;
3104 total_servers++;
3105 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3106 }
3107 break;
3108 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003109 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003110 altered_servers++;
3111 total_servers++;
3112 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3113 }
3114 break;
3115 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003116 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003117 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3118 altered_servers++;
3119 total_servers++;
3120 }
3121 break;
3122 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003123 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003124 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3125 altered_servers++;
3126 total_servers++;
3127 }
3128 break;
3129 case ST_ADM_ACTION_DHLTH:
3130 if (sv->check.state & CHK_ST_CONFIGURED) {
3131 sv->check.state &= ~CHK_ST_ENABLED;
3132 altered_servers++;
3133 total_servers++;
3134 }
3135 break;
3136 case ST_ADM_ACTION_EHLTH:
3137 if (sv->check.state & CHK_ST_CONFIGURED) {
3138 sv->check.state |= CHK_ST_ENABLED;
3139 altered_servers++;
3140 total_servers++;
3141 }
3142 break;
3143 case ST_ADM_ACTION_HRUNN:
3144 if (!(sv->track)) {
3145 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003146 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003147 altered_servers++;
3148 total_servers++;
3149 }
3150 break;
3151 case ST_ADM_ACTION_HNOLB:
3152 if (!(sv->track)) {
3153 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003154 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003155 altered_servers++;
3156 total_servers++;
3157 }
3158 break;
3159 case ST_ADM_ACTION_HDOWN:
3160 if (!(sv->track)) {
3161 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003162 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003163 altered_servers++;
3164 total_servers++;
3165 }
3166 break;
3167 case ST_ADM_ACTION_DAGENT:
3168 if (sv->agent.state & CHK_ST_CONFIGURED) {
3169 sv->agent.state &= ~CHK_ST_ENABLED;
3170 altered_servers++;
3171 total_servers++;
3172 }
3173 break;
3174 case ST_ADM_ACTION_EAGENT:
3175 if (sv->agent.state & CHK_ST_CONFIGURED) {
3176 sv->agent.state |= CHK_ST_ENABLED;
3177 altered_servers++;
3178 total_servers++;
3179 }
3180 break;
3181 case ST_ADM_ACTION_ARUNN:
3182 if (sv->agent.state & CHK_ST_ENABLED) {
3183 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003184 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003185 altered_servers++;
3186 total_servers++;
3187 }
3188 break;
3189 case ST_ADM_ACTION_ADOWN:
3190 if (sv->agent.state & CHK_ST_ENABLED) {
3191 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003192 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003193 altered_servers++;
3194 total_servers++;
3195 }
3196 break;
3197 case ST_ADM_ACTION_READY:
3198 srv_adm_set_ready(sv);
3199 altered_servers++;
3200 total_servers++;
3201 break;
3202 case ST_ADM_ACTION_DRAIN:
3203 srv_adm_set_drain(sv);
3204 altered_servers++;
3205 total_servers++;
3206 break;
3207 case ST_ADM_ACTION_MAINT:
3208 srv_adm_set_maint(sv);
3209 altered_servers++;
3210 total_servers++;
3211 break;
3212 case ST_ADM_ACTION_SHUTDOWN:
3213 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003214 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003215 altered_servers++;
3216 total_servers++;
3217 }
3218 break;
3219 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003220 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003221 } else {
3222 /* the server name is unknown or ambiguous (duplicate names) */
3223 total_servers++;
3224 }
3225 }
3226 if (reprocess && px && action) {
3227 /* Now, we know the backend and the action chosen by the user.
3228 * We can safely restart from the first server parameter
3229 * to reprocess them
3230 */
3231 cur_param = st_cur_param;
3232 next_param = st_next_param;
3233 reprocess = 0;
3234 goto reprocess_servers;
3235 }
3236
3237 next_param = cur_param;
3238 }
3239 }
3240
3241 if (total_servers == 0) {
3242 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3243 }
3244 else if (altered_servers == 0) {
3245 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3246 }
3247 else if (altered_servers == total_servers) {
3248 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3249 }
3250 else {
3251 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3252 }
3253 out:
3254 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003255 wait:
3256 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3257 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003258}
3259
3260
Christopher Fauletb7f88902019-07-15 21:56:43 +02003261static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003262{
3263 struct stream *s = si_strm(si);
3264 struct uri_auth *uri = s->be->uri_auth;
3265 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003266 struct htx_sl *sl;
3267 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003268
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003269 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);
3270 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3271 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003272 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003273 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003274
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003275 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003276 goto full;
3277 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3278 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3279 goto full;
3280 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003281 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3282 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3283 goto full;
3284 }
Christopher Fauletef779222018-10-31 08:47:01 +01003285 else {
3286 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3287 goto full;
3288 }
3289
3290 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3291 const char *refresh = U2A(uri->refresh);
3292 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3293 goto full;
3294 }
3295
3296 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3297 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3298 goto full;
3299 }
3300
3301 if (!htx_add_endof(htx, HTX_BLK_EOH))
3302 goto full;
3303
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003304 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003305 return 1;
3306
3307 full:
3308 htx_reset(htx);
3309 si_rx_room_blk(si);
3310 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003311}
3312
Christopher Fauletef779222018-10-31 08:47:01 +01003313
Christopher Fauletb7f88902019-07-15 21:56:43 +02003314static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003315{
3316 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3317 struct stream *s = si_strm(si);
3318 struct uri_auth *uri = s->be->uri_auth;
3319 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003320 struct htx_sl *sl;
3321 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003322
3323 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3324 scope_txt[0] = 0;
3325 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003326 const char *scope_ptr = stats_scope_ptr(appctx, si);
3327
Christopher Fauletef779222018-10-31 08:47:01 +01003328 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003329 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003330 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3331 }
3332
3333 /* We don't want to land on the posted stats page because a refresh will
3334 * repost the data. We don't want this to happen on accident so we redirect
3335 * the browse to the stats page with a GET.
3336 */
3337 chunk_printf(&trash, "%s;st=%s%s%s%s",
3338 uri->uri_prefix,
3339 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3340 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3341 stat_status_codes[appctx->ctx.stats.st_code]) ?
3342 stat_status_codes[appctx->ctx.stats.st_code] :
3343 stat_status_codes[STAT_STATUS_UNKN],
3344 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3345 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3346 scope_txt);
3347
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003348 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3349 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3350 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003351 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003352 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003353
3354 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003355 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3356 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3357 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3358 goto full;
3359
3360 if (!htx_add_endof(htx, HTX_BLK_EOH))
3361 goto full;
3362
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003363 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003364 return 1;
3365
3366full:
3367 htx_reset(htx);
3368 si_rx_room_blk(si);
3369 return 0;
3370}
William Lallemand74c24fb2016-11-21 17:18:36 +01003371
Simon Horman05ee2132017-01-04 09:37:25 +01003372
William Lallemand74c24fb2016-11-21 17:18:36 +01003373/* This I/O handler runs as an applet embedded in a stream interface. It is
3374 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3375 * appctx->st0 contains the operation in progress (dump, done). The handler
3376 * automatically unregisters itself once transfer is complete.
3377 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003378static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003379{
3380 struct stream_interface *si = appctx->owner;
3381 struct stream *s = si_strm(si);
3382 struct channel *req = si_oc(si);
3383 struct channel *res = si_ic(si);
3384 struct htx *req_htx, *res_htx;
3385
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003386 /* only proxy stats are available via http */
3387 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3388
Christopher Fauletef779222018-10-31 08:47:01 +01003389 res_htx = htx_from_buf(&res->buf);
3390
3391 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3392 goto out;
3393
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003394 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003395 if (!b_size(&res->buf)) {
3396 si_rx_room_blk(si);
3397 goto out;
3398 }
3399
3400 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003401 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3402 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003403
3404 /* all states are processed in sequence */
3405 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003406 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003407 if (s->txn->meth == HTTP_METH_HEAD)
3408 appctx->st0 = STAT_HTTP_DONE;
3409 else
3410 appctx->st0 = STAT_HTTP_DUMP;
3411 }
3412 }
3413
3414 if (appctx->st0 == STAT_HTTP_DUMP) {
3415 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3416 appctx->st0 = STAT_HTTP_DONE;
3417 }
3418
3419 if (appctx->st0 == STAT_HTTP_POST) {
3420 if (stats_process_http_post(si))
3421 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003422 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003423 appctx->st0 = STAT_HTTP_DONE;
3424 }
3425
3426 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003427 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003428 appctx->st0 = STAT_HTTP_DONE;
3429 }
3430
3431 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003432 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003433 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003434 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3435 si_rx_room_blk(si);
3436 goto out;
3437 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003438 channel_add_input(&s->res, 1);
3439 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003440 }
3441
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003442 if (appctx->st0 == STAT_HTTP_END) {
3443 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003444 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003445 si_shutr(si);
3446 }
3447
3448 /* eat the whole request */
3449 if (co_data(req)) {
3450 req_htx = htx_from_buf(&req->buf);
3451 co_htx_skip(req, req_htx, co_data(req));
3452 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003453 }
3454 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003455
Christopher Fauletef779222018-10-31 08:47:01 +01003456 out:
3457 /* we have left the request in the buffer for the case where we
3458 * process a POST, and this automatically re-enables activity on
3459 * read. It's better to indicate that we want to stop reading when
3460 * we're sending, so that we know there's at most one direction
3461 * deciding to wake the applet up. It saves it from looping when
3462 * emitting large blocks into small TCP windows.
3463 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003464 htx_to_buf(res_htx, &res->buf);
3465 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003466 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003467}
3468
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003469/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003470static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003471 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003472{
3473 int field;
3474
3475 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3476 if (!field_format(info, field))
3477 continue;
3478
Willy Tarreaueaa55372019-10-09 07:39:11 +02003479 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003480 return 0;
3481 if (!stats_emit_raw_data_field(out, &info[field]))
3482 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003483 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3484 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003485 if (!chunk_strcat(out, "\n"))
3486 return 0;
3487 }
3488 return 1;
3489}
3490
3491/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003492static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003493 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003494{
3495 int field;
3496
3497 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3498 if (!field_format(info, field))
3499 continue;
3500
Willy Tarreaueaa55372019-10-09 07:39:11 +02003501 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 +01003502 return 0;
3503 if (!stats_emit_field_tags(out, &info[field], ':'))
3504 return 0;
3505 if (!stats_emit_typed_data_field(out, &info[field]))
3506 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003507 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3508 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003509 if (!chunk_strcat(out, "\n"))
3510 return 0;
3511 }
3512 return 1;
3513}
3514
3515/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003516 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003517 * INF_TOTAL_FIELDS. If this length is less then this value, the
3518 * function returns 0, otherwise, it returns 1.
3519 */
3520int stats_fill_info(struct field *info, int len)
3521{
3522 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003523 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003524
3525#ifdef USE_OPENSSL
3526 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3527 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3528 int ssl_reuse = 0;
3529
3530 if (ssl_key_rate < ssl_sess_rate) {
3531 /* count the ssl reuse ratio and avoid overflows in both directions */
3532 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3533 }
3534#endif
3535
3536 if (len < INF_TOTAL_FIELDS)
3537 return 0;
3538
3539 chunk_reset(out);
3540 memset(info, 0, sizeof(*info) * len);
3541
3542 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003543 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3544 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003545
Yves Lafon95317282018-02-26 11:10:37 +01003546 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003547 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3548 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3549 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3550
3551 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3552 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3553
3554 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3555 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3556 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3557 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3558 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3559 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3560 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3561 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3562 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3563 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3564 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3565 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3566#ifdef USE_OPENSSL
3567 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3568 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3569 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3570#endif
3571 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3572 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3573 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3574 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3575 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3576 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3577 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3578 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3579 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3580
3581#ifdef USE_OPENSSL
3582 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3583 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3584 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3585 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3586 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3587 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3588 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3589 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3590 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3591 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3592#endif
3593 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3594 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3595 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3596#ifdef USE_ZLIB
3597 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3598 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3599#endif
3600 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003601 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003602 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003603 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3604 if (global.desc)
3605 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003606 info[INF_STOPPING] = mkf_u32(0, stopping);
3607 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003608 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003609 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003610 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003611 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003612 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003613 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003614 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003615 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003616 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003617 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 +02003618 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003619 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003620
3621 return 1;
3622}
3623
3624/* This function dumps information onto the stream interface's read buffer.
3625 * It returns 0 as long as it does not complete, non-zero upon completion.
3626 * No state is used.
3627 */
3628static int stats_dump_info_to_buffer(struct stream_interface *si)
3629{
3630 struct appctx *appctx = __objt_appctx(si->end);
3631
3632 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3633 return 0;
3634
3635 chunk_reset(&trash);
3636
3637 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003638 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003639 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003640 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003641 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003642 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003643
Willy Tarreau06d80a92017-10-19 14:32:15 +02003644 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003645 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003646 return 0;
3647 }
3648
3649 return 1;
3650}
3651
Simon Horman6f6bb382017-01-04 09:37:26 +01003652/* This function dumps the schema onto the stream interface's read buffer.
3653 * It returns 0 as long as it does not complete, non-zero upon completion.
3654 * No state is used.
3655 *
3656 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3657 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3658 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003659static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003660{
3661
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003662 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003663
3664 chunk_strcat(out,
3665 "{"
3666 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3667 "\"oneOf\":["
3668 "{"
3669 "\"title\":\"Info\","
3670 "\"type\":\"array\","
3671 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003672 "\"title\":\"InfoItem\","
3673 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003674 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003675 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3676 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3677 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3678 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3679 "},"
3680 "\"required\":[\"field\",\"processNum\",\"tags\","
3681 "\"value\"]"
3682 "}"
3683 "},"
3684 "{"
3685 "\"title\":\"Stat\","
3686 "\"type\":\"array\","
3687 "\"items\":{"
3688 "\"title\":\"InfoItem\","
3689 "\"type\":\"object\","
3690 "\"properties\":{"
3691 "\"objType\":{"
3692 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3693 "\"Server\",\"Unknown\"]"
3694 "},"
3695 "\"proxyId\":{"
3696 "\"type\":\"integer\","
3697 "\"minimum\":0"
3698 "},"
3699 "\"id\":{"
3700 "\"type\":\"integer\","
3701 "\"minimum\":0"
3702 "},"
3703 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3704 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3705 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3706 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3707 "},"
3708 "\"required\":[\"objType\",\"proxyId\",\"id\","
3709 "\"field\",\"processNum\",\"tags\","
3710 "\"value\"]"
3711 "}"
3712 "},"
3713 "{"
3714 "\"title\":\"Error\","
3715 "\"type\":\"object\","
3716 "\"properties\":{"
3717 "\"errorStr\":{"
3718 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003719 "}"
3720 "},"
3721 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01003722 "}"
3723 "],"
3724 "\"definitions\":{"
3725 "\"field\":{"
3726 "\"type\":\"object\","
3727 "\"pos\":{"
3728 "\"type\":\"integer\","
3729 "\"minimum\":0"
3730 "},"
3731 "\"name\":{"
3732 "\"type\":\"string\""
3733 "},"
3734 "\"required\":[\"pos\",\"name\"]"
3735 "},"
3736 "\"processNum\":{"
3737 "\"type\":\"integer\","
3738 "\"minimum\":1"
3739 "},"
3740 "\"tags\":{"
3741 "\"type\":\"object\","
3742 "\"origin\":{"
3743 "\"type\":\"string\","
3744 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3745 "\"Config\",\"Product\",\"Unknown\"]"
3746 "},"
3747 "\"nature\":{"
3748 "\"type\":\"string\","
3749 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3750 "\"Rate\",\"Counter\",\"Duration\","
3751 "\"Age\",\"Time\",\"Name\",\"Output\","
3752 "\"Avg\", \"Unknown\"]"
3753 "},"
3754 "\"scope\":{"
3755 "\"type\":\"string\","
3756 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3757 "\"System\",\"Unknown\"]"
3758 "},"
3759 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3760 "},"
3761 "\"typedValue\":{"
3762 "\"type\":\"object\","
3763 "\"oneOf\":["
3764 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3765 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3766 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3767 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3768 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3769 "],"
3770 "\"definitions\":{"
3771 "\"s32Value\":{"
3772 "\"properties\":{"
3773 "\"type\":{"
3774 "\"type\":\"string\","
3775 "\"enum\":[\"s32\"]"
3776 "},"
3777 "\"value\":{"
3778 "\"type\":\"integer\","
3779 "\"minimum\":-2147483648,"
3780 "\"maximum\":2147483647"
3781 "}"
3782 "},"
3783 "\"required\":[\"type\",\"value\"]"
3784 "},"
3785 "\"s64Value\":{"
3786 "\"properties\":{"
3787 "\"type\":{"
3788 "\"type\":\"string\","
3789 "\"enum\":[\"s64\"]"
3790 "},"
3791 "\"value\":{"
3792 "\"type\":\"integer\","
3793 "\"minimum\":-9007199254740991,"
3794 "\"maximum\":9007199254740991"
3795 "}"
3796 "},"
3797 "\"required\":[\"type\",\"value\"]"
3798 "},"
3799 "\"u32Value\":{"
3800 "\"properties\":{"
3801 "\"type\":{"
3802 "\"type\":\"string\","
3803 "\"enum\":[\"u32\"]"
3804 "},"
3805 "\"value\":{"
3806 "\"type\":\"integer\","
3807 "\"minimum\":0,"
3808 "\"maximum\":4294967295"
3809 "}"
3810 "},"
3811 "\"required\":[\"type\",\"value\"]"
3812 "},"
3813 "\"u64Value\":{"
3814 "\"properties\":{"
3815 "\"type\":{"
3816 "\"type\":\"string\","
3817 "\"enum\":[\"u64\"]"
3818 "},"
3819 "\"value\":{"
3820 "\"type\":\"integer\","
3821 "\"minimum\":0,"
3822 "\"maximum\":9007199254740991"
3823 "}"
3824 "},"
3825 "\"required\":[\"type\",\"value\"]"
3826 "},"
3827 "\"strValue\":{"
3828 "\"properties\":{"
3829 "\"type\":{"
3830 "\"type\":\"string\","
3831 "\"enum\":[\"str\"]"
3832 "},"
3833 "\"value\":{\"type\":\"string\"}"
3834 "},"
3835 "\"required\":[\"type\",\"value\"]"
3836 "},"
3837 "\"unknownValue\":{"
3838 "\"properties\":{"
3839 "\"type\":{"
3840 "\"type\":\"integer\","
3841 "\"minimum\":0"
3842 "},"
3843 "\"value\":{"
3844 "\"type\":\"string\","
3845 "\"enum\":[\"unknown\"]"
3846 "}"
3847 "},"
3848 "\"required\":[\"type\",\"value\"]"
3849 "}"
3850 "}"
3851 "}"
3852 "}"
3853 "}");
3854
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003855 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003856 chunk_reset(out);
3857 chunk_appendf(out,
3858 "{\"errorStr\":\"output buffer too short\"}");
3859 }
3860}
3861
3862/* This function dumps the schema onto the stream interface's read buffer.
3863 * It returns 0 as long as it does not complete, non-zero upon completion.
3864 * No state is used.
3865 */
3866static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3867{
3868 chunk_reset(&trash);
3869
3870 stats_dump_json_schema(&trash);
3871
Willy Tarreau06d80a92017-10-19 14:32:15 +02003872 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003873 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003874 return 0;
3875 }
3876
3877 return 1;
3878}
3879
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003880static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003881{
3882 struct proxy *px;
3883 struct server *sv;
3884 struct listener *li;
3885 int clrall = 0;
3886
3887 if (strcmp(args[2], "all") == 0)
3888 clrall = 1;
3889
3890 /* check permissions */
3891 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3892 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3893 return 1;
3894
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003895 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003896 if (clrall) {
3897 memset(&px->be_counters, 0, sizeof(px->be_counters));
3898 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3899 }
3900 else {
3901 px->be_counters.conn_max = 0;
3902 px->be_counters.p.http.rps_max = 0;
3903 px->be_counters.sps_max = 0;
3904 px->be_counters.cps_max = 0;
3905 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003906 px->be_counters.qtime_max = 0;
3907 px->be_counters.ctime_max = 0;
3908 px->be_counters.dtime_max = 0;
3909 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003910
3911 px->fe_counters.conn_max = 0;
3912 px->fe_counters.p.http.rps_max = 0;
3913 px->fe_counters.sps_max = 0;
3914 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003915 }
3916
3917 for (sv = px->srv; sv; sv = sv->next)
3918 if (clrall)
3919 memset(&sv->counters, 0, sizeof(sv->counters));
3920 else {
3921 sv->counters.cur_sess_max = 0;
3922 sv->counters.nbpend_max = 0;
3923 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003924 sv->counters.qtime_max = 0;
3925 sv->counters.ctime_max = 0;
3926 sv->counters.dtime_max = 0;
3927 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003928 }
3929
3930 list_for_each_entry(li, &px->conf.listeners, by_fe)
3931 if (li->counters) {
3932 if (clrall)
3933 memset(li->counters, 0, sizeof(*li->counters));
3934 else
3935 li->counters->conn_max = 0;
3936 }
3937 }
3938
3939 global.cps_max = 0;
3940 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003941 global.ssl_max = 0;
3942 global.ssl_fe_keys_max = 0;
3943 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003944
3945 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003946 return 1;
3947}
3948
3949
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003950static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003951{
Willy Tarreau2f397382019-10-09 11:43:59 +02003952 int arg = 2;
3953
Willy Tarreaud25fc792016-12-16 12:33:47 +01003954 appctx->ctx.stats.scope_str = 0;
3955 appctx->ctx.stats.scope_len = 0;
3956 appctx->ctx.stats.flags = 0;
3957
Willy Tarreau2f397382019-10-09 11:43:59 +02003958 while (*args[arg]) {
3959 if (strcmp(args[arg], "typed") == 0)
3960 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3961 else if (strcmp(args[arg], "json") == 0)
3962 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3963 else if (strcmp(args[arg], "desc") == 0)
3964 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3965 arg++;
3966 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003967 return 0;
3968}
3969
3970
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003971static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003972{
Willy Tarreau2f397382019-10-09 11:43:59 +02003973 int arg = 2;
3974
Willy Tarreaud25fc792016-12-16 12:33:47 +01003975 appctx->ctx.stats.scope_str = 0;
3976 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003977 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3978
3979 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3980 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003981
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003982 /* proxy is the default domain */
3983 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3984 if (!strcmp(args[arg], "domain")) {
3985 ++args;
3986
3987 if (!strcmp(args[arg], "proxy"))
3988 ++args;
3989 else
3990 return cli_err(appctx, "Invalid statistics domain.\n");
3991 }
3992
3993 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
3994 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003995 struct proxy *px;
3996
Willy Tarreau2f397382019-10-09 11:43:59 +02003997 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003998 if (px)
3999 appctx->ctx.stats.iid = px->uuid;
4000 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004001 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004002
Willy Tarreau9d008692019-08-09 11:21:01 +02004003 if (!appctx->ctx.stats.iid)
4004 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004005
Willy Tarreau2b812e22016-11-22 16:18:05 +01004006 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004007 appctx->ctx.stats.type = atoi(args[arg+1]);
4008 appctx->ctx.stats.sid = atoi(args[arg+2]);
4009 arg += 3;
4010 }
4011
4012 while (*args[arg]) {
4013 if (strcmp(args[arg], "typed") == 0)
4014 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4015 else if (strcmp(args[arg], "json") == 0)
4016 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4017 else if (strcmp(args[arg], "desc") == 0)
4018 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4019 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004020 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004021
Willy Tarreau2b812e22016-11-22 16:18:05 +01004022 return 0;
4023}
4024
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004025static int cli_io_handler_dump_info(struct appctx *appctx)
4026{
4027 return stats_dump_info_to_buffer(appctx->owner);
4028}
4029
Willy Tarreau2b812e22016-11-22 16:18:05 +01004030/* This I/O handler runs as an applet embedded in a stream interface. It is
4031 * used to send raw stats over a socket.
4032 */
4033static int cli_io_handler_dump_stat(struct appctx *appctx)
4034{
Christopher Fauletef779222018-10-31 08:47:01 +01004035 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004036}
4037
Simon Horman6f6bb382017-01-04 09:37:26 +01004038static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4039{
4040 return stats_dump_json_schema_to_buffer(appctx->owner);
4041}
4042
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004043void stats_register_module(struct stats_module *m)
4044{
4045 const uint8_t domain = stats_get_domain(m->domain_flags);
4046
4047 LIST_ADDQ(&stats_module_list[domain], &m->list);
4048}
4049
Willy Tarreau2b812e22016-11-22 16:18:05 +01004050/* register cli keywords */
4051static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004052 { { "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 +02004053 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
4054 { { "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 +01004055 { { "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 +01004056 {{},}
4057}};
4058
Willy Tarreau0108d902018-11-25 19:14:37 +01004059INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4060
William Lallemand74c24fb2016-11-21 17:18:36 +01004061struct applet http_stats_applet = {
4062 .obj_type = OBJ_TYPE_APPLET,
4063 .name = "<STATS>", /* used for logging */
4064 .fct = http_stats_io_handler,
4065 .release = NULL,
4066};
4067
William Lallemand74c24fb2016-11-21 17:18:36 +01004068/*
4069 * Local variables:
4070 * c-indent-level: 8
4071 * c-basic-offset: 8
4072 * End:
4073 */