blob: 562cd82733a50f4db734f6fc5477aa4fb4f14917 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020028#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020035#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020036#include <haproxy/debug.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020037#include <haproxy/dns.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020044#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020045#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020046#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020047#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020048#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020049#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020050#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020051#include <haproxy/pipe.h>
52#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020053#include <haproxy/proxy.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020054#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020055#include <haproxy/session.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020056#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020057#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020058#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020059#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020060#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020061#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020062#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020063#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020064#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020065#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066
William Lallemand74c24fb2016-11-21 17:18:36 +010067
Christopher Faulet2b9b6782019-02-27 16:42:58 +010068/* status codes available for the stats admin page (strictly 4 chars length) */
69const char *stat_status_codes[STAT_STATUS_SIZE] = {
70 [STAT_STATUS_DENY] = "DENY",
71 [STAT_STATUS_DONE] = "DONE",
72 [STAT_STATUS_ERRP] = "ERRP",
73 [STAT_STATUS_EXCD] = "EXCD",
74 [STAT_STATUS_NONE] = "NONE",
75 [STAT_STATUS_PART] = "PART",
76 [STAT_STATUS_UNKN] = "UNKN",
77 [STAT_STATUS_IVAL] = "IVAL",
78};
79
Willy Tarreau0baac8c2016-11-22 16:36:53 +010080/* These are the field names for each INF_* field position. Please pay attention
81 * to always use the exact same name except that the strings for new names must
82 * be lower case or CamelCase while the enum entries must be upper case.
83 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020084const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020085 [INF_NAME] = { .name = "Name", .desc = "Product name" },
86 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
87 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
88 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
89 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
90 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
91 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
92 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
93 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
94 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
95 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
96 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
97 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
98 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
99 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
100 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
101 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
102 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
103 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
104 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
105 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
106 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
107 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
108 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
109 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
110 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
111 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
112 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
113 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
114 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
115 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
116 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
117 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
118 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
119 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
120 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
121 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
122 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
123 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
124 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
125 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
126 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
127 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
128 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
129 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
130 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
131 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
132 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
133 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
134 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
135 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
136 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
137 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
138 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
139 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
140 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
141 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
142 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
143 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
144 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
145 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
146 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Christopher Fauletaaa70852020-07-10 13:56:30 +0200147 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicdedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200148 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200149 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200150 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100151};
152
Willy Tarreaueaa55372019-10-09 07:39:11 +0200153const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
155 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
156 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
157 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
158 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
159 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
160 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
161 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
162 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
163 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
164 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
165 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
166 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
167 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
168 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
169 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
170 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
171 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
172 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
173 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
174 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
175 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
176 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
177 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
178 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
179 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
180 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
181 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
182 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
183 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
184 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
185 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
186 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
187 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
188 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
189 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
190 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
191 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
192 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
193 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
194 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
195 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
196 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
197 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
198 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
199 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
200 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
201 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
202 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
203 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
204 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
205 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
206 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
207 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
208 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
209 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
210 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
211 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
212 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
213 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
214 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
215 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
216 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
217 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
218 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
219 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
220 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
221 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
222 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
223 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
224 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
225 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
226 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
227 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
228 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
229 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
230 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
231 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
232 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
233 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
234 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
235 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
236 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
237 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
238 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
239 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
240 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
241 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
242 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
243 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100244 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
245 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
246 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
247 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100248 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200249 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
250 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
251 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200252 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100253};
254
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100255/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100256static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100257/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100258static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100259
Christopher Faulet6338a082019-09-09 15:50:54 +0200260static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100261
Christopher Fauletef779222018-10-31 08:47:01 +0100262static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
263{
264 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100265 if (chk->data >= channel_htx_recv_max(chn, htx))
266 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200267 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100268 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100269 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100270 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100271 }
272 else {
273 if (ci_putchk(chn, chk) == -1)
274 return 0;
275 }
276 return 1;
277}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100278
Christopher Fauleted7a0662019-01-14 11:07:34 +0100279static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
280{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200281 struct channel *req = si_oc(si);
282 struct htx *htx = htxbuf(&req->buf);
283 struct htx_blk *blk;
284 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100285
Christopher Fauletb7f88902019-07-15 21:56:43 +0200286 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100287 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200288 ALREADY_CHECKED(blk);
289 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
290 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100291}
292
William Lallemand74c24fb2016-11-21 17:18:36 +0100293/*
294 * http_stats_io_handler()
295 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
296 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100297 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100298 * -> stats_dump_html_head() // emits the HTML headers
299 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
300 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
301 * -> stats_dump_html_px_hdr()
302 * -> stats_dump_fe_stats()
303 * -> stats_dump_li_stats()
304 * -> stats_dump_sv_stats()
305 * -> stats_dump_be_stats()
306 * -> stats_dump_html_px_end()
307 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100308 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100309 */
310
311
William Lallemand74c24fb2016-11-21 17:18:36 +0100312/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
313 * for clearing it if needed.
314 * NOTE: Some tools happen to rely on the field position instead of its name,
315 * so please only append new fields at the end, never in the middle.
316 */
317static void stats_dump_csv_header()
318{
319 int field;
320
321 chunk_appendf(&trash, "# ");
322 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200323 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100324
325 chunk_appendf(&trash, "\n");
326}
327
328
329/* Emits a stats field without any surrounding element and properly encoded to
330 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
331 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200332int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100333{
334 switch (field_format(f, 0)) {
335 case FF_EMPTY: return 1;
336 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
337 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
338 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
339 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200340 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100341 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
342 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
343 }
344}
345
346/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
347 * output is supposed to be used on its own line. Returns non-zero on success, 0
348 * if the buffer is full.
349 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200350int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100351{
352 switch (field_format(f, 0)) {
353 case FF_EMPTY: return 1;
354 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
355 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
356 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
357 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200358 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100359 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
360 default: return chunk_appendf(out, "%08x:?", f->type);
361 }
362}
363
Simon Horman05ee2132017-01-04 09:37:25 +0100364/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
365 * the recommendation for interoperable integers in section 6 of RFC 7159.
366 */
367#define JSON_INT_MAX ((1ULL << 53) - 1)
368#define JSON_INT_MIN (0 - JSON_INT_MAX)
369
370/* Emits a stats field value and its type in JSON.
371 * Returns non-zero on success, 0 on error.
372 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200373int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100374{
375 int old_len;
376 char buf[20];
377 const char *type, *value = buf, *quote = "";
378
379 switch (field_format(f, 0)) {
380 case FF_EMPTY: return 1;
381 case FF_S32: type = "\"s32\"";
382 snprintf(buf, sizeof(buf), "%d", f->u.s32);
383 break;
384 case FF_U32: type = "\"u32\"";
385 snprintf(buf, sizeof(buf), "%u", f->u.u32);
386 break;
387 case FF_S64: type = "\"s64\"";
388 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
389 return 0;
390 type = "\"s64\"";
391 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
392 break;
393 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
394 return 0;
395 type = "\"u64\"";
396 snprintf(buf, sizeof(buf), "%llu",
397 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200398 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200399 case FF_FLT: type = "\"flt\"";
400 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100401 break;
402 case FF_STR: type = "\"str\"";
403 value = field_str(f, 0);
404 quote = "\"";
405 break;
406 default: snprintf(buf, sizeof(buf), "%u", f->type);
407 type = buf;
408 value = "unknown";
409 quote = "\"";
410 break;
411 }
412
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200413 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100414 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
415 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200416 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100417}
418
William Lallemand74c24fb2016-11-21 17:18:36 +0100419/* Emits an encoding of the field type on 3 characters followed by a delimiter.
420 * Returns non-zero on success, 0 if the buffer is full.
421 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200422int stats_emit_field_tags(struct buffer *out, const struct field *f,
423 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100424{
425 char origin, nature, scope;
426
427 switch (field_origin(f, 0)) {
428 case FO_METRIC: origin = 'M'; break;
429 case FO_STATUS: origin = 'S'; break;
430 case FO_KEY: origin = 'K'; break;
431 case FO_CONFIG: origin = 'C'; break;
432 case FO_PRODUCT: origin = 'P'; break;
433 default: origin = '?'; break;
434 }
435
436 switch (field_nature(f, 0)) {
437 case FN_GAUGE: nature = 'G'; break;
438 case FN_LIMIT: nature = 'L'; break;
439 case FN_MIN: nature = 'm'; break;
440 case FN_MAX: nature = 'M'; break;
441 case FN_RATE: nature = 'R'; break;
442 case FN_COUNTER: nature = 'C'; break;
443 case FN_DURATION: nature = 'D'; break;
444 case FN_AGE: nature = 'A'; break;
445 case FN_TIME: nature = 'T'; break;
446 case FN_NAME: nature = 'N'; break;
447 case FN_OUTPUT: nature = 'O'; break;
448 case FN_AVG: nature = 'a'; break;
449 default: nature = '?'; break;
450 }
451
452 switch (field_scope(f, 0)) {
453 case FS_PROCESS: scope = 'P'; break;
454 case FS_SERVICE: scope = 'S'; break;
455 case FS_SYSTEM: scope = 's'; break;
456 case FS_CLUSTER: scope = 'C'; break;
457 default: scope = '?'; break;
458 }
459
460 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
461}
462
Simon Horman05ee2132017-01-04 09:37:25 +0100463/* Emits an encoding of the field type as JSON.
464 * Returns non-zero on success, 0 if the buffer is full.
465 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200466int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100467{
468 const char *origin, *nature, *scope;
469 int old_len;
470
471 switch (field_origin(f, 0)) {
472 case FO_METRIC: origin = "Metric"; break;
473 case FO_STATUS: origin = "Status"; break;
474 case FO_KEY: origin = "Key"; break;
475 case FO_CONFIG: origin = "Config"; break;
476 case FO_PRODUCT: origin = "Product"; break;
477 default: origin = "Unknown"; break;
478 }
479
480 switch (field_nature(f, 0)) {
481 case FN_GAUGE: nature = "Gauge"; break;
482 case FN_LIMIT: nature = "Limit"; break;
483 case FN_MIN: nature = "Min"; break;
484 case FN_MAX: nature = "Max"; break;
485 case FN_RATE: nature = "Rate"; break;
486 case FN_COUNTER: nature = "Counter"; break;
487 case FN_DURATION: nature = "Duration"; break;
488 case FN_AGE: nature = "Age"; break;
489 case FN_TIME: nature = "Time"; break;
490 case FN_NAME: nature = "Name"; break;
491 case FN_OUTPUT: nature = "Output"; break;
492 case FN_AVG: nature = "Avg"; break;
493 default: nature = "Unknown"; break;
494 }
495
496 switch (field_scope(f, 0)) {
497 case FS_PROCESS: scope = "Process"; break;
498 case FS_SERVICE: scope = "Service"; break;
499 case FS_SYSTEM: scope = "System"; break;
500 case FS_CLUSTER: scope = "Cluster"; break;
501 default: scope = "Unknown"; break;
502 }
503
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200504 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100505 chunk_appendf(out, "\"tags\":{"
506 "\"origin\":\"%s\","
507 "\"nature\":\"%s\","
508 "\"scope\":\"%s\""
509 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200510 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100511}
William Lallemand74c24fb2016-11-21 17:18:36 +0100512
513/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200514static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200515 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100516{
517 int field;
518
519 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
520 if (!stats_emit_raw_data_field(out, &stats[field]))
521 return 0;
522 if (!chunk_strcat(out, ","))
523 return 0;
524 }
525 chunk_strcat(out, "\n");
526 return 1;
527}
528
529/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200530static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200531 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100532{
533 int field;
534
535 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
536 if (!stats[field].type)
537 continue;
538
539 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
540 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
541 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
542 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
543 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
544 '?',
545 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200546 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100547
548 if (!stats_emit_field_tags(out, &stats[field], ':'))
549 return 0;
550 if (!stats_emit_typed_data_field(out, &stats[field]))
551 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200552 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
553 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100554 if (!chunk_strcat(out, "\n"))
555 return 0;
556 }
557 return 1;
558}
559
Simon Horman05ee2132017-01-04 09:37:25 +0100560/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200561static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200562 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100563{
564 int field;
565 int started = 0;
566
567 if (!chunk_strcat(out, "["))
568 return 0;
569
570 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
571 int old_len;
572
573 if (!field_format(info, field))
574 continue;
575
576 if (started && !chunk_strcat(out, ","))
577 goto err;
578 started = 1;
579
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200580 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100581 chunk_appendf(out,
582 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
583 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200584 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100585 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200586 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100587 goto err;
588
589 if (!stats_emit_json_field_tags(out, &info[field]))
590 goto err;
591
592 if (!stats_emit_json_data_field(out, &info[field]))
593 goto err;
594
595 if (!chunk_strcat(out, "}"))
596 goto err;
597 }
598
599 if (!chunk_strcat(out, "]"))
600 goto err;
601 return 1;
602
603err:
604 chunk_reset(out);
605 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
606 return 0;
607}
608
609/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200610static int stats_dump_fields_json(struct buffer *out,
611 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200612 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100613{
614 int field;
615 int started = 0;
616
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200617 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100618 return 0;
619 if (!chunk_strcat(out, "["))
620 return 0;
621
622 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
623 const char *obj_type;
624 int old_len;
625
626 if (!stats[field].type)
627 continue;
628
629 if (started && !chunk_strcat(out, ","))
630 goto err;
631 started = 1;
632
633 switch (stats[ST_F_TYPE].u.u32) {
634 case STATS_TYPE_FE: obj_type = "Frontend"; break;
635 case STATS_TYPE_BE: obj_type = "Backend"; break;
636 case STATS_TYPE_SO: obj_type = "Listener"; break;
637 case STATS_TYPE_SV: obj_type = "Server"; break;
638 default: obj_type = "Unknown"; break;
639 }
640
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200641 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100642 chunk_appendf(out,
643 "{"
644 "\"objType\":\"%s\","
645 "\"proxyId\":%d,"
646 "\"id\":%d,"
647 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
648 "\"processNum\":%u,",
649 obj_type, stats[ST_F_IID].u.u32,
650 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200651 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200652 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100653 goto err;
654
655 if (!stats_emit_json_field_tags(out, &stats[field]))
656 goto err;
657
658 if (!stats_emit_json_data_field(out, &stats[field]))
659 goto err;
660
661 if (!chunk_strcat(out, "}"))
662 goto err;
663 }
664
665 if (!chunk_strcat(out, "]"))
666 goto err;
667
668 return 1;
669
670err:
671 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200672 if (flags & STAT_STARTED)
673 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100674 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
675 return 0;
676}
677
William Lallemand74c24fb2016-11-21 17:18:36 +0100678/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200679 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200680 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100681 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200682static int stats_dump_fields_html(struct buffer *out,
683 const struct field *stats,
684 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100685{
Willy Tarreau83061a82018-07-13 11:56:34 +0200686 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100687
688 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
689 chunk_appendf(out,
690 /* name, queue */
691 "<tr class=\"frontend\">");
692
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200693 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100694 /* Column sub-heading for Enable or Disable server */
695 chunk_appendf(out, "<td></td>");
696 }
697
698 chunk_appendf(out,
699 "<td class=ac>"
700 "<a name=\"%s/Frontend\"></a>"
701 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
702 "<td colspan=3></td>"
703 "",
704 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
705
706 chunk_appendf(out,
707 /* sessions rate : current */
708 "<td><u>%s<div class=tips><table class=det>"
709 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
710 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
711 "",
712 U2H(stats[ST_F_RATE].u.u32),
713 U2H(stats[ST_F_CONN_RATE].u.u32),
714 U2H(stats[ST_F_RATE].u.u32));
715
716 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
717 chunk_appendf(out,
718 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
719 U2H(stats[ST_F_REQ_RATE].u.u32));
720
721 chunk_appendf(out,
722 "</table></div></u></td>"
723 /* sessions rate : max */
724 "<td><u>%s<div class=tips><table class=det>"
725 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
726 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
727 "",
728 U2H(stats[ST_F_RATE_MAX].u.u32),
729 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
730 U2H(stats[ST_F_RATE_MAX].u.u32));
731
732 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
733 chunk_appendf(out,
734 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
735 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
736
737 chunk_appendf(out,
738 "</table></div></u></td>"
739 /* sessions rate : limit */
740 "<td>%s</td>",
741 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
742
743 chunk_appendf(out,
744 /* sessions: current, max, limit, total */
745 "<td>%s</td><td>%s</td><td>%s</td>"
746 "<td><u>%s<div class=tips><table class=det>"
747 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
748 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
749 "",
750 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
751 U2H(stats[ST_F_STOT].u.u64),
752 U2H(stats[ST_F_CONN_TOT].u.u64),
753 U2H(stats[ST_F_STOT].u.u64));
754
755 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
756 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
757 chunk_appendf(out,
758 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
759 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
760 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
761 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
762 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
763 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
764 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
765 "<tr><th>- other responses:</th><td>%s</td></tr>"
766 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100767 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
768 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200769 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100770 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100771 "",
772 U2H(stats[ST_F_REQ_TOT].u.u64),
773 U2H(stats[ST_F_HRSP_1XX].u.u64),
774 U2H(stats[ST_F_HRSP_2XX].u.u64),
775 U2H(stats[ST_F_COMP_RSP].u.u64),
776 stats[ST_F_HRSP_2XX].u.u64 ?
777 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
778 U2H(stats[ST_F_HRSP_3XX].u.u64),
779 U2H(stats[ST_F_HRSP_4XX].u.u64),
780 U2H(stats[ST_F_HRSP_5XX].u.u64),
781 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200782 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100783 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
784 U2H(stats[ST_F_CACHE_HITS].u.u64),
785 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
786 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100787 U2H(stats[ST_F_WREW].u.u64),
788 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100789 }
790
791 chunk_appendf(out,
792 "</table></div></u></td>"
793 /* sessions: lbtot, lastsess */
794 "<td></td><td></td>"
795 /* bytes : in */
796 "<td>%s</td>"
797 "",
798 U2H(stats[ST_F_BIN].u.u64));
799
800 chunk_appendf(out,
801 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
802 "<td>%s%s<div class=tips><table class=det>"
803 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
804 "<tr><th>Compression in:</th><td>%s</td></tr>"
805 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
806 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
807 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
808 "</table></div>%s</td>",
809 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
810 U2H(stats[ST_F_BOUT].u.u64),
811 U2H(stats[ST_F_BOUT].u.u64),
812 U2H(stats[ST_F_COMP_IN].u.u64),
813 U2H(stats[ST_F_COMP_OUT].u.u64),
814 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
815 U2H(stats[ST_F_COMP_BYP].u.u64),
816 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
817 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,
818 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
819
820 chunk_appendf(out,
821 /* denied: req, resp */
822 "<td>%s</td><td>%s</td>"
823 /* errors : request, connect, response */
824 "<td>%s</td><td></td><td></td>"
825 /* warnings: retries, redispatches */
826 "<td></td><td></td>"
827 /* server status : reflect frontend status */
828 "<td class=ac>%s</td>"
829 /* rest of server: nothing */
830 "<td class=ac colspan=8></td></tr>"
831 "",
832 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
833 U2H(stats[ST_F_EREQ].u.u64),
834 field_str(stats, ST_F_STATUS));
835 }
836 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
837 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200838 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100839 /* Column sub-heading for Enable or Disable server */
840 chunk_appendf(out, "<td></td>");
841 }
842
843 chunk_appendf(out,
844 /* frontend name, listener name */
845 "<td class=ac><a name=\"%s/+%s\"></a>%s"
846 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
847 "",
848 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200849 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100850 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
851
Willy Tarreau708c4162019-10-09 10:19:16 +0200852 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100853 chunk_appendf(out, "<div class=tips>");
854
Willy Tarreau90807112020-02-25 08:16:33 +0100855 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100856 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
857 else if (*field_str(stats, ST_F_ADDR) == '[')
858 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
859 else if (*field_str(stats, ST_F_ADDR))
860 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
861
862 /* id */
863 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
864 }
865
866 chunk_appendf(out,
867 /* queue */
868 "%s</td><td colspan=3></td>"
869 /* sessions rate: current, max, limit */
870 "<td colspan=3>&nbsp;</td>"
871 /* sessions: current, max, limit, total, lbtot, lastsess */
872 "<td>%s</td><td>%s</td><td>%s</td>"
873 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
874 /* bytes: in, out */
875 "<td>%s</td><td>%s</td>"
876 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200877 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100878 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
879 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
880
881 chunk_appendf(out,
882 /* denied: req, resp */
883 "<td>%s</td><td>%s</td>"
884 /* errors: request, connect, response */
885 "<td>%s</td><td></td><td></td>"
886 /* warnings: retries, redispatches */
887 "<td></td><td></td>"
888 /* server status: reflect listener status */
889 "<td class=ac>%s</td>"
890 /* rest of server: nothing */
891 "<td class=ac colspan=8></td></tr>"
892 "",
893 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
894 U2H(stats[ST_F_EREQ].u.u64),
895 field_str(stats, ST_F_STATUS));
896 }
897 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
898 const char *style;
899
900 /* determine the style to use depending on the server's state,
901 * its health and weight. There isn't a 1-to-1 mapping between
902 * state and styles for the cases where the server is (still)
903 * up. The reason is that we don't want to report nolb and
904 * drain with the same color.
905 */
906
907 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
908 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
909 style = "down";
910 }
911 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
912 style = "going_up";
913 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400914 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
915 style = "draining";
916 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100917 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
918 style = "going_down";
919 }
920 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
921 style = "nolb";
922 }
923 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
924 style = "no_check";
925 }
926 else if (!stats[ST_F_CHKFAIL].type ||
927 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
928 /* no check or max health = UP */
929 if (stats[ST_F_WEIGHT].u.u32)
930 style = "up";
931 else
932 style = "draining";
933 }
934 else {
935 style = "going_down";
936 }
937
938 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
939 chunk_appendf(out, "<tr class=\"maintain\">");
940 else
941 chunk_appendf(out,
942 "<tr class=\"%s_%s\">",
943 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
944
945
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200946 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100947 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000948 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
949 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100950 field_str(stats, ST_F_SVNAME));
951
952 chunk_appendf(out,
953 "<td class=ac><a name=\"%s/%s\"></a>%s"
954 "<a class=lfsb href=\"#%s/%s\">%s</a>"
955 "",
956 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200957 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100958 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
959
Willy Tarreau708c4162019-10-09 10:19:16 +0200960 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100961 chunk_appendf(out, "<div class=tips>");
962
Willy Tarreau90807112020-02-25 08:16:33 +0100963 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100964 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
965 else if (*field_str(stats, ST_F_ADDR) == '[')
966 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
967 else if (*field_str(stats, ST_F_ADDR))
968 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
969
970 /* id */
971 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
972
973 /* cookie */
974 if (stats[ST_F_COOKIE].type) {
975 chunk_appendf(out, ", cookie: '");
976 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
977 chunk_htmlencode(out, &src);
978 chunk_appendf(out, "'");
979 }
980
981 chunk_appendf(out, "</div>");
982 }
983
984 chunk_appendf(out,
985 /* queue : current, max, limit */
986 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
987 /* sessions rate : current, max, limit */
988 "<td>%s</td><td>%s</td><td></td>"
989 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200990 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100991 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
992 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
993
994 chunk_appendf(out,
995 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200996 "<td><u>%s<div class=tips>"
997 "<table class=det>"
998 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200999 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001000 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001001 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1002 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001003 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001004 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1005 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1006 "</table></div></u>"
1007 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001008 "<td><u>%s<div class=tips><table class=det>"
1009 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1010 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001011 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001012 U2H(stats[ST_F_SCUR].u.u32),
1013 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1014 U2H(stats[ST_F_SRV_ICUR].u.u32),
1015 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1016 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001017 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001018
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001019 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1020 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1021 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001022 U2H(stats[ST_F_STOT].u.u64),
1023 U2H(stats[ST_F_STOT].u.u64));
1024
1025 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1026 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001027 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001028 "<tr><th>New connections:</th><td>%s</td></tr>"
1029 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001030 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001031 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1032 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1033 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1034 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1035 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1036 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001037 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001038 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001039 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001040 U2H(stats[ST_F_CONNECT].u.u64),
1041 U2H(stats[ST_F_REUSE].u.u64),
1042 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1043 (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 +02001044 U2H(stats[ST_F_REQ_TOT].u.u64),
1045 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1046 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1047 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1048 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1049 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1050 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1051 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1052 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1053 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1054 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1055 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1056 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001057 U2H(stats[ST_F_WREW].u.u64),
1058 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001059 }
1060
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001061 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1062 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1063 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1064 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1065 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001066 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001067 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1068 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1069 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1070 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001071
1072 chunk_appendf(out,
1073 "</table></div></u></td>"
1074 /* sessions: lbtot, last */
1075 "<td>%s</td><td>%s</td>",
1076 U2H(stats[ST_F_LBTOT].u.u64),
1077 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1078
1079 chunk_appendf(out,
1080 /* bytes : in, out */
1081 "<td>%s</td><td>%s</td>"
1082 /* denied: req, resp */
1083 "<td></td><td>%s</td>"
1084 /* errors : request, connect */
1085 "<td></td><td>%s</td>"
1086 /* errors : response */
1087 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1088 /* warnings: retries, redispatches */
1089 "<td>%lld</td><td>%lld</td>"
1090 "",
1091 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1092 U2H(stats[ST_F_DRESP].u.u64),
1093 U2H(stats[ST_F_ECON].u.u64),
1094 U2H(stats[ST_F_ERESP].u.u64),
1095 (long long)stats[ST_F_CLI_ABRT].u.u64,
1096 (long long)stats[ST_F_SRV_ABRT].u.u64,
1097 (long long)stats[ST_F_WRETR].u.u64,
1098 (long long)stats[ST_F_WREDIS].u.u64);
1099
1100 /* status, last change */
1101 chunk_appendf(out, "<td class=ac>");
1102
1103 /* FIXME!!!!
1104 * LASTCHG should contain the last change for *this* server and must be computed
1105 * properly above, as was done below, ie: this server if maint, otherwise ref server
1106 * if tracking. Note that ref is either local or remote depending on tracking.
1107 */
1108
1109
1110 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1111 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1112 }
1113 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1114 chunk_strcat(out, "<i>no check</i>");
1115 }
1116 else {
1117 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1118 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1119 if (stats[ST_F_CHECK_HEALTH].u.u32)
1120 chunk_strcat(out, " &uarr;");
1121 }
1122 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1123 chunk_strcat(out, " &darr;");
1124 }
1125
1126 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1127 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1128 chunk_appendf(out,
1129 "</td><td class=ac><u> %s",
1130 field_str(stats, ST_F_AGENT_STATUS));
1131
1132 if (stats[ST_F_AGENT_CODE].type)
1133 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1134
1135 if (stats[ST_F_AGENT_DURATION].type)
1136 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1137
1138 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1139
1140 if (*field_str(stats, ST_F_LAST_AGT)) {
1141 chunk_appendf(out, ": ");
1142 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1143 chunk_htmlencode(out, &src);
1144 }
1145 chunk_appendf(out, "</div></u>");
1146 }
1147 else if (stats[ST_F_CHECK_STATUS].type) {
1148 chunk_appendf(out,
1149 "</td><td class=ac><u> %s",
1150 field_str(stats, ST_F_CHECK_STATUS));
1151
1152 if (stats[ST_F_CHECK_CODE].type)
1153 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1154
1155 if (stats[ST_F_CHECK_DURATION].type)
1156 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1157
1158 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1159
1160 if (*field_str(stats, ST_F_LAST_CHK)) {
1161 chunk_appendf(out, ": ");
1162 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1163 chunk_htmlencode(out, &src);
1164 }
1165 chunk_appendf(out, "</div></u>");
1166 }
1167 else
1168 chunk_appendf(out, "</td><td>");
1169
1170 chunk_appendf(out,
1171 /* weight */
1172 "</td><td class=ac>%d</td>"
1173 /* act, bck */
1174 "<td class=ac>%s</td><td class=ac>%s</td>"
1175 "",
1176 stats[ST_F_WEIGHT].u.u32,
1177 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1178 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1179
1180 /* check failures: unique, fatal, down time */
1181 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1182 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1183 }
1184 else if (stats[ST_F_CHKFAIL].type) {
1185 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1186
1187 if (stats[ST_F_HANAFAIL].type)
1188 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1189
1190 chunk_appendf(out,
1191 "<div class=tips>Failed Health Checks%s</div></u></td>"
1192 "<td>%lld</td><td>%s</td>"
1193 "",
1194 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1195 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1196 }
1197 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1198 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1199 chunk_appendf(out,
1200 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1201 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1202 }
1203 else
1204 chunk_appendf(out, "<td colspan=3></td>");
1205
1206 /* throttle */
1207 if (stats[ST_F_THROTTLE].type)
1208 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1209 else
1210 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1211 }
1212 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1213 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001214 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001215 /* Column sub-heading for Enable or Disable server */
1216 chunk_appendf(out, "<td></td>");
1217 }
1218 chunk_appendf(out,
1219 "<td class=ac>"
1220 /* name */
1221 "%s<a name=\"%s/Backend\"></a>"
1222 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1223 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001224 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001225 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1226
Willy Tarreau708c4162019-10-09 10:19:16 +02001227 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001228 /* balancing */
1229 chunk_appendf(out, "<div class=tips>balancing: %s",
1230 field_str(stats, ST_F_ALGO));
1231
1232 /* cookie */
1233 if (stats[ST_F_COOKIE].type) {
1234 chunk_appendf(out, ", cookie: '");
1235 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1236 chunk_htmlencode(out, &src);
1237 chunk_appendf(out, "'");
1238 }
1239 chunk_appendf(out, "</div>");
1240 }
1241
1242 chunk_appendf(out,
1243 "%s</td>"
1244 /* queue : current, max */
1245 "<td>%s</td><td>%s</td><td></td>"
1246 /* sessions rate : current, max, limit */
1247 "<td>%s</td><td>%s</td><td></td>"
1248 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001249 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001250 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1251 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1252
1253 chunk_appendf(out,
1254 /* sessions: current, max, limit, total */
1255 "<td>%s</td><td>%s</td><td>%s</td>"
1256 "<td><u>%s<div class=tips><table class=det>"
1257 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1258 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001259 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 +01001260 U2H(stats[ST_F_STOT].u.u64),
1261 U2H(stats[ST_F_STOT].u.u64));
1262
1263 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1264 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1265 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001266 "<tr><th>New connections:</th><td>%s</td></tr>"
1267 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001268 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1269 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1270 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1271 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1272 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1273 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1274 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1275 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001276 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1277 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001278 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001279 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001280 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001281 U2H(stats[ST_F_CONNECT].u.u64),
1282 U2H(stats[ST_F_REUSE].u.u64),
1283 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1284 (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 +01001285 U2H(stats[ST_F_REQ_TOT].u.u64),
1286 U2H(stats[ST_F_HRSP_1XX].u.u64),
1287 U2H(stats[ST_F_HRSP_2XX].u.u64),
1288 U2H(stats[ST_F_COMP_RSP].u.u64),
1289 stats[ST_F_HRSP_2XX].u.u64 ?
1290 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1291 U2H(stats[ST_F_HRSP_3XX].u.u64),
1292 U2H(stats[ST_F_HRSP_4XX].u.u64),
1293 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001294 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001295 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1296 U2H(stats[ST_F_CACHE_HITS].u.u64),
1297 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1298 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001299 U2H(stats[ST_F_WREW].u.u64),
1300 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001301 }
1302
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001303 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1304 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1305 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1306 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1307 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001308 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001309 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1310 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1311 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1312 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001313
1314 chunk_appendf(out,
1315 "</table></div></u></td>"
1316 /* sessions: lbtot, last */
1317 "<td>%s</td><td>%s</td>"
1318 /* bytes: in */
1319 "<td>%s</td>"
1320 "",
1321 U2H(stats[ST_F_LBTOT].u.u64),
1322 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1323 U2H(stats[ST_F_BIN].u.u64));
1324
1325 chunk_appendf(out,
1326 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1327 "<td>%s%s<div class=tips><table class=det>"
1328 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1329 "<tr><th>Compression in:</th><td>%s</td></tr>"
1330 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1331 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1332 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1333 "</table></div>%s</td>",
1334 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1335 U2H(stats[ST_F_BOUT].u.u64),
1336 U2H(stats[ST_F_BOUT].u.u64),
1337 U2H(stats[ST_F_COMP_IN].u.u64),
1338 U2H(stats[ST_F_COMP_OUT].u.u64),
1339 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1340 U2H(stats[ST_F_COMP_BYP].u.u64),
1341 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1342 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,
1343 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1344
1345 chunk_appendf(out,
1346 /* denied: req, resp */
1347 "<td>%s</td><td>%s</td>"
1348 /* errors : request, connect */
1349 "<td></td><td>%s</td>"
1350 /* errors : response */
1351 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1352 /* warnings: retries, redispatches */
1353 "<td>%lld</td><td>%lld</td>"
1354 /* backend status: reflect backend status (up/down): we display UP
1355 * if the backend has known working servers or if it has no server at
1356 * all (eg: for stats). Then we display the total weight, number of
1357 * active and backups. */
1358 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1359 "<td class=ac>%d</td><td class=ac>%d</td>"
1360 "",
1361 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1362 U2H(stats[ST_F_ECON].u.u64),
1363 U2H(stats[ST_F_ERESP].u.u64),
1364 (long long)stats[ST_F_CLI_ABRT].u.u64,
1365 (long long)stats[ST_F_SRV_ABRT].u.u64,
1366 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1367 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1368 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1369 stats[ST_F_WEIGHT].u.u32,
1370 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1371
1372 chunk_appendf(out,
1373 /* rest of backend: nothing, down transitions, total downtime, throttle */
1374 "<td class=ac>&nbsp;</td><td>%d</td>"
1375 "<td>%s</td>"
1376 "<td></td>"
1377 "</tr>",
1378 stats[ST_F_CHKDOWN].u.u32,
1379 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1380 }
1381 return 1;
1382}
1383
Willy Tarreau43241ff2019-10-09 11:27:51 +02001384static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001385{
Simon Horman05ee2132017-01-04 09:37:25 +01001386 int ret;
1387
William Lallemand74c24fb2016-11-21 17:18:36 +01001388 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001389 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001390 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001391 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001392 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001393 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001394 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001395 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001396
1397 if (ret)
1398 appctx->ctx.stats.flags |= STAT_STARTED;
1399
1400 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001401}
1402
1403/* Fill <stats> with the frontend statistics. <stats> is
1404 * preallocated array of length <len>. The length of the array
1405 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1406 * this value, the function returns 0, otherwise, it returns 1.
1407 */
1408int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1409{
1410 if (len < ST_F_TOTAL_FIELDS)
1411 return 0;
1412
1413 memset(stats, 0, sizeof(*stats) * len);
1414
1415 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1416 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1417 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1418 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1419 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1420 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1421 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1422 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1423 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1424 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1425 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1426 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1427 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1428 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1429 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1430 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1431 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1432 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1433 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1434 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1435 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1436 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001437 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001438 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001439
1440 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1441 if (px->mode == PR_MODE_HTTP) {
1442 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1443 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1444 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1445 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1446 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1447 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1448 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001449 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1450 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001451 }
1452
1453 /* requests : req_rate, req_rate_max, req_tot, */
1454 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1455 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1456 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1457
1458 /* compression: in, out, bypassed, responses */
1459 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1460 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1461 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1462 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1463
1464 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1465 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1466 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1467 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1468
1469 return 1;
1470}
1471
1472/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1473 * the state from stream interface <si>. The caller is responsible for clearing
1474 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1475 */
1476static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1477{
1478 struct appctx *appctx = __objt_appctx(si->end);
1479
1480 if (!(px->cap & PR_CAP_FE))
1481 return 0;
1482
1483 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1484 return 0;
1485
1486 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1487 return 0;
1488
Willy Tarreau43241ff2019-10-09 11:27:51 +02001489 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001490}
1491
1492/* Fill <stats> with the listener statistics. <stats> is
1493 * preallocated array of length <len>. The length of the array
1494 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1495 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001496 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001497 */
1498int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1499 struct field *stats, int len)
1500{
Willy Tarreau83061a82018-07-13 11:56:34 +02001501 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001502
1503 if (len < ST_F_TOTAL_FIELDS)
1504 return 0;
1505
1506 if (!l->counters)
1507 return 0;
1508
1509 chunk_reset(out);
1510 memset(stats, 0, sizeof(*stats) * len);
1511
1512 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1513 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1514 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1515 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1516 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1517 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1518 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1519 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1520 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1521 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1522 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1523 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1524 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1525 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001526 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 +01001527 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1528 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1529 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1530 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001531 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001532 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001533
Willy Tarreau708c4162019-10-09 10:19:16 +02001534 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001535 char str[INET6_ADDRSTRLEN];
1536 int port;
1537
1538 port = get_host_port(&l->addr);
1539 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1540 case AF_INET:
1541 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1542 chunk_appendf(out, "%s:%d", str, port);
1543 break;
1544 case AF_INET6:
1545 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1546 chunk_appendf(out, "[%s]:%d", str, port);
1547 break;
1548 case AF_UNIX:
1549 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1550 break;
1551 case -1:
1552 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1553 chunk_strcat(out, strerror(errno));
1554 break;
1555 default: /* address family not supported */
1556 break;
1557 }
1558 }
1559
1560 return 1;
1561}
1562
1563/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001564 * from stream interface <si>. The caller is responsible for clearing the trash
1565 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001566 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001567static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001568{
1569 struct appctx *appctx = __objt_appctx(si->end);
1570
Willy Tarreau43241ff2019-10-09 11:27:51 +02001571 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001572 return 0;
1573
Willy Tarreau43241ff2019-10-09 11:27:51 +02001574 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001575}
1576
1577enum srv_stats_state {
1578 SRV_STATS_STATE_DOWN = 0,
1579 SRV_STATS_STATE_DOWN_AGENT,
1580 SRV_STATS_STATE_GOING_UP,
1581 SRV_STATS_STATE_UP_GOING_DOWN,
1582 SRV_STATS_STATE_UP,
1583 SRV_STATS_STATE_NOLB_GOING_DOWN,
1584 SRV_STATS_STATE_NOLB,
1585 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1586 SRV_STATS_STATE_DRAIN,
1587 SRV_STATS_STATE_DRAIN_AGENT,
1588 SRV_STATS_STATE_NO_CHECK,
1589
1590 SRV_STATS_STATE_COUNT, /* Must be last */
1591};
1592
1593static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1594 [SRV_STATS_STATE_DOWN] = "DOWN",
1595 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1596 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1597 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1598 [SRV_STATS_STATE_UP] = "UP",
1599 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1600 [SRV_STATS_STATE_NOLB] = "NOLB",
1601 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1602 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1603 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1604 [SRV_STATS_STATE_NO_CHECK] = "no check"
1605};
1606
1607/* Fill <stats> with the server statistics. <stats> is
1608 * preallocated array of length <len>. The length of the array
1609 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1610 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001611 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001612 */
1613int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1614 struct field *stats, int len)
1615{
1616 struct server *via, *ref;
1617 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001618 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001619 enum srv_stats_state state;
1620 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001621 long long srv_samples_counter;
1622 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001623
1624 if (len < ST_F_TOTAL_FIELDS)
1625 return 0;
1626
1627 memset(stats, 0, sizeof(*stats) * len);
1628
1629 /* we have "via" which is the tracked server as described in the configuration,
1630 * and "ref" which is the checked server and the end of the chain.
1631 */
1632 via = sv->track ? sv->track : sv;
1633 ref = via;
1634 while (ref->track)
1635 ref = ref->track;
1636
Emeric Brun52a91d32017-08-31 14:41:55 +02001637 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001638 if ((ref->check.state & CHK_ST_ENABLED) &&
1639 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1640 state = SRV_STATS_STATE_UP_GOING_DOWN;
1641 } else {
1642 state = SRV_STATS_STATE_UP;
1643 }
1644
Emeric Brun52a91d32017-08-31 14:41:55 +02001645 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001646 if (ref->agent.state & CHK_ST_ENABLED)
1647 state = SRV_STATS_STATE_DRAIN_AGENT;
1648 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1649 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1650 else
1651 state = SRV_STATS_STATE_DRAIN;
1652 }
1653
1654 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1655 state = SRV_STATS_STATE_NO_CHECK;
1656 }
1657 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001658 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001659 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1660 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1661 state = SRV_STATS_STATE_NOLB;
1662 } else {
1663 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1664 }
1665 }
1666 else { /* stopped */
1667 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1668 state = SRV_STATS_STATE_DOWN_AGENT;
1669 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1670 state = SRV_STATS_STATE_DOWN; /* DOWN */
1671 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1672 state = SRV_STATS_STATE_GOING_UP;
1673 } else {
1674 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1675 }
1676 }
1677
1678 chunk_reset(out);
1679
1680 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1681 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1682 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1683 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1684 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1685 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1686 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1687
1688 if (sv->maxconn)
1689 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1690
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001691 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1692 if (sv->max_idle_conns != -1)
1693 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1694
William Lallemand74c24fb2016-11-21 17:18:36 +01001695 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1696 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1697 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001698 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001699 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1700 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1701 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1702 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001703 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001704 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001705 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1706 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001707
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001708 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1709 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1710 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001711 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001712
William Lallemand74c24fb2016-11-21 17:18:36 +01001713 /* status */
1714 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001715 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001716 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001717 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001718 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001719 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001720 chunk_appendf(out, "MAINT");
1721 else
1722 chunk_appendf(out,
1723 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001724 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1725 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001726
1727 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1728 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001729 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 +01001730 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1731 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1732
1733 /* check failures: unique, fatal; last change, total downtime */
1734 if (sv->check.state & CHK_ST_ENABLED) {
1735 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1736 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1737 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1738 }
1739
1740 if (sv->maxqueue)
1741 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1742
1743 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1744 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1745 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1746
Emeric Brun52a91d32017-08-31 14:41:55 +02001747 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001748 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1749
1750 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1751
1752 if (sv->track) {
1753 char *fld_track = chunk_newstr(out);
1754
1755 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1756 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1757 }
1758
1759 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1760 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1761 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1762
1763 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1764 const char *fld_chksts;
1765
1766 fld_chksts = chunk_newstr(out);
1767 chunk_strcat(out, "* "); // for check in progress
1768 chunk_strcat(out, get_check_status_info(sv->check.status));
1769 if (!(sv->check.state & CHK_ST_INPROGRESS))
1770 fld_chksts += 2; // skip "* "
1771 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1772
1773 if (sv->check.status >= HCHK_STATUS_L57DATA)
1774 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1775
1776 if (sv->check.status >= HCHK_STATUS_CHECKED)
1777 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1778
1779 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1780 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1781 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1782 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1783 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1784 }
1785
1786 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1787 const char *fld_chksts;
1788
1789 fld_chksts = chunk_newstr(out);
1790 chunk_strcat(out, "* "); // for check in progress
1791 chunk_strcat(out, get_check_status_info(sv->agent.status));
1792 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1793 fld_chksts += 2; // skip "* "
1794 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1795
1796 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1797 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1798
1799 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1800 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1801
1802 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1803 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1804 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1805 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1806 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1807 }
1808
1809 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1810 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001811 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001812 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1813 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1814 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1815 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1816 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1817 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1818 }
1819
1820 if (ref->observe)
1821 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1822
1823 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1824 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1825 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1826
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001827 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1828 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1829 srv_samples_window = srv_samples_counter;
1830
1831 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1832 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1833 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1834 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001835
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001836 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1837 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1838 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1839 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1840
Willy Tarreau708c4162019-10-09 10:19:16 +02001841 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001842 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1843 case AF_INET:
1844 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001845 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001846 break;
1847 case AF_INET6:
1848 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001849 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001850 break;
1851 case AF_UNIX:
1852 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1853 break;
1854 case -1:
1855 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1856 chunk_strcat(out, strerror(errno));
1857 break;
1858 default: /* address family not supported */
1859 break;
1860 }
1861
1862 if (sv->cookie)
1863 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1864 }
1865
1866 return 1;
1867}
1868
1869/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001870 * from stream interface <si>, and server state <state>. The caller is
1871 * responsible for clearing the trash if needed. Returns non-zero if it emits
1872 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001873 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001874static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001875{
1876 struct appctx *appctx = __objt_appctx(si->end);
1877
Willy Tarreau43241ff2019-10-09 11:27:51 +02001878 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001879 return 0;
1880
Willy Tarreau43241ff2019-10-09 11:27:51 +02001881 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001882}
1883
1884/* Fill <stats> with the backend statistics. <stats> is
1885 * preallocated array of length <len>. The length of the array
1886 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1887 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001888 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001889 */
1890int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1891{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001892 long long be_samples_counter;
1893 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1894
William Lallemand74c24fb2016-11-21 17:18:36 +01001895 if (len < ST_F_TOTAL_FIELDS)
1896 return 0;
1897
1898 memset(stats, 0, sizeof(*stats) * len);
1899
1900 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1901 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1902 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1903 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1904 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001905 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001906 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1907 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1908 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1909 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1910 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1911 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1912 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1913 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1914 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1915 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1916 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001917 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001918 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001919 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1920 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001921 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1922 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1923 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1924 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1925 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1926 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1927 if (px->srv)
1928 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1929
1930 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1931 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1932 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1933 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1934 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1935 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1936 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1937
Willy Tarreau708c4162019-10-09 10:19:16 +02001938 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001939 if (px->cookie_name)
1940 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1941 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1942 }
1943
1944 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1945 if (px->mode == PR_MODE_HTTP) {
1946 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1947 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1948 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1949 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1950 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1951 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1952 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001953 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1954 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001955 }
1956
1957 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1958 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1959
1960 /* compression: in, out, bypassed, responses */
1961 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1962 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1963 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1964 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1965 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1966
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001967 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1968 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1969 be_samples_window = be_samples_counter;
1970
1971 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1972 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1973 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1974 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 +01001975
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001976 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1977 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1978 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1979 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1980
William Lallemand74c24fb2016-11-21 17:18:36 +01001981 return 1;
1982}
1983
1984/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001985 * interface <si>. The caller is responsible for clearing the trash if needed.
1986 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001987 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001988static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001989{
1990 struct appctx *appctx = __objt_appctx(si->end);
1991
1992 if (!(px->cap & PR_CAP_BE))
1993 return 0;
1994
1995 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1996 return 0;
1997
Willy Tarreau43241ff2019-10-09 11:27:51 +02001998 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001999 return 0;
2000
Willy Tarreau43241ff2019-10-09 11:27:51 +02002001 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002002}
2003
2004/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2005 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2006 * for clearing the trash if needed.
2007 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002008static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002009{
2010 struct appctx *appctx = __objt_appctx(si->end);
2011 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2012
2013 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2014 /* A form to enable/disable this proxy servers */
2015
2016 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2017 scope_txt[0] = 0;
2018 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002019 const char *scope_ptr = stats_scope_ptr(appctx, si);
2020
William Lallemand74c24fb2016-11-21 17:18:36 +01002021 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002022 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002023 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2024 }
2025
2026 chunk_appendf(&trash,
2027 "<form method=\"post\">");
2028 }
2029
2030 /* print a new table */
2031 chunk_appendf(&trash,
2032 "<table class=\"tbl\" width=\"100%%\">\n"
2033 "<tr class=\"titre\">"
2034 "<th class=\"pxname\" width=\"10%%\">");
2035
2036 chunk_appendf(&trash,
2037 "<a name=\"%s\"></a>%s"
2038 "<a class=px href=\"#%s\">%s</a>",
2039 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002040 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002041 px->id, px->id);
2042
Willy Tarreau676c29e2019-10-09 10:50:01 +02002043 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002044 /* cap, mode, id */
2045 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2046 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2047 px->uuid);
2048 chunk_appendf(&trash, "</div>");
2049 }
2050
2051 chunk_appendf(&trash,
2052 "%s</th>"
2053 "<th class=\"%s\" width=\"90%%\">%s</th>"
2054 "</tr>\n"
2055 "</table>\n"
2056 "<table class=\"tbl\" width=\"100%%\">\n"
2057 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002058 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002059 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2060
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002061 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002062 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002063 if ((px->cap & PR_CAP_BE) && px->srv)
2064 chunk_appendf(&trash,
2065 "<th rowspan=2 width=1><input type=\"checkbox\" "
2066 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2067 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2068 px->id,
2069 px->id);
2070 else
2071 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002072 }
2073
2074 chunk_appendf(&trash,
2075 "<th rowspan=2></th>"
2076 "<th colspan=3>Queue</th>"
2077 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2078 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2079 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2080 "<th colspan=9>Server</th>"
2081 "</tr>\n"
2082 "<tr class=\"titre\">"
2083 "<th>Cur</th><th>Max</th><th>Limit</th>"
2084 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2085 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2086 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2087 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2088 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2089 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2090 "<th>Thrtle</th>\n"
2091 "</tr>");
2092}
2093
2094/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2095 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2096 */
2097static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2098{
2099 struct appctx *appctx = __objt_appctx(si->end);
2100 chunk_appendf(&trash, "</table>");
2101
2102 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2103 /* close the form used to enable/disable this proxy servers */
2104 chunk_appendf(&trash,
2105 "Choose the action to perform on the checked servers : "
2106 "<select name=action>"
2107 "<option value=\"\"></option>"
2108 "<option value=\"ready\">Set state to READY</option>"
2109 "<option value=\"drain\">Set state to DRAIN</option>"
2110 "<option value=\"maint\">Set state to MAINT</option>"
2111 "<option value=\"dhlth\">Health: disable checks</option>"
2112 "<option value=\"ehlth\">Health: enable checks</option>"
2113 "<option value=\"hrunn\">Health: force UP</option>"
2114 "<option value=\"hnolb\">Health: force NOLB</option>"
2115 "<option value=\"hdown\">Health: force DOWN</option>"
2116 "<option value=\"dagent\">Agent: disable checks</option>"
2117 "<option value=\"eagent\">Agent: enable checks</option>"
2118 "<option value=\"arunn\">Agent: force UP</option>"
2119 "<option value=\"adown\">Agent: force DOWN</option>"
2120 "<option value=\"shutdown\">Kill Sessions</option>"
2121 "</select>"
2122 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2123 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2124 "</form>",
2125 px->uuid);
2126 }
2127
2128 chunk_appendf(&trash, "<p>\n");
2129}
2130
2131/*
2132 * Dumps statistics for a proxy. The output is sent to the stream interface's
2133 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2134 * buffer space, or non-zero if everything completed. This function is used
2135 * both by the CLI and the HTTP entry points, and is able to dump the output
2136 * in HTML or CSV formats. If the later, <uri> must be NULL.
2137 */
Christopher Fauletef779222018-10-31 08:47:01 +01002138int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2139 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002140{
2141 struct appctx *appctx = __objt_appctx(si->end);
2142 struct stream *s = si_strm(si);
2143 struct channel *rep = si_ic(si);
2144 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2145 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002146
2147 chunk_reset(&trash);
2148
2149 switch (appctx->ctx.stats.px_st) {
2150 case STAT_PX_ST_INIT:
2151 /* we are on a new proxy */
2152 if (uri && uri->scope) {
2153 /* we have a limited scope, we have to check the proxy name */
2154 struct stat_scope *scope;
2155 int len;
2156
2157 len = strlen(px->id);
2158 scope = uri->scope;
2159
2160 while (scope) {
2161 /* match exact proxy name */
2162 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2163 break;
2164
2165 /* match '.' which means 'self' proxy */
2166 if (!strcmp(scope->px_id, ".") && px == s->be)
2167 break;
2168 scope = scope->next;
2169 }
2170
2171 /* proxy name not found : don't dump anything */
2172 if (scope == NULL)
2173 return 1;
2174 }
2175
2176 /* if the user has requested a limited output and the proxy
2177 * name does not match, skip it.
2178 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002179 if (appctx->ctx.stats.scope_len) {
2180 const char *scope_ptr = stats_scope_ptr(appctx, si);
2181
2182 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2183 return 1;
2184 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002185
2186 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2187 (appctx->ctx.stats.iid != -1) &&
2188 (px->uuid != appctx->ctx.stats.iid))
2189 return 1;
2190
2191 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2192 /* fall through */
2193
2194 case STAT_PX_ST_TH:
2195 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002196 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002197 if (!stats_putchk(rep, htx, &trash))
2198 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002199 }
2200
2201 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2202 /* fall through */
2203
2204 case STAT_PX_ST_FE:
2205 /* print the frontend */
2206 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002207 if (!stats_putchk(rep, htx, &trash))
2208 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002209 }
2210
2211 appctx->ctx.stats.l = px->conf.listeners.n;
2212 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2213 /* fall through */
2214
2215 case STAT_PX_ST_LI:
2216 /* stats.l has been initialized above */
2217 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002218 if (htx) {
2219 if (htx_almost_full(htx))
2220 goto full;
2221 }
2222 else {
2223 if (buffer_almost_full(&rep->buf))
2224 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002225 }
2226
2227 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2228 if (!l->counters)
2229 continue;
2230
2231 if (appctx->ctx.stats.flags & STAT_BOUND) {
2232 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2233 break;
2234
2235 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2236 continue;
2237 }
2238
2239 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002240 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002241 if (!stats_putchk(rep, htx, &trash))
2242 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002243 }
2244 }
2245
2246 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2247 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2248 /* fall through */
2249
2250 case STAT_PX_ST_SV:
2251 /* stats.sv has been initialized above */
2252 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002253 if (htx) {
2254 if (htx_almost_full(htx))
2255 goto full;
2256 }
2257 else {
2258 if (buffer_almost_full(&rep->buf))
2259 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002260 }
2261
2262 sv = appctx->ctx.stats.sv;
2263
2264 if (appctx->ctx.stats.flags & STAT_BOUND) {
2265 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2266 break;
2267
2268 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2269 continue;
2270 }
2271
2272 svs = sv;
2273 while (svs->track)
2274 svs = svs->track;
2275
2276 /* do not report servers which are DOWN and not changing state */
2277 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002278 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2279 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002280 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2281 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2282 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2283 continue;
2284 }
2285
Willy Tarreau43241ff2019-10-09 11:27:51 +02002286 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002287 if (!stats_putchk(rep, htx, &trash))
2288 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002289 }
2290 } /* for sv */
2291
2292 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2293 /* fall through */
2294
2295 case STAT_PX_ST_BE:
2296 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002297 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002298 if (!stats_putchk(rep, htx, &trash))
2299 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002300 }
2301
2302 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2303 /* fall through */
2304
2305 case STAT_PX_ST_END:
2306 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2307 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002308 if (!stats_putchk(rep, htx, &trash))
2309 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002310 }
2311
2312 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2313 /* fall through */
2314
2315 case STAT_PX_ST_FIN:
2316 return 1;
2317
2318 default:
2319 /* unknown state, we should put an abort() here ! */
2320 return 1;
2321 }
Christopher Fauletef779222018-10-31 08:47:01 +01002322
2323 full:
2324 si_rx_room_blk(si);
2325 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002326}
2327
2328/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2329 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2330 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002331static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002332{
2333 /* WARNING! This must fit in the first buffer !!! */
2334 chunk_appendf(&trash,
2335 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2336 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2337 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2338 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2339 "<style type=\"text/css\"><!--\n"
2340 "body {"
2341 " font-family: arial, helvetica, sans-serif;"
2342 " font-size: 12px;"
2343 " font-weight: normal;"
2344 " color: black;"
2345 " background: white;"
2346 "}\n"
2347 "th,td {"
2348 " font-size: 10px;"
2349 "}\n"
2350 "h1 {"
2351 " font-size: x-large;"
2352 " margin-bottom: 0.5em;"
2353 "}\n"
2354 "h2 {"
2355 " font-family: helvetica, arial;"
2356 " font-size: x-large;"
2357 " font-weight: bold;"
2358 " font-style: italic;"
2359 " color: #6020a0;"
2360 " margin-top: 0em;"
2361 " margin-bottom: 0em;"
2362 "}\n"
2363 "h3 {"
2364 " font-family: helvetica, arial;"
2365 " font-size: 16px;"
2366 " font-weight: bold;"
2367 " color: #b00040;"
2368 " background: #e8e8d0;"
2369 " margin-top: 0em;"
2370 " margin-bottom: 0em;"
2371 "}\n"
2372 "li {"
2373 " margin-top: 0.25em;"
2374 " margin-right: 2em;"
2375 "}\n"
2376 ".hr {margin-top: 0.25em;"
2377 " border-color: black;"
2378 " border-bottom-style: solid;"
2379 "}\n"
2380 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2381 ".total {background: #20D0D0;color: #ffff80;}\n"
2382 ".frontend {background: #e8e8d0;}\n"
2383 ".socket {background: #d0d0d0;}\n"
2384 ".backend {background: #e8e8d0;}\n"
2385 ".active_down {background: #ff9090;}\n"
2386 ".active_going_up {background: #ffd020;}\n"
2387 ".active_going_down {background: #ffffa0;}\n"
2388 ".active_up {background: #c0ffc0;}\n"
2389 ".active_nolb {background: #20a0ff;}\n"
2390 ".active_draining {background: #20a0FF;}\n"
2391 ".active_no_check {background: #e0e0e0;}\n"
2392 ".backup_down {background: #ff9090;}\n"
2393 ".backup_going_up {background: #ff80ff;}\n"
2394 ".backup_going_down {background: #c060ff;}\n"
2395 ".backup_up {background: #b0d0ff;}\n"
2396 ".backup_nolb {background: #90b0e0;}\n"
2397 ".backup_draining {background: #cc9900;}\n"
2398 ".backup_no_check {background: #e0e0e0;}\n"
2399 ".maintain {background: #c07820;}\n"
2400 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2401 "\n"
2402 "a.px:link {color: #ffff40; text-decoration: none;}"
2403 "a.px:visited {color: #ffff40; text-decoration: none;}"
2404 "a.px:hover {color: #ffffff; text-decoration: none;}"
2405 "a.lfsb:link {color: #000000; text-decoration: none;}"
2406 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2407 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2408 "\n"
2409 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2410 "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"
2411 "table.tbl td.ac { text-align: center;}\n"
2412 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2413 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2414 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2415 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2416 "\n"
2417 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2418 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2419 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2420 "table.det { border-collapse: collapse; border-style: none; }\n"
2421 "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"
2422 "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"
2423 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2424 "div.tips {\n"
2425 " display:block;\n"
2426 " visibility:hidden;\n"
2427 " z-index:2147483647;\n"
2428 " position:absolute;\n"
2429 " padding:2px 4px 3px;\n"
2430 " background:#f0f060; color:#000000;\n"
2431 " border:1px solid #7040c0;\n"
2432 " white-space:nowrap;\n"
2433 " font-style:normal;font-size:11px;font-weight:normal;\n"
2434 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2435 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2436 "}\n"
2437 "u:hover div.tips {visibility:visible;}\n"
2438 "-->\n"
2439 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002440 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002441 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002442 );
2443}
2444
2445/* Dumps the HTML stats information block to the trash for and uses the state from
2446 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2447 * for clearing the trash if needed.
2448 */
2449static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2450{
2451 struct appctx *appctx = __objt_appctx(si->end);
2452 unsigned int up = (now.tv_sec - start_date.tv_sec);
2453 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002454 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002455 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2456
2457 /* Turn the bytes per second to bits per second and take care of the
2458 * usual ethernet overhead in order to help figure how far we are from
2459 * interface saturation since it's the only case which usually matters.
2460 * For this we count the total size of an Ethernet frame on the wire
2461 * including preamble and IFG (1538) for the largest TCP segment it
2462 * transports (1448 with TCP timestamps). This is not valid for smaller
2463 * packets (under-estimated), but it gives a reasonably accurate
2464 * estimation of how far we are from uplink saturation.
2465 */
2466 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002467
2468 /* WARNING! this has to fit the first packet too.
2469 * We are around 3.5 kB, add adding entries will
2470 * become tricky if we want to support 4kB buffers !
2471 */
2472 chunk_appendf(&trash,
2473 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2474 PRODUCT_NAME "%s</a></h1>\n"
2475 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2476 "<hr width=\"100%%\" class=\"hr\">\n"
2477 "<h3>&gt; General process information</h3>\n"
2478 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002479 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002480 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2481 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2482 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002483 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002484 "Running tasks: %d/%d; idle = %d %%<br>\n"
2485 "</td><td align=\"center\" nowrap>\n"
2486 "<table class=\"lgd\"><tr>\n"
2487 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2488 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2489 "</tr><tr>\n"
2490 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2491 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2492 "</tr><tr>\n"
2493 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2494 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2495 "</tr><tr>\n"
2496 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2497 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2498 "</tr><tr>\n"
2499 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2500 "</tr><tr>\n"
2501 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2502 "</tr></table>\n"
2503 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2504 "</td>"
2505 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2506 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2507 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002508 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2509 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2510 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2511 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2512 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002513 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002514 up / 86400, (up % 86400) / 3600,
2515 (up % 3600) / 60, (up % 60),
2516 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2517 global.rlimit_memmax ? " MB" : "",
2518 global.rlimit_nofile,
2519 global.maxsock, global.maxconn, global.maxpipes,
2520 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002521 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2522 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002523 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002524 );
2525
2526 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002527 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002528 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2529
2530 chunk_appendf(&trash,
2531 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2532 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2533 STAT_SCOPE_TXT_MAXLEN);
2534
2535 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2536 scope_txt[0] = 0;
2537 if (appctx->ctx.stats.scope_len) {
2538 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002539 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002540 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2541 }
2542
2543 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2544 chunk_appendf(&trash,
2545 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2546 uri->uri_prefix,
2547 "",
2548 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2549 scope_txt);
2550 else
2551 chunk_appendf(&trash,
2552 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2553 uri->uri_prefix,
2554 ";up",
2555 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2556 scope_txt);
2557
2558 if (uri->refresh > 0) {
2559 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2560 chunk_appendf(&trash,
2561 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2562 uri->uri_prefix,
2563 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2564 "",
2565 scope_txt);
2566 else
2567 chunk_appendf(&trash,
2568 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2569 uri->uri_prefix,
2570 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2571 ";norefresh",
2572 scope_txt);
2573 }
2574
2575 chunk_appendf(&trash,
2576 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2577 uri->uri_prefix,
2578 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2579 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2580 scope_txt);
2581
2582 chunk_appendf(&trash,
2583 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2584 uri->uri_prefix,
2585 (uri->refresh > 0) ? ";norefresh" : "",
2586 scope_txt);
2587
2588 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002589 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2590 uri->uri_prefix,
2591 (uri->refresh > 0) ? ";norefresh" : "",
2592 scope_txt, uri->uri_prefix);
2593
2594 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002595 "</ul></td>"
2596 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2597 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2598 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2599 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2600 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2601 "</ul>"
2602 "</td>"
2603 "</tr></table>\n"
2604 ""
2605 );
2606
2607 if (appctx->ctx.stats.st_code) {
2608 switch (appctx->ctx.stats.st_code) {
2609 case STAT_STATUS_DONE:
2610 chunk_appendf(&trash,
2611 "<p><div class=active_up>"
2612 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2613 "Action processed successfully."
2614 "</div>\n", uri->uri_prefix,
2615 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2616 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2617 scope_txt);
2618 break;
2619 case STAT_STATUS_NONE:
2620 chunk_appendf(&trash,
2621 "<p><div class=active_going_down>"
2622 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2623 "Nothing has changed."
2624 "</div>\n", uri->uri_prefix,
2625 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2626 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2627 scope_txt);
2628 break;
2629 case STAT_STATUS_PART:
2630 chunk_appendf(&trash,
2631 "<p><div class=active_going_down>"
2632 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2633 "Action partially processed.<br>"
2634 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2635 "</div>\n", uri->uri_prefix,
2636 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2637 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2638 scope_txt);
2639 break;
2640 case STAT_STATUS_ERRP:
2641 chunk_appendf(&trash,
2642 "<p><div class=active_down>"
2643 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2644 "Action not processed because of invalid parameters."
2645 "<ul>"
2646 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002647 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002648 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2649 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2650 "</ul>"
2651 "</div>\n", uri->uri_prefix,
2652 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2653 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2654 scope_txt);
2655 break;
2656 case STAT_STATUS_EXCD:
2657 chunk_appendf(&trash,
2658 "<p><div class=active_down>"
2659 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2660 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2661 "You should retry with less servers at a time.</b>"
2662 "</div>\n", uri->uri_prefix,
2663 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2664 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2665 scope_txt);
2666 break;
2667 case STAT_STATUS_DENY:
2668 chunk_appendf(&trash,
2669 "<p><div class=active_down>"
2670 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2671 "<b>Action denied.</b>"
2672 "</div>\n", uri->uri_prefix,
2673 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2674 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2675 scope_txt);
2676 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002677 case STAT_STATUS_IVAL:
2678 chunk_appendf(&trash,
2679 "<p><div class=active_down>"
2680 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2681 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2682 "</div>\n", uri->uri_prefix,
2683 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2684 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2685 scope_txt);
2686 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002687 default:
2688 chunk_appendf(&trash,
2689 "<p><div class=active_no_check>"
2690 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2691 "Unexpected result."
2692 "</div>\n", uri->uri_prefix,
2693 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2694 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2695 scope_txt);
2696 }
2697 chunk_appendf(&trash, "<p>\n");
2698 }
2699}
2700
2701/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2702 * for clearing the trash if needed.
2703 */
2704static void stats_dump_html_end()
2705{
2706 chunk_appendf(&trash, "</body></html>\n");
2707}
2708
Simon Horman05ee2132017-01-04 09:37:25 +01002709/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2710 * for clearing it if needed.
2711 */
2712static void stats_dump_json_header()
2713{
2714 chunk_strcat(&trash, "[");
2715}
2716
2717
2718/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2719 * for clearing the trash if needed.
2720 */
2721static void stats_dump_json_end()
2722{
2723 chunk_strcat(&trash, "]");
2724}
2725
William Lallemand74c24fb2016-11-21 17:18:36 +01002726/* This function dumps statistics onto the stream interface's read buffer in
2727 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2728 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2729 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2730 * and the stream must be closed, or -1 in case of any error. This function is
2731 * used by both the CLI and the HTTP handlers.
2732 */
Christopher Fauletef779222018-10-31 08:47:01 +01002733static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2734 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002735{
2736 struct appctx *appctx = __objt_appctx(si->end);
2737 struct channel *rep = si_ic(si);
2738 struct proxy *px;
2739
2740 chunk_reset(&trash);
2741
2742 switch (appctx->st2) {
2743 case STAT_ST_INIT:
2744 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2745 /* fall through */
2746
2747 case STAT_ST_HEAD:
2748 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002749 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002750 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2751 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002752 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002753 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002754 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2755 stats_dump_csv_header();
2756
Christopher Fauletef779222018-10-31 08:47:01 +01002757 if (!stats_putchk(rep, htx, &trash))
2758 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002759
Christopher Faulet6338a082019-09-09 15:50:54 +02002760 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2761 appctx->st2 = STAT_ST_FIN;
2762 return 1;
2763 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002764 appctx->st2 = STAT_ST_INFO;
2765 /* fall through */
2766
2767 case STAT_ST_INFO:
2768 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2769 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002770 if (!stats_putchk(rep, htx, &trash))
2771 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002772 }
2773
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002774 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002775 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2776 appctx->st2 = STAT_ST_LIST;
2777 /* fall through */
2778
2779 case STAT_ST_LIST:
2780 /* dump proxies */
2781 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002782 if (htx) {
2783 if (htx_almost_full(htx))
2784 goto full;
2785 }
2786 else {
2787 if (buffer_almost_full(&rep->buf))
2788 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002789 }
2790
2791 px = appctx->ctx.stats.px;
2792 /* skip the disabled proxies, global frontend and non-networked ones */
2793 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002794 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002795 return 0;
2796
2797 appctx->ctx.stats.px = px->next;
2798 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2799 }
2800 /* here, we just have reached the last proxy */
2801
2802 appctx->st2 = STAT_ST_END;
2803 /* fall through */
2804
2805 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002806 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2807 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2808 stats_dump_html_end();
2809 else
2810 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002811 if (!stats_putchk(rep, htx, &trash))
2812 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002813 }
2814
2815 appctx->st2 = STAT_ST_FIN;
2816 /* fall through */
2817
2818 case STAT_ST_FIN:
2819 return 1;
2820
2821 default:
2822 /* unknown state ! */
2823 appctx->st2 = STAT_ST_FIN;
2824 return -1;
2825 }
Christopher Fauletef779222018-10-31 08:47:01 +01002826
2827 full:
2828 si_rx_room_blk(si);
2829 return 0;
2830
William Lallemand74c24fb2016-11-21 17:18:36 +01002831}
2832
2833/* We reached the stats page through a POST request. The appctx is
2834 * expected to have already been allocated by the caller.
2835 * Parse the posted data and enable/disable servers if necessary.
2836 * Returns 1 if request was parsed or zero if it needs more data.
2837 */
2838static int stats_process_http_post(struct stream_interface *si)
2839{
2840 struct stream *s = si_strm(si);
2841 struct appctx *appctx = objt_appctx(si->end);
2842
2843 struct proxy *px = NULL;
2844 struct server *sv = NULL;
2845
2846 char key[LINESIZE];
2847 int action = ST_ADM_ACTION_NONE;
2848 int reprocess = 0;
2849
2850 int total_servers = 0;
2851 int altered_servers = 0;
2852
2853 char *first_param, *cur_param, *next_param, *end_params;
2854 char *st_cur_param = NULL;
2855 char *st_next_param = NULL;
2856
Christopher Fauleta3618372018-12-13 21:59:56 +01002857 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002858
Christopher Fauletb7f88902019-07-15 21:56:43 +02002859 struct htx *htx = htxbuf(&s->req.buf);
2860 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002861
Christopher Fauletb7f88902019-07-15 21:56:43 +02002862 /* we need more data */
2863 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2864 /* check if we can receive more */
2865 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2866 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2867 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002868 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002869 goto wait;
2870 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002871
Christopher Fauletb7f88902019-07-15 21:56:43 +02002872 /* The request was fully received. Copy data */
2873 blk = htx_get_head_blk(htx);
2874 while (blk) {
2875 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002876
Christopher Fauletb7f88902019-07-15 21:56:43 +02002877 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2878 break;
2879 if (type == HTX_BLK_DATA) {
2880 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002881
Christopher Fauletb7f88902019-07-15 21:56:43 +02002882 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002883 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2884 goto out;
2885 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002886 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002887 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002888 }
2889
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002890 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002891 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002892 cur_param = next_param = end_params;
2893 *end_params = '\0';
2894
2895 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2896
2897 /*
2898 * Parse the parameters in reverse order to only store the last value.
2899 * From the html form, the backend and the action are at the end.
2900 */
2901 while (cur_param > first_param) {
2902 char *value;
2903 int poffset, plen;
2904
2905 cur_param--;
2906
2907 if ((*cur_param == '&') || (cur_param == first_param)) {
2908 reprocess_servers:
2909 /* Parse the key */
2910 poffset = (cur_param != first_param ? 1 : 0);
2911 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2912 if ((plen > 0) && (plen <= sizeof(key))) {
2913 strncpy(key, cur_param + poffset, plen);
2914 key[plen - 1] = '\0';
2915 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002916 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002917 goto out;
2918 }
2919
2920 /* Parse the value */
2921 value = key;
2922 while (*value != '\0' && *value != '=') {
2923 value++;
2924 }
2925 if (*value == '=') {
2926 /* Ok, a value is found, we can mark the end of the key */
2927 *value++ = '\0';
2928 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002929 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002930 break;
2931
2932 /* Now we can check the key to see what to do */
2933 if (!px && (strcmp(key, "b") == 0)) {
2934 if ((px = proxy_be_by_name(value)) == NULL) {
2935 /* the backend name is unknown or ambiguous (duplicate names) */
2936 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2937 goto out;
2938 }
2939 }
2940 else if (!action && (strcmp(key, "action") == 0)) {
2941 if (strcmp(value, "ready") == 0) {
2942 action = ST_ADM_ACTION_READY;
2943 }
2944 else if (strcmp(value, "drain") == 0) {
2945 action = ST_ADM_ACTION_DRAIN;
2946 }
2947 else if (strcmp(value, "maint") == 0) {
2948 action = ST_ADM_ACTION_MAINT;
2949 }
2950 else if (strcmp(value, "shutdown") == 0) {
2951 action = ST_ADM_ACTION_SHUTDOWN;
2952 }
2953 else if (strcmp(value, "dhlth") == 0) {
2954 action = ST_ADM_ACTION_DHLTH;
2955 }
2956 else if (strcmp(value, "ehlth") == 0) {
2957 action = ST_ADM_ACTION_EHLTH;
2958 }
2959 else if (strcmp(value, "hrunn") == 0) {
2960 action = ST_ADM_ACTION_HRUNN;
2961 }
2962 else if (strcmp(value, "hnolb") == 0) {
2963 action = ST_ADM_ACTION_HNOLB;
2964 }
2965 else if (strcmp(value, "hdown") == 0) {
2966 action = ST_ADM_ACTION_HDOWN;
2967 }
2968 else if (strcmp(value, "dagent") == 0) {
2969 action = ST_ADM_ACTION_DAGENT;
2970 }
2971 else if (strcmp(value, "eagent") == 0) {
2972 action = ST_ADM_ACTION_EAGENT;
2973 }
2974 else if (strcmp(value, "arunn") == 0) {
2975 action = ST_ADM_ACTION_ARUNN;
2976 }
2977 else if (strcmp(value, "adown") == 0) {
2978 action = ST_ADM_ACTION_ADOWN;
2979 }
2980 /* else these are the old supported methods */
2981 else if (strcmp(value, "disable") == 0) {
2982 action = ST_ADM_ACTION_DISABLE;
2983 }
2984 else if (strcmp(value, "enable") == 0) {
2985 action = ST_ADM_ACTION_ENABLE;
2986 }
2987 else if (strcmp(value, "stop") == 0) {
2988 action = ST_ADM_ACTION_STOP;
2989 }
2990 else if (strcmp(value, "start") == 0) {
2991 action = ST_ADM_ACTION_START;
2992 }
2993 else {
2994 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2995 goto out;
2996 }
2997 }
2998 else if (strcmp(key, "s") == 0) {
2999 if (!(px && action)) {
3000 /*
3001 * Indicates that we'll need to reprocess the parameters
3002 * as soon as backend and action are known
3003 */
3004 if (!reprocess) {
3005 st_cur_param = cur_param;
3006 st_next_param = next_param;
3007 }
3008 reprocess = 1;
3009 }
3010 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003011 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003012 switch (action) {
3013 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003014 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003015 altered_servers++;
3016 total_servers++;
3017 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3018 }
3019 break;
3020 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003021 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003022 altered_servers++;
3023 total_servers++;
3024 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3025 }
3026 break;
3027 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003028 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003029 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3030 altered_servers++;
3031 total_servers++;
3032 }
3033 break;
3034 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003035 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003036 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3037 altered_servers++;
3038 total_servers++;
3039 }
3040 break;
3041 case ST_ADM_ACTION_DHLTH:
3042 if (sv->check.state & CHK_ST_CONFIGURED) {
3043 sv->check.state &= ~CHK_ST_ENABLED;
3044 altered_servers++;
3045 total_servers++;
3046 }
3047 break;
3048 case ST_ADM_ACTION_EHLTH:
3049 if (sv->check.state & CHK_ST_CONFIGURED) {
3050 sv->check.state |= CHK_ST_ENABLED;
3051 altered_servers++;
3052 total_servers++;
3053 }
3054 break;
3055 case ST_ADM_ACTION_HRUNN:
3056 if (!(sv->track)) {
3057 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003058 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003059 altered_servers++;
3060 total_servers++;
3061 }
3062 break;
3063 case ST_ADM_ACTION_HNOLB:
3064 if (!(sv->track)) {
3065 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003066 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003067 altered_servers++;
3068 total_servers++;
3069 }
3070 break;
3071 case ST_ADM_ACTION_HDOWN:
3072 if (!(sv->track)) {
3073 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003074 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003075 altered_servers++;
3076 total_servers++;
3077 }
3078 break;
3079 case ST_ADM_ACTION_DAGENT:
3080 if (sv->agent.state & CHK_ST_CONFIGURED) {
3081 sv->agent.state &= ~CHK_ST_ENABLED;
3082 altered_servers++;
3083 total_servers++;
3084 }
3085 break;
3086 case ST_ADM_ACTION_EAGENT:
3087 if (sv->agent.state & CHK_ST_CONFIGURED) {
3088 sv->agent.state |= CHK_ST_ENABLED;
3089 altered_servers++;
3090 total_servers++;
3091 }
3092 break;
3093 case ST_ADM_ACTION_ARUNN:
3094 if (sv->agent.state & CHK_ST_ENABLED) {
3095 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003096 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003097 altered_servers++;
3098 total_servers++;
3099 }
3100 break;
3101 case ST_ADM_ACTION_ADOWN:
3102 if (sv->agent.state & CHK_ST_ENABLED) {
3103 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003104 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003105 altered_servers++;
3106 total_servers++;
3107 }
3108 break;
3109 case ST_ADM_ACTION_READY:
3110 srv_adm_set_ready(sv);
3111 altered_servers++;
3112 total_servers++;
3113 break;
3114 case ST_ADM_ACTION_DRAIN:
3115 srv_adm_set_drain(sv);
3116 altered_servers++;
3117 total_servers++;
3118 break;
3119 case ST_ADM_ACTION_MAINT:
3120 srv_adm_set_maint(sv);
3121 altered_servers++;
3122 total_servers++;
3123 break;
3124 case ST_ADM_ACTION_SHUTDOWN:
3125 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003126 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003127 altered_servers++;
3128 total_servers++;
3129 }
3130 break;
3131 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003132 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003133 } else {
3134 /* the server name is unknown or ambiguous (duplicate names) */
3135 total_servers++;
3136 }
3137 }
3138 if (reprocess && px && action) {
3139 /* Now, we know the backend and the action chosen by the user.
3140 * We can safely restart from the first server parameter
3141 * to reprocess them
3142 */
3143 cur_param = st_cur_param;
3144 next_param = st_next_param;
3145 reprocess = 0;
3146 goto reprocess_servers;
3147 }
3148
3149 next_param = cur_param;
3150 }
3151 }
3152
3153 if (total_servers == 0) {
3154 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3155 }
3156 else if (altered_servers == 0) {
3157 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3158 }
3159 else if (altered_servers == total_servers) {
3160 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3161 }
3162 else {
3163 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3164 }
3165 out:
3166 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003167 wait:
3168 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3169 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003170}
3171
3172
Christopher Fauletb7f88902019-07-15 21:56:43 +02003173static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003174{
3175 struct stream *s = si_strm(si);
3176 struct uri_auth *uri = s->be->uri_auth;
3177 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003178 struct htx_sl *sl;
3179 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003180
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003181 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);
3182 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3183 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003184 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003185 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003186
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003187 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003188 goto full;
3189 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3190 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3191 goto full;
3192 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003193 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3194 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3195 goto full;
3196 }
Christopher Fauletef779222018-10-31 08:47:01 +01003197 else {
3198 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3199 goto full;
3200 }
3201
3202 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3203 const char *refresh = U2A(uri->refresh);
3204 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3205 goto full;
3206 }
3207
3208 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3209 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3210 goto full;
3211 }
3212
3213 if (!htx_add_endof(htx, HTX_BLK_EOH))
3214 goto full;
3215
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003216 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003217 return 1;
3218
3219 full:
3220 htx_reset(htx);
3221 si_rx_room_blk(si);
3222 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003223}
3224
Christopher Fauletef779222018-10-31 08:47:01 +01003225
Christopher Fauletb7f88902019-07-15 21:56:43 +02003226static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003227{
3228 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3229 struct stream *s = si_strm(si);
3230 struct uri_auth *uri = s->be->uri_auth;
3231 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003232 struct htx_sl *sl;
3233 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003234
3235 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3236 scope_txt[0] = 0;
3237 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003238 const char *scope_ptr = stats_scope_ptr(appctx, si);
3239
Christopher Fauletef779222018-10-31 08:47:01 +01003240 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003241 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003242 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3243 }
3244
3245 /* We don't want to land on the posted stats page because a refresh will
3246 * repost the data. We don't want this to happen on accident so we redirect
3247 * the browse to the stats page with a GET.
3248 */
3249 chunk_printf(&trash, "%s;st=%s%s%s%s",
3250 uri->uri_prefix,
3251 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3252 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3253 stat_status_codes[appctx->ctx.stats.st_code]) ?
3254 stat_status_codes[appctx->ctx.stats.st_code] :
3255 stat_status_codes[STAT_STATUS_UNKN],
3256 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3257 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3258 scope_txt);
3259
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003260 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3261 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3262 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003263 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003264 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003265
3266 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003267 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3268 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3269 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3270 goto full;
3271
3272 if (!htx_add_endof(htx, HTX_BLK_EOH))
3273 goto full;
3274
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003275 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003276 return 1;
3277
3278full:
3279 htx_reset(htx);
3280 si_rx_room_blk(si);
3281 return 0;
3282}
William Lallemand74c24fb2016-11-21 17:18:36 +01003283
Simon Horman05ee2132017-01-04 09:37:25 +01003284
William Lallemand74c24fb2016-11-21 17:18:36 +01003285/* This I/O handler runs as an applet embedded in a stream interface. It is
3286 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3287 * appctx->st0 contains the operation in progress (dump, done). The handler
3288 * automatically unregisters itself once transfer is complete.
3289 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003290static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003291{
3292 struct stream_interface *si = appctx->owner;
3293 struct stream *s = si_strm(si);
3294 struct channel *req = si_oc(si);
3295 struct channel *res = si_ic(si);
3296 struct htx *req_htx, *res_htx;
3297
3298 res_htx = htx_from_buf(&res->buf);
3299
3300 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3301 goto out;
3302
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003303 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003304 if (!b_size(&res->buf)) {
3305 si_rx_room_blk(si);
3306 goto out;
3307 }
3308
3309 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003310 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3311 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003312
3313 /* all states are processed in sequence */
3314 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003315 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003316 if (s->txn->meth == HTTP_METH_HEAD)
3317 appctx->st0 = STAT_HTTP_DONE;
3318 else
3319 appctx->st0 = STAT_HTTP_DUMP;
3320 }
3321 }
3322
3323 if (appctx->st0 == STAT_HTTP_DUMP) {
3324 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3325 appctx->st0 = STAT_HTTP_DONE;
3326 }
3327
3328 if (appctx->st0 == STAT_HTTP_POST) {
3329 if (stats_process_http_post(si))
3330 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003331 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003332 appctx->st0 = STAT_HTTP_DONE;
3333 }
3334
3335 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003336 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003337 appctx->st0 = STAT_HTTP_DONE;
3338 }
3339
3340 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003341 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003342 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003343 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3344 si_rx_room_blk(si);
3345 goto out;
3346 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003347 channel_add_input(&s->res, 1);
3348 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003349 }
3350
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003351 if (appctx->st0 == STAT_HTTP_END) {
3352 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003353 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003354 si_shutr(si);
3355 }
3356
3357 /* eat the whole request */
3358 if (co_data(req)) {
3359 req_htx = htx_from_buf(&req->buf);
3360 co_htx_skip(req, req_htx, co_data(req));
3361 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003362 }
3363 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003364
Christopher Fauletef779222018-10-31 08:47:01 +01003365 out:
3366 /* we have left the request in the buffer for the case where we
3367 * process a POST, and this automatically re-enables activity on
3368 * read. It's better to indicate that we want to stop reading when
3369 * we're sending, so that we know there's at most one direction
3370 * deciding to wake the applet up. It saves it from looping when
3371 * emitting large blocks into small TCP windows.
3372 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003373 htx_to_buf(res_htx, &res->buf);
3374 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003375 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003376}
3377
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003378/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003379static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003380 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003381{
3382 int field;
3383
3384 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3385 if (!field_format(info, field))
3386 continue;
3387
Willy Tarreaueaa55372019-10-09 07:39:11 +02003388 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003389 return 0;
3390 if (!stats_emit_raw_data_field(out, &info[field]))
3391 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003392 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3393 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003394 if (!chunk_strcat(out, "\n"))
3395 return 0;
3396 }
3397 return 1;
3398}
3399
3400/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003401static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003402 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003403{
3404 int field;
3405
3406 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3407 if (!field_format(info, field))
3408 continue;
3409
Willy Tarreaueaa55372019-10-09 07:39:11 +02003410 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 +01003411 return 0;
3412 if (!stats_emit_field_tags(out, &info[field], ':'))
3413 return 0;
3414 if (!stats_emit_typed_data_field(out, &info[field]))
3415 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003416 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3417 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003418 if (!chunk_strcat(out, "\n"))
3419 return 0;
3420 }
3421 return 1;
3422}
3423
3424/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003425 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003426 * INF_TOTAL_FIELDS. If this length is less then this value, the
3427 * function returns 0, otherwise, it returns 1.
3428 */
3429int stats_fill_info(struct field *info, int len)
3430{
3431 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003432 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003433
3434#ifdef USE_OPENSSL
3435 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3436 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3437 int ssl_reuse = 0;
3438
3439 if (ssl_key_rate < ssl_sess_rate) {
3440 /* count the ssl reuse ratio and avoid overflows in both directions */
3441 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3442 }
3443#endif
3444
3445 if (len < INF_TOTAL_FIELDS)
3446 return 0;
3447
3448 chunk_reset(out);
3449 memset(info, 0, sizeof(*info) * len);
3450
3451 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003452 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3453 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003454
Yves Lafon95317282018-02-26 11:10:37 +01003455 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003456 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3457 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3458 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3459
3460 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3461 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3462
3463 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3464 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3465 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3466 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3467 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3468 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3469 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3470 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3471 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3472 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3473 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3474 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3475#ifdef USE_OPENSSL
3476 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3477 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3478 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3479#endif
3480 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3481 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3482 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3483 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3484 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3485 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3486 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3487 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3488 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3489
3490#ifdef USE_OPENSSL
3491 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3492 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3493 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3494 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3495 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3496 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3497 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3498 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3499 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3500 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3501#endif
3502 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3503 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3504 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3505#ifdef USE_ZLIB
3506 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3507 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3508#endif
3509 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003510 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003511 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003512 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3513 if (global.desc)
3514 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003515 info[INF_STOPPING] = mkf_u32(0, stopping);
3516 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003517 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003518 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003519 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003520 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003521 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003522 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003523 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003524 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003525 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003526 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 +02003527 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003528 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003529
3530 return 1;
3531}
3532
3533/* This function dumps information onto the stream interface's read buffer.
3534 * It returns 0 as long as it does not complete, non-zero upon completion.
3535 * No state is used.
3536 */
3537static int stats_dump_info_to_buffer(struct stream_interface *si)
3538{
3539 struct appctx *appctx = __objt_appctx(si->end);
3540
3541 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3542 return 0;
3543
3544 chunk_reset(&trash);
3545
3546 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003547 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003548 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003549 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003550 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003551 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003552
Willy Tarreau06d80a92017-10-19 14:32:15 +02003553 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003554 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003555 return 0;
3556 }
3557
3558 return 1;
3559}
3560
Simon Horman6f6bb382017-01-04 09:37:26 +01003561/* This function dumps the schema onto the stream interface's read buffer.
3562 * It returns 0 as long as it does not complete, non-zero upon completion.
3563 * No state is used.
3564 *
3565 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3566 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3567 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003568static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003569{
3570
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003571 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003572
3573 chunk_strcat(out,
3574 "{"
3575 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3576 "\"oneOf\":["
3577 "{"
3578 "\"title\":\"Info\","
3579 "\"type\":\"array\","
3580 "\"items\":{"
3581 "\"properties\":{"
3582 "\"title\":\"InfoItem\","
3583 "\"type\":\"object\","
3584 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3585 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3586 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3587 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3588 "},"
3589 "\"required\":[\"field\",\"processNum\",\"tags\","
3590 "\"value\"]"
3591 "}"
3592 "},"
3593 "{"
3594 "\"title\":\"Stat\","
3595 "\"type\":\"array\","
3596 "\"items\":{"
3597 "\"title\":\"InfoItem\","
3598 "\"type\":\"object\","
3599 "\"properties\":{"
3600 "\"objType\":{"
3601 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3602 "\"Server\",\"Unknown\"]"
3603 "},"
3604 "\"proxyId\":{"
3605 "\"type\":\"integer\","
3606 "\"minimum\":0"
3607 "},"
3608 "\"id\":{"
3609 "\"type\":\"integer\","
3610 "\"minimum\":0"
3611 "},"
3612 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3613 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3614 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3615 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3616 "},"
3617 "\"required\":[\"objType\",\"proxyId\",\"id\","
3618 "\"field\",\"processNum\",\"tags\","
3619 "\"value\"]"
3620 "}"
3621 "},"
3622 "{"
3623 "\"title\":\"Error\","
3624 "\"type\":\"object\","
3625 "\"properties\":{"
3626 "\"errorStr\":{"
3627 "\"type\":\"string\""
3628 "},"
3629 "\"required\":[\"errorStr\"]"
3630 "}"
3631 "}"
3632 "],"
3633 "\"definitions\":{"
3634 "\"field\":{"
3635 "\"type\":\"object\","
3636 "\"pos\":{"
3637 "\"type\":\"integer\","
3638 "\"minimum\":0"
3639 "},"
3640 "\"name\":{"
3641 "\"type\":\"string\""
3642 "},"
3643 "\"required\":[\"pos\",\"name\"]"
3644 "},"
3645 "\"processNum\":{"
3646 "\"type\":\"integer\","
3647 "\"minimum\":1"
3648 "},"
3649 "\"tags\":{"
3650 "\"type\":\"object\","
3651 "\"origin\":{"
3652 "\"type\":\"string\","
3653 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3654 "\"Config\",\"Product\",\"Unknown\"]"
3655 "},"
3656 "\"nature\":{"
3657 "\"type\":\"string\","
3658 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3659 "\"Rate\",\"Counter\",\"Duration\","
3660 "\"Age\",\"Time\",\"Name\",\"Output\","
3661 "\"Avg\", \"Unknown\"]"
3662 "},"
3663 "\"scope\":{"
3664 "\"type\":\"string\","
3665 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3666 "\"System\",\"Unknown\"]"
3667 "},"
3668 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3669 "},"
3670 "\"typedValue\":{"
3671 "\"type\":\"object\","
3672 "\"oneOf\":["
3673 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3674 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3675 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3676 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3677 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3678 "],"
3679 "\"definitions\":{"
3680 "\"s32Value\":{"
3681 "\"properties\":{"
3682 "\"type\":{"
3683 "\"type\":\"string\","
3684 "\"enum\":[\"s32\"]"
3685 "},"
3686 "\"value\":{"
3687 "\"type\":\"integer\","
3688 "\"minimum\":-2147483648,"
3689 "\"maximum\":2147483647"
3690 "}"
3691 "},"
3692 "\"required\":[\"type\",\"value\"]"
3693 "},"
3694 "\"s64Value\":{"
3695 "\"properties\":{"
3696 "\"type\":{"
3697 "\"type\":\"string\","
3698 "\"enum\":[\"s64\"]"
3699 "},"
3700 "\"value\":{"
3701 "\"type\":\"integer\","
3702 "\"minimum\":-9007199254740991,"
3703 "\"maximum\":9007199254740991"
3704 "}"
3705 "},"
3706 "\"required\":[\"type\",\"value\"]"
3707 "},"
3708 "\"u32Value\":{"
3709 "\"properties\":{"
3710 "\"type\":{"
3711 "\"type\":\"string\","
3712 "\"enum\":[\"u32\"]"
3713 "},"
3714 "\"value\":{"
3715 "\"type\":\"integer\","
3716 "\"minimum\":0,"
3717 "\"maximum\":4294967295"
3718 "}"
3719 "},"
3720 "\"required\":[\"type\",\"value\"]"
3721 "},"
3722 "\"u64Value\":{"
3723 "\"properties\":{"
3724 "\"type\":{"
3725 "\"type\":\"string\","
3726 "\"enum\":[\"u64\"]"
3727 "},"
3728 "\"value\":{"
3729 "\"type\":\"integer\","
3730 "\"minimum\":0,"
3731 "\"maximum\":9007199254740991"
3732 "}"
3733 "},"
3734 "\"required\":[\"type\",\"value\"]"
3735 "},"
3736 "\"strValue\":{"
3737 "\"properties\":{"
3738 "\"type\":{"
3739 "\"type\":\"string\","
3740 "\"enum\":[\"str\"]"
3741 "},"
3742 "\"value\":{\"type\":\"string\"}"
3743 "},"
3744 "\"required\":[\"type\",\"value\"]"
3745 "},"
3746 "\"unknownValue\":{"
3747 "\"properties\":{"
3748 "\"type\":{"
3749 "\"type\":\"integer\","
3750 "\"minimum\":0"
3751 "},"
3752 "\"value\":{"
3753 "\"type\":\"string\","
3754 "\"enum\":[\"unknown\"]"
3755 "}"
3756 "},"
3757 "\"required\":[\"type\",\"value\"]"
3758 "}"
3759 "}"
3760 "}"
3761 "}"
3762 "}");
3763
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003764 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003765 chunk_reset(out);
3766 chunk_appendf(out,
3767 "{\"errorStr\":\"output buffer too short\"}");
3768 }
3769}
3770
3771/* This function dumps the schema onto the stream interface's read buffer.
3772 * It returns 0 as long as it does not complete, non-zero upon completion.
3773 * No state is used.
3774 */
3775static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3776{
3777 chunk_reset(&trash);
3778
3779 stats_dump_json_schema(&trash);
3780
Willy Tarreau06d80a92017-10-19 14:32:15 +02003781 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003782 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003783 return 0;
3784 }
3785
3786 return 1;
3787}
3788
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003789static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003790{
3791 struct proxy *px;
3792 struct server *sv;
3793 struct listener *li;
3794 int clrall = 0;
3795
3796 if (strcmp(args[2], "all") == 0)
3797 clrall = 1;
3798
3799 /* check permissions */
3800 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3801 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3802 return 1;
3803
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003804 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003805 if (clrall) {
3806 memset(&px->be_counters, 0, sizeof(px->be_counters));
3807 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3808 }
3809 else {
3810 px->be_counters.conn_max = 0;
3811 px->be_counters.p.http.rps_max = 0;
3812 px->be_counters.sps_max = 0;
3813 px->be_counters.cps_max = 0;
3814 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003815 px->be_counters.qtime_max = 0;
3816 px->be_counters.ctime_max = 0;
3817 px->be_counters.dtime_max = 0;
3818 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003819
3820 px->fe_counters.conn_max = 0;
3821 px->fe_counters.p.http.rps_max = 0;
3822 px->fe_counters.sps_max = 0;
3823 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003824 }
3825
3826 for (sv = px->srv; sv; sv = sv->next)
3827 if (clrall)
3828 memset(&sv->counters, 0, sizeof(sv->counters));
3829 else {
3830 sv->counters.cur_sess_max = 0;
3831 sv->counters.nbpend_max = 0;
3832 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003833 sv->counters.qtime_max = 0;
3834 sv->counters.ctime_max = 0;
3835 sv->counters.dtime_max = 0;
3836 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003837 }
3838
3839 list_for_each_entry(li, &px->conf.listeners, by_fe)
3840 if (li->counters) {
3841 if (clrall)
3842 memset(li->counters, 0, sizeof(*li->counters));
3843 else
3844 li->counters->conn_max = 0;
3845 }
3846 }
3847
3848 global.cps_max = 0;
3849 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003850 global.ssl_max = 0;
3851 global.ssl_fe_keys_max = 0;
3852 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003853
3854 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003855 return 1;
3856}
3857
3858
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003859static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003860{
Willy Tarreau2f397382019-10-09 11:43:59 +02003861 int arg = 2;
3862
Willy Tarreaud25fc792016-12-16 12:33:47 +01003863 appctx->ctx.stats.scope_str = 0;
3864 appctx->ctx.stats.scope_len = 0;
3865 appctx->ctx.stats.flags = 0;
3866
Willy Tarreau2f397382019-10-09 11:43:59 +02003867 while (*args[arg]) {
3868 if (strcmp(args[arg], "typed") == 0)
3869 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3870 else if (strcmp(args[arg], "json") == 0)
3871 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3872 else if (strcmp(args[arg], "desc") == 0)
3873 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3874 arg++;
3875 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003876 return 0;
3877}
3878
3879
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003880static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003881{
Willy Tarreau2f397382019-10-09 11:43:59 +02003882 int arg = 2;
3883
Willy Tarreaud25fc792016-12-16 12:33:47 +01003884 appctx->ctx.stats.scope_str = 0;
3885 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003886 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3887
3888 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3889 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003890
Willy Tarreau2f397382019-10-09 11:43:59 +02003891 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003892 struct proxy *px;
3893
Willy Tarreau2f397382019-10-09 11:43:59 +02003894 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003895 if (px)
3896 appctx->ctx.stats.iid = px->uuid;
3897 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003898 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003899
Willy Tarreau9d008692019-08-09 11:21:01 +02003900 if (!appctx->ctx.stats.iid)
3901 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003902
Willy Tarreau2b812e22016-11-22 16:18:05 +01003903 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003904 appctx->ctx.stats.type = atoi(args[arg+1]);
3905 appctx->ctx.stats.sid = atoi(args[arg+2]);
3906 arg += 3;
3907 }
3908
3909 while (*args[arg]) {
3910 if (strcmp(args[arg], "typed") == 0)
3911 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3912 else if (strcmp(args[arg], "json") == 0)
3913 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3914 else if (strcmp(args[arg], "desc") == 0)
3915 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3916 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003917 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003918
Willy Tarreau2b812e22016-11-22 16:18:05 +01003919 return 0;
3920}
3921
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003922static int cli_io_handler_dump_info(struct appctx *appctx)
3923{
3924 return stats_dump_info_to_buffer(appctx->owner);
3925}
3926
Willy Tarreau2b812e22016-11-22 16:18:05 +01003927/* This I/O handler runs as an applet embedded in a stream interface. It is
3928 * used to send raw stats over a socket.
3929 */
3930static int cli_io_handler_dump_stat(struct appctx *appctx)
3931{
Christopher Fauletef779222018-10-31 08:47:01 +01003932 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003933}
3934
Simon Horman6f6bb382017-01-04 09:37:26 +01003935static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3936{
3937 return stats_dump_json_schema_to_buffer(appctx->owner);
3938}
3939
Willy Tarreau2b812e22016-11-22 16:18:05 +01003940/* register cli keywords */
3941static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003942 { { "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 +02003943 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3944 { { "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 +01003945 { { "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 +01003946 {{},}
3947}};
3948
Willy Tarreau0108d902018-11-25 19:14:37 +01003949INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3950
William Lallemand74c24fb2016-11-21 17:18:36 +01003951struct applet http_stats_applet = {
3952 .obj_type = OBJ_TYPE_APPLET,
3953 .name = "<STATS>", /* used for logging */
3954 .fct = http_stats_io_handler,
3955 .release = NULL,
3956};
3957
William Lallemand74c24fb2016-11-21 17:18:36 +01003958/*
3959 * Local variables:
3960 * c-indent-level: 8
3961 * c-basic-offset: 8
3962 * End:
3963 */