blob: 36aee99f9a864b2ed894027123d0717a0b2b5fbd [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
Willy Tarreau7b524852020-08-11 10:26:36 +0200938 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +0100939 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
Willy Tarreau7b524852020-08-11 10:26:36 +02001110 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001111 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1112 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001113 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001114 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));
Willy Tarreau7b524852020-08-11 10:26:36 +02001118 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001119 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
Willy Tarreau7b524852020-08-11 10:26:36 +02001126 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001127 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"
zurikus6d599932020-08-18 11:16:05 +03002338 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002339 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2340 "<style type=\"text/css\"><!--\n"
2341 "body {"
2342 " font-family: arial, helvetica, sans-serif;"
2343 " font-size: 12px;"
2344 " font-weight: normal;"
2345 " color: black;"
2346 " background: white;"
2347 "}\n"
2348 "th,td {"
2349 " font-size: 10px;"
2350 "}\n"
2351 "h1 {"
2352 " font-size: x-large;"
2353 " margin-bottom: 0.5em;"
2354 "}\n"
2355 "h2 {"
2356 " font-family: helvetica, arial;"
2357 " font-size: x-large;"
2358 " font-weight: bold;"
2359 " font-style: italic;"
2360 " color: #6020a0;"
2361 " margin-top: 0em;"
2362 " margin-bottom: 0em;"
2363 "}\n"
2364 "h3 {"
2365 " font-family: helvetica, arial;"
2366 " font-size: 16px;"
2367 " font-weight: bold;"
2368 " color: #b00040;"
2369 " background: #e8e8d0;"
2370 " margin-top: 0em;"
2371 " margin-bottom: 0em;"
2372 "}\n"
2373 "li {"
2374 " margin-top: 0.25em;"
2375 " margin-right: 2em;"
2376 "}\n"
2377 ".hr {margin-top: 0.25em;"
2378 " border-color: black;"
2379 " border-bottom-style: solid;"
2380 "}\n"
2381 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2382 ".total {background: #20D0D0;color: #ffff80;}\n"
2383 ".frontend {background: #e8e8d0;}\n"
2384 ".socket {background: #d0d0d0;}\n"
2385 ".backend {background: #e8e8d0;}\n"
2386 ".active_down {background: #ff9090;}\n"
2387 ".active_going_up {background: #ffd020;}\n"
2388 ".active_going_down {background: #ffffa0;}\n"
2389 ".active_up {background: #c0ffc0;}\n"
2390 ".active_nolb {background: #20a0ff;}\n"
2391 ".active_draining {background: #20a0FF;}\n"
2392 ".active_no_check {background: #e0e0e0;}\n"
2393 ".backup_down {background: #ff9090;}\n"
2394 ".backup_going_up {background: #ff80ff;}\n"
2395 ".backup_going_down {background: #c060ff;}\n"
2396 ".backup_up {background: #b0d0ff;}\n"
2397 ".backup_nolb {background: #90b0e0;}\n"
2398 ".backup_draining {background: #cc9900;}\n"
2399 ".backup_no_check {background: #e0e0e0;}\n"
2400 ".maintain {background: #c07820;}\n"
2401 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2402 "\n"
2403 "a.px:link {color: #ffff40; text-decoration: none;}"
2404 "a.px:visited {color: #ffff40; text-decoration: none;}"
2405 "a.px:hover {color: #ffffff; text-decoration: none;}"
2406 "a.lfsb:link {color: #000000; text-decoration: none;}"
2407 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2408 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2409 "\n"
2410 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2411 "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"
2412 "table.tbl td.ac { text-align: center;}\n"
2413 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2414 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2415 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2416 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2417 "\n"
2418 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2419 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2420 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2421 "table.det { border-collapse: collapse; border-style: none; }\n"
2422 "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"
2423 "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"
2424 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2425 "div.tips {\n"
2426 " display:block;\n"
2427 " visibility:hidden;\n"
2428 " z-index:2147483647;\n"
2429 " position:absolute;\n"
2430 " padding:2px 4px 3px;\n"
2431 " background:#f0f060; color:#000000;\n"
2432 " border:1px solid #7040c0;\n"
2433 " white-space:nowrap;\n"
2434 " font-style:normal;font-size:11px;font-weight:normal;\n"
2435 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2436 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2437 "}\n"
2438 "u:hover div.tips {visibility:visible;}\n"
2439 "-->\n"
2440 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002441 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002442 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002443 );
2444}
2445
2446/* Dumps the HTML stats information block to the trash for and uses the state from
2447 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2448 * for clearing the trash if needed.
2449 */
2450static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2451{
2452 struct appctx *appctx = __objt_appctx(si->end);
2453 unsigned int up = (now.tv_sec - start_date.tv_sec);
2454 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002455 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002456 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2457
2458 /* Turn the bytes per second to bits per second and take care of the
2459 * usual ethernet overhead in order to help figure how far we are from
2460 * interface saturation since it's the only case which usually matters.
2461 * For this we count the total size of an Ethernet frame on the wire
2462 * including preamble and IFG (1538) for the largest TCP segment it
2463 * transports (1448 with TCP timestamps). This is not valid for smaller
2464 * packets (under-estimated), but it gives a reasonably accurate
2465 * estimation of how far we are from uplink saturation.
2466 */
2467 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002468
2469 /* WARNING! this has to fit the first packet too.
2470 * We are around 3.5 kB, add adding entries will
2471 * become tricky if we want to support 4kB buffers !
2472 */
2473 chunk_appendf(&trash,
2474 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2475 PRODUCT_NAME "%s</a></h1>\n"
2476 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2477 "<hr width=\"100%%\" class=\"hr\">\n"
2478 "<h3>&gt; General process information</h3>\n"
2479 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002480 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002481 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2482 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2483 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002484 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002485 "Running tasks: %d/%d; idle = %d %%<br>\n"
2486 "</td><td align=\"center\" nowrap>\n"
2487 "<table class=\"lgd\"><tr>\n"
2488 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2489 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2490 "</tr><tr>\n"
2491 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2492 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2493 "</tr><tr>\n"
2494 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2495 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2496 "</tr><tr>\n"
2497 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2498 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2499 "</tr><tr>\n"
2500 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2501 "</tr><tr>\n"
2502 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2503 "</tr></table>\n"
2504 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2505 "</td>"
2506 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2507 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2508 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002509 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2510 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2511 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2512 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2513 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002514 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002515 up / 86400, (up % 86400) / 3600,
2516 (up % 3600) / 60, (up % 60),
2517 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2518 global.rlimit_memmax ? " MB" : "",
2519 global.rlimit_nofile,
2520 global.maxsock, global.maxconn, global.maxpipes,
2521 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002522 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2523 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002524 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002525 );
2526
2527 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002528 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002529 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2530
2531 chunk_appendf(&trash,
2532 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2533 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2534 STAT_SCOPE_TXT_MAXLEN);
2535
2536 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2537 scope_txt[0] = 0;
2538 if (appctx->ctx.stats.scope_len) {
2539 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002540 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002541 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2542 }
2543
2544 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2545 chunk_appendf(&trash,
2546 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2547 uri->uri_prefix,
2548 "",
2549 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2550 scope_txt);
2551 else
2552 chunk_appendf(&trash,
2553 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2554 uri->uri_prefix,
2555 ";up",
2556 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2557 scope_txt);
2558
2559 if (uri->refresh > 0) {
2560 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2561 chunk_appendf(&trash,
2562 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2563 uri->uri_prefix,
2564 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2565 "",
2566 scope_txt);
2567 else
2568 chunk_appendf(&trash,
2569 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2570 uri->uri_prefix,
2571 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2572 ";norefresh",
2573 scope_txt);
2574 }
2575
2576 chunk_appendf(&trash,
2577 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2578 uri->uri_prefix,
2579 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2580 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2581 scope_txt);
2582
2583 chunk_appendf(&trash,
2584 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2585 uri->uri_prefix,
2586 (uri->refresh > 0) ? ";norefresh" : "",
2587 scope_txt);
2588
2589 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002590 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2591 uri->uri_prefix,
2592 (uri->refresh > 0) ? ";norefresh" : "",
2593 scope_txt, uri->uri_prefix);
2594
2595 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002596 "</ul></td>"
2597 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2598 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2599 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2600 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2601 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2602 "</ul>"
2603 "</td>"
2604 "</tr></table>\n"
2605 ""
2606 );
2607
2608 if (appctx->ctx.stats.st_code) {
2609 switch (appctx->ctx.stats.st_code) {
2610 case STAT_STATUS_DONE:
2611 chunk_appendf(&trash,
2612 "<p><div class=active_up>"
2613 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2614 "Action processed successfully."
2615 "</div>\n", uri->uri_prefix,
2616 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2617 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2618 scope_txt);
2619 break;
2620 case STAT_STATUS_NONE:
2621 chunk_appendf(&trash,
2622 "<p><div class=active_going_down>"
2623 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2624 "Nothing has changed."
2625 "</div>\n", uri->uri_prefix,
2626 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2627 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2628 scope_txt);
2629 break;
2630 case STAT_STATUS_PART:
2631 chunk_appendf(&trash,
2632 "<p><div class=active_going_down>"
2633 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2634 "Action partially processed.<br>"
2635 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2636 "</div>\n", uri->uri_prefix,
2637 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2638 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2639 scope_txt);
2640 break;
2641 case STAT_STATUS_ERRP:
2642 chunk_appendf(&trash,
2643 "<p><div class=active_down>"
2644 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2645 "Action not processed because of invalid parameters."
2646 "<ul>"
2647 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002648 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002649 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2650 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2651 "</ul>"
2652 "</div>\n", uri->uri_prefix,
2653 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2654 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2655 scope_txt);
2656 break;
2657 case STAT_STATUS_EXCD:
2658 chunk_appendf(&trash,
2659 "<p><div class=active_down>"
2660 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2661 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2662 "You should retry with less servers at a time.</b>"
2663 "</div>\n", uri->uri_prefix,
2664 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2665 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2666 scope_txt);
2667 break;
2668 case STAT_STATUS_DENY:
2669 chunk_appendf(&trash,
2670 "<p><div class=active_down>"
2671 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2672 "<b>Action denied.</b>"
2673 "</div>\n", uri->uri_prefix,
2674 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2675 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2676 scope_txt);
2677 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002678 case STAT_STATUS_IVAL:
2679 chunk_appendf(&trash,
2680 "<p><div class=active_down>"
2681 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2682 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2683 "</div>\n", uri->uri_prefix,
2684 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2685 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2686 scope_txt);
2687 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002688 default:
2689 chunk_appendf(&trash,
2690 "<p><div class=active_no_check>"
2691 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2692 "Unexpected result."
2693 "</div>\n", uri->uri_prefix,
2694 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2695 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2696 scope_txt);
2697 }
2698 chunk_appendf(&trash, "<p>\n");
2699 }
2700}
2701
2702/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2703 * for clearing the trash if needed.
2704 */
2705static void stats_dump_html_end()
2706{
2707 chunk_appendf(&trash, "</body></html>\n");
2708}
2709
Simon Horman05ee2132017-01-04 09:37:25 +01002710/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2711 * for clearing it if needed.
2712 */
2713static void stats_dump_json_header()
2714{
2715 chunk_strcat(&trash, "[");
2716}
2717
2718
2719/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2720 * for clearing the trash if needed.
2721 */
2722static void stats_dump_json_end()
2723{
2724 chunk_strcat(&trash, "]");
2725}
2726
William Lallemand74c24fb2016-11-21 17:18:36 +01002727/* This function dumps statistics onto the stream interface's read buffer in
2728 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2729 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2730 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2731 * and the stream must be closed, or -1 in case of any error. This function is
2732 * used by both the CLI and the HTTP handlers.
2733 */
Christopher Fauletef779222018-10-31 08:47:01 +01002734static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2735 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002736{
2737 struct appctx *appctx = __objt_appctx(si->end);
2738 struct channel *rep = si_ic(si);
2739 struct proxy *px;
2740
2741 chunk_reset(&trash);
2742
2743 switch (appctx->st2) {
2744 case STAT_ST_INIT:
2745 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2746 /* fall through */
2747
2748 case STAT_ST_HEAD:
2749 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002750 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002751 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2752 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002753 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002754 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002755 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2756 stats_dump_csv_header();
2757
Christopher Fauletef779222018-10-31 08:47:01 +01002758 if (!stats_putchk(rep, htx, &trash))
2759 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002760
Christopher Faulet6338a082019-09-09 15:50:54 +02002761 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2762 appctx->st2 = STAT_ST_FIN;
2763 return 1;
2764 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002765 appctx->st2 = STAT_ST_INFO;
2766 /* fall through */
2767
2768 case STAT_ST_INFO:
2769 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2770 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002771 if (!stats_putchk(rep, htx, &trash))
2772 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002773 }
2774
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002775 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002776 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2777 appctx->st2 = STAT_ST_LIST;
2778 /* fall through */
2779
2780 case STAT_ST_LIST:
2781 /* dump proxies */
2782 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002783 if (htx) {
2784 if (htx_almost_full(htx))
2785 goto full;
2786 }
2787 else {
2788 if (buffer_almost_full(&rep->buf))
2789 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002790 }
2791
2792 px = appctx->ctx.stats.px;
2793 /* skip the disabled proxies, global frontend and non-networked ones */
2794 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002795 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002796 return 0;
2797
2798 appctx->ctx.stats.px = px->next;
2799 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2800 }
2801 /* here, we just have reached the last proxy */
2802
2803 appctx->st2 = STAT_ST_END;
2804 /* fall through */
2805
2806 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002807 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2808 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2809 stats_dump_html_end();
2810 else
2811 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002812 if (!stats_putchk(rep, htx, &trash))
2813 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002814 }
2815
2816 appctx->st2 = STAT_ST_FIN;
2817 /* fall through */
2818
2819 case STAT_ST_FIN:
2820 return 1;
2821
2822 default:
2823 /* unknown state ! */
2824 appctx->st2 = STAT_ST_FIN;
2825 return -1;
2826 }
Christopher Fauletef779222018-10-31 08:47:01 +01002827
2828 full:
2829 si_rx_room_blk(si);
2830 return 0;
2831
William Lallemand74c24fb2016-11-21 17:18:36 +01002832}
2833
2834/* We reached the stats page through a POST request. The appctx is
2835 * expected to have already been allocated by the caller.
2836 * Parse the posted data and enable/disable servers if necessary.
2837 * Returns 1 if request was parsed or zero if it needs more data.
2838 */
2839static int stats_process_http_post(struct stream_interface *si)
2840{
2841 struct stream *s = si_strm(si);
2842 struct appctx *appctx = objt_appctx(si->end);
2843
2844 struct proxy *px = NULL;
2845 struct server *sv = NULL;
2846
2847 char key[LINESIZE];
2848 int action = ST_ADM_ACTION_NONE;
2849 int reprocess = 0;
2850
2851 int total_servers = 0;
2852 int altered_servers = 0;
2853
2854 char *first_param, *cur_param, *next_param, *end_params;
2855 char *st_cur_param = NULL;
2856 char *st_next_param = NULL;
2857
Christopher Fauleta3618372018-12-13 21:59:56 +01002858 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002859
Christopher Fauletb7f88902019-07-15 21:56:43 +02002860 struct htx *htx = htxbuf(&s->req.buf);
2861 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002862
Christopher Fauletb7f88902019-07-15 21:56:43 +02002863 /* we need more data */
2864 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2865 /* check if we can receive more */
2866 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2867 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2868 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002869 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002870 goto wait;
2871 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002872
Christopher Fauletb7f88902019-07-15 21:56:43 +02002873 /* The request was fully received. Copy data */
2874 blk = htx_get_head_blk(htx);
2875 while (blk) {
2876 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002877
Christopher Fauletb7f88902019-07-15 21:56:43 +02002878 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2879 break;
2880 if (type == HTX_BLK_DATA) {
2881 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002882
Christopher Fauletb7f88902019-07-15 21:56:43 +02002883 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002884 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2885 goto out;
2886 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002887 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002888 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002889 }
2890
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002891 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002892 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002893 cur_param = next_param = end_params;
2894 *end_params = '\0';
2895
2896 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2897
2898 /*
2899 * Parse the parameters in reverse order to only store the last value.
2900 * From the html form, the backend and the action are at the end.
2901 */
2902 while (cur_param > first_param) {
2903 char *value;
2904 int poffset, plen;
2905
2906 cur_param--;
2907
2908 if ((*cur_param == '&') || (cur_param == first_param)) {
2909 reprocess_servers:
2910 /* Parse the key */
2911 poffset = (cur_param != first_param ? 1 : 0);
2912 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2913 if ((plen > 0) && (plen <= sizeof(key))) {
2914 strncpy(key, cur_param + poffset, plen);
2915 key[plen - 1] = '\0';
2916 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002917 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002918 goto out;
2919 }
2920
2921 /* Parse the value */
2922 value = key;
2923 while (*value != '\0' && *value != '=') {
2924 value++;
2925 }
2926 if (*value == '=') {
2927 /* Ok, a value is found, we can mark the end of the key */
2928 *value++ = '\0';
2929 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002930 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002931 break;
2932
2933 /* Now we can check the key to see what to do */
2934 if (!px && (strcmp(key, "b") == 0)) {
2935 if ((px = proxy_be_by_name(value)) == NULL) {
2936 /* the backend name is unknown or ambiguous (duplicate names) */
2937 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2938 goto out;
2939 }
2940 }
2941 else if (!action && (strcmp(key, "action") == 0)) {
2942 if (strcmp(value, "ready") == 0) {
2943 action = ST_ADM_ACTION_READY;
2944 }
2945 else if (strcmp(value, "drain") == 0) {
2946 action = ST_ADM_ACTION_DRAIN;
2947 }
2948 else if (strcmp(value, "maint") == 0) {
2949 action = ST_ADM_ACTION_MAINT;
2950 }
2951 else if (strcmp(value, "shutdown") == 0) {
2952 action = ST_ADM_ACTION_SHUTDOWN;
2953 }
2954 else if (strcmp(value, "dhlth") == 0) {
2955 action = ST_ADM_ACTION_DHLTH;
2956 }
2957 else if (strcmp(value, "ehlth") == 0) {
2958 action = ST_ADM_ACTION_EHLTH;
2959 }
2960 else if (strcmp(value, "hrunn") == 0) {
2961 action = ST_ADM_ACTION_HRUNN;
2962 }
2963 else if (strcmp(value, "hnolb") == 0) {
2964 action = ST_ADM_ACTION_HNOLB;
2965 }
2966 else if (strcmp(value, "hdown") == 0) {
2967 action = ST_ADM_ACTION_HDOWN;
2968 }
2969 else if (strcmp(value, "dagent") == 0) {
2970 action = ST_ADM_ACTION_DAGENT;
2971 }
2972 else if (strcmp(value, "eagent") == 0) {
2973 action = ST_ADM_ACTION_EAGENT;
2974 }
2975 else if (strcmp(value, "arunn") == 0) {
2976 action = ST_ADM_ACTION_ARUNN;
2977 }
2978 else if (strcmp(value, "adown") == 0) {
2979 action = ST_ADM_ACTION_ADOWN;
2980 }
2981 /* else these are the old supported methods */
2982 else if (strcmp(value, "disable") == 0) {
2983 action = ST_ADM_ACTION_DISABLE;
2984 }
2985 else if (strcmp(value, "enable") == 0) {
2986 action = ST_ADM_ACTION_ENABLE;
2987 }
2988 else if (strcmp(value, "stop") == 0) {
2989 action = ST_ADM_ACTION_STOP;
2990 }
2991 else if (strcmp(value, "start") == 0) {
2992 action = ST_ADM_ACTION_START;
2993 }
2994 else {
2995 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2996 goto out;
2997 }
2998 }
2999 else if (strcmp(key, "s") == 0) {
3000 if (!(px && action)) {
3001 /*
3002 * Indicates that we'll need to reprocess the parameters
3003 * as soon as backend and action are known
3004 */
3005 if (!reprocess) {
3006 st_cur_param = cur_param;
3007 st_next_param = next_param;
3008 }
3009 reprocess = 1;
3010 }
3011 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003012 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003013 switch (action) {
3014 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003015 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003016 altered_servers++;
3017 total_servers++;
3018 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3019 }
3020 break;
3021 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003022 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003023 altered_servers++;
3024 total_servers++;
3025 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3026 }
3027 break;
3028 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003029 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003030 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3031 altered_servers++;
3032 total_servers++;
3033 }
3034 break;
3035 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003036 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003037 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3038 altered_servers++;
3039 total_servers++;
3040 }
3041 break;
3042 case ST_ADM_ACTION_DHLTH:
3043 if (sv->check.state & CHK_ST_CONFIGURED) {
3044 sv->check.state &= ~CHK_ST_ENABLED;
3045 altered_servers++;
3046 total_servers++;
3047 }
3048 break;
3049 case ST_ADM_ACTION_EHLTH:
3050 if (sv->check.state & CHK_ST_CONFIGURED) {
3051 sv->check.state |= CHK_ST_ENABLED;
3052 altered_servers++;
3053 total_servers++;
3054 }
3055 break;
3056 case ST_ADM_ACTION_HRUNN:
3057 if (!(sv->track)) {
3058 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003059 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003060 altered_servers++;
3061 total_servers++;
3062 }
3063 break;
3064 case ST_ADM_ACTION_HNOLB:
3065 if (!(sv->track)) {
3066 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003067 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003068 altered_servers++;
3069 total_servers++;
3070 }
3071 break;
3072 case ST_ADM_ACTION_HDOWN:
3073 if (!(sv->track)) {
3074 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003075 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003076 altered_servers++;
3077 total_servers++;
3078 }
3079 break;
3080 case ST_ADM_ACTION_DAGENT:
3081 if (sv->agent.state & CHK_ST_CONFIGURED) {
3082 sv->agent.state &= ~CHK_ST_ENABLED;
3083 altered_servers++;
3084 total_servers++;
3085 }
3086 break;
3087 case ST_ADM_ACTION_EAGENT:
3088 if (sv->agent.state & CHK_ST_CONFIGURED) {
3089 sv->agent.state |= CHK_ST_ENABLED;
3090 altered_servers++;
3091 total_servers++;
3092 }
3093 break;
3094 case ST_ADM_ACTION_ARUNN:
3095 if (sv->agent.state & CHK_ST_ENABLED) {
3096 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003097 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003098 altered_servers++;
3099 total_servers++;
3100 }
3101 break;
3102 case ST_ADM_ACTION_ADOWN:
3103 if (sv->agent.state & CHK_ST_ENABLED) {
3104 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003105 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003106 altered_servers++;
3107 total_servers++;
3108 }
3109 break;
3110 case ST_ADM_ACTION_READY:
3111 srv_adm_set_ready(sv);
3112 altered_servers++;
3113 total_servers++;
3114 break;
3115 case ST_ADM_ACTION_DRAIN:
3116 srv_adm_set_drain(sv);
3117 altered_servers++;
3118 total_servers++;
3119 break;
3120 case ST_ADM_ACTION_MAINT:
3121 srv_adm_set_maint(sv);
3122 altered_servers++;
3123 total_servers++;
3124 break;
3125 case ST_ADM_ACTION_SHUTDOWN:
3126 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003127 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003128 altered_servers++;
3129 total_servers++;
3130 }
3131 break;
3132 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003133 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003134 } else {
3135 /* the server name is unknown or ambiguous (duplicate names) */
3136 total_servers++;
3137 }
3138 }
3139 if (reprocess && px && action) {
3140 /* Now, we know the backend and the action chosen by the user.
3141 * We can safely restart from the first server parameter
3142 * to reprocess them
3143 */
3144 cur_param = st_cur_param;
3145 next_param = st_next_param;
3146 reprocess = 0;
3147 goto reprocess_servers;
3148 }
3149
3150 next_param = cur_param;
3151 }
3152 }
3153
3154 if (total_servers == 0) {
3155 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3156 }
3157 else if (altered_servers == 0) {
3158 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3159 }
3160 else if (altered_servers == total_servers) {
3161 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3162 }
3163 else {
3164 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3165 }
3166 out:
3167 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003168 wait:
3169 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3170 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003171}
3172
3173
Christopher Fauletb7f88902019-07-15 21:56:43 +02003174static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003175{
3176 struct stream *s = si_strm(si);
3177 struct uri_auth *uri = s->be->uri_auth;
3178 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003179 struct htx_sl *sl;
3180 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003181
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003182 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);
3183 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3184 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003185 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003186 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003187
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003188 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003189 goto full;
3190 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3191 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3192 goto full;
3193 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003194 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3195 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3196 goto full;
3197 }
Christopher Fauletef779222018-10-31 08:47:01 +01003198 else {
3199 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3200 goto full;
3201 }
3202
3203 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3204 const char *refresh = U2A(uri->refresh);
3205 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3206 goto full;
3207 }
3208
3209 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3210 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3211 goto full;
3212 }
3213
3214 if (!htx_add_endof(htx, HTX_BLK_EOH))
3215 goto full;
3216
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003217 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003218 return 1;
3219
3220 full:
3221 htx_reset(htx);
3222 si_rx_room_blk(si);
3223 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003224}
3225
Christopher Fauletef779222018-10-31 08:47:01 +01003226
Christopher Fauletb7f88902019-07-15 21:56:43 +02003227static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003228{
3229 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3230 struct stream *s = si_strm(si);
3231 struct uri_auth *uri = s->be->uri_auth;
3232 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003233 struct htx_sl *sl;
3234 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003235
3236 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3237 scope_txt[0] = 0;
3238 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003239 const char *scope_ptr = stats_scope_ptr(appctx, si);
3240
Christopher Fauletef779222018-10-31 08:47:01 +01003241 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003242 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003243 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3244 }
3245
3246 /* We don't want to land on the posted stats page because a refresh will
3247 * repost the data. We don't want this to happen on accident so we redirect
3248 * the browse to the stats page with a GET.
3249 */
3250 chunk_printf(&trash, "%s;st=%s%s%s%s",
3251 uri->uri_prefix,
3252 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3253 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3254 stat_status_codes[appctx->ctx.stats.st_code]) ?
3255 stat_status_codes[appctx->ctx.stats.st_code] :
3256 stat_status_codes[STAT_STATUS_UNKN],
3257 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3258 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3259 scope_txt);
3260
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003261 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3262 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3263 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003264 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003265 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003266
3267 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003268 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3269 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3270 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3271 goto full;
3272
3273 if (!htx_add_endof(htx, HTX_BLK_EOH))
3274 goto full;
3275
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003276 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003277 return 1;
3278
3279full:
3280 htx_reset(htx);
3281 si_rx_room_blk(si);
3282 return 0;
3283}
William Lallemand74c24fb2016-11-21 17:18:36 +01003284
Simon Horman05ee2132017-01-04 09:37:25 +01003285
William Lallemand74c24fb2016-11-21 17:18:36 +01003286/* This I/O handler runs as an applet embedded in a stream interface. It is
3287 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3288 * appctx->st0 contains the operation in progress (dump, done). The handler
3289 * automatically unregisters itself once transfer is complete.
3290 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003291static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003292{
3293 struct stream_interface *si = appctx->owner;
3294 struct stream *s = si_strm(si);
3295 struct channel *req = si_oc(si);
3296 struct channel *res = si_ic(si);
3297 struct htx *req_htx, *res_htx;
3298
3299 res_htx = htx_from_buf(&res->buf);
3300
3301 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3302 goto out;
3303
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003304 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003305 if (!b_size(&res->buf)) {
3306 si_rx_room_blk(si);
3307 goto out;
3308 }
3309
3310 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003311 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3312 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003313
3314 /* all states are processed in sequence */
3315 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003316 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003317 if (s->txn->meth == HTTP_METH_HEAD)
3318 appctx->st0 = STAT_HTTP_DONE;
3319 else
3320 appctx->st0 = STAT_HTTP_DUMP;
3321 }
3322 }
3323
3324 if (appctx->st0 == STAT_HTTP_DUMP) {
3325 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3326 appctx->st0 = STAT_HTTP_DONE;
3327 }
3328
3329 if (appctx->st0 == STAT_HTTP_POST) {
3330 if (stats_process_http_post(si))
3331 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003332 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003333 appctx->st0 = STAT_HTTP_DONE;
3334 }
3335
3336 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003337 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003338 appctx->st0 = STAT_HTTP_DONE;
3339 }
3340
3341 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003342 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003343 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003344 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3345 si_rx_room_blk(si);
3346 goto out;
3347 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003348 channel_add_input(&s->res, 1);
3349 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003350 }
3351
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003352 if (appctx->st0 == STAT_HTTP_END) {
3353 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003354 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003355 si_shutr(si);
3356 }
3357
3358 /* eat the whole request */
3359 if (co_data(req)) {
3360 req_htx = htx_from_buf(&req->buf);
3361 co_htx_skip(req, req_htx, co_data(req));
3362 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003363 }
3364 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003365
Christopher Fauletef779222018-10-31 08:47:01 +01003366 out:
3367 /* we have left the request in the buffer for the case where we
3368 * process a POST, and this automatically re-enables activity on
3369 * read. It's better to indicate that we want to stop reading when
3370 * we're sending, so that we know there's at most one direction
3371 * deciding to wake the applet up. It saves it from looping when
3372 * emitting large blocks into small TCP windows.
3373 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003374 htx_to_buf(res_htx, &res->buf);
3375 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003376 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003377}
3378
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003379/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003380static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003381 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003382{
3383 int field;
3384
3385 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3386 if (!field_format(info, field))
3387 continue;
3388
Willy Tarreaueaa55372019-10-09 07:39:11 +02003389 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003390 return 0;
3391 if (!stats_emit_raw_data_field(out, &info[field]))
3392 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003393 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3394 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003395 if (!chunk_strcat(out, "\n"))
3396 return 0;
3397 }
3398 return 1;
3399}
3400
3401/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003402static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003403 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003404{
3405 int field;
3406
3407 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3408 if (!field_format(info, field))
3409 continue;
3410
Willy Tarreaueaa55372019-10-09 07:39:11 +02003411 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 +01003412 return 0;
3413 if (!stats_emit_field_tags(out, &info[field], ':'))
3414 return 0;
3415 if (!stats_emit_typed_data_field(out, &info[field]))
3416 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003417 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3418 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003419 if (!chunk_strcat(out, "\n"))
3420 return 0;
3421 }
3422 return 1;
3423}
3424
3425/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003426 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003427 * INF_TOTAL_FIELDS. If this length is less then this value, the
3428 * function returns 0, otherwise, it returns 1.
3429 */
3430int stats_fill_info(struct field *info, int len)
3431{
3432 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003433 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003434
3435#ifdef USE_OPENSSL
3436 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3437 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3438 int ssl_reuse = 0;
3439
3440 if (ssl_key_rate < ssl_sess_rate) {
3441 /* count the ssl reuse ratio and avoid overflows in both directions */
3442 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3443 }
3444#endif
3445
3446 if (len < INF_TOTAL_FIELDS)
3447 return 0;
3448
3449 chunk_reset(out);
3450 memset(info, 0, sizeof(*info) * len);
3451
3452 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003453 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3454 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003455
Yves Lafon95317282018-02-26 11:10:37 +01003456 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003457 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3458 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3459 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3460
3461 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3462 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3463
3464 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3465 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3466 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3467 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3468 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3469 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3470 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3471 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3472 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3473 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3474 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3475 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3476#ifdef USE_OPENSSL
3477 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3478 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3479 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3480#endif
3481 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3482 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3483 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3484 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3485 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3486 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3487 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3488 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3489 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3490
3491#ifdef USE_OPENSSL
3492 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3493 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3494 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3495 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3496 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3497 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3498 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3499 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3500 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3501 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3502#endif
3503 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3504 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3505 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3506#ifdef USE_ZLIB
3507 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3508 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3509#endif
3510 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003511 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003512 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003513 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3514 if (global.desc)
3515 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003516 info[INF_STOPPING] = mkf_u32(0, stopping);
3517 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003518 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003519 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003520 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003521 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003522 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003523 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003524 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003525 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003526 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003527 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 +02003528 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003529 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003530
3531 return 1;
3532}
3533
3534/* This function dumps information onto the stream interface's read buffer.
3535 * It returns 0 as long as it does not complete, non-zero upon completion.
3536 * No state is used.
3537 */
3538static int stats_dump_info_to_buffer(struct stream_interface *si)
3539{
3540 struct appctx *appctx = __objt_appctx(si->end);
3541
3542 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3543 return 0;
3544
3545 chunk_reset(&trash);
3546
3547 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003548 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003549 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003550 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003551 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003552 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003553
Willy Tarreau06d80a92017-10-19 14:32:15 +02003554 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003555 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003556 return 0;
3557 }
3558
3559 return 1;
3560}
3561
Simon Horman6f6bb382017-01-04 09:37:26 +01003562/* This function dumps the schema onto the stream interface's read buffer.
3563 * It returns 0 as long as it does not complete, non-zero upon completion.
3564 * No state is used.
3565 *
3566 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3567 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3568 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003569static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003570{
3571
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003572 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003573
3574 chunk_strcat(out,
3575 "{"
3576 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3577 "\"oneOf\":["
3578 "{"
3579 "\"title\":\"Info\","
3580 "\"type\":\"array\","
3581 "\"items\":{"
3582 "\"properties\":{"
3583 "\"title\":\"InfoItem\","
3584 "\"type\":\"object\","
3585 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3586 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3587 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3588 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3589 "},"
3590 "\"required\":[\"field\",\"processNum\",\"tags\","
3591 "\"value\"]"
3592 "}"
3593 "},"
3594 "{"
3595 "\"title\":\"Stat\","
3596 "\"type\":\"array\","
3597 "\"items\":{"
3598 "\"title\":\"InfoItem\","
3599 "\"type\":\"object\","
3600 "\"properties\":{"
3601 "\"objType\":{"
3602 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3603 "\"Server\",\"Unknown\"]"
3604 "},"
3605 "\"proxyId\":{"
3606 "\"type\":\"integer\","
3607 "\"minimum\":0"
3608 "},"
3609 "\"id\":{"
3610 "\"type\":\"integer\","
3611 "\"minimum\":0"
3612 "},"
3613 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3614 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3615 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3616 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3617 "},"
3618 "\"required\":[\"objType\",\"proxyId\",\"id\","
3619 "\"field\",\"processNum\",\"tags\","
3620 "\"value\"]"
3621 "}"
3622 "},"
3623 "{"
3624 "\"title\":\"Error\","
3625 "\"type\":\"object\","
3626 "\"properties\":{"
3627 "\"errorStr\":{"
3628 "\"type\":\"string\""
3629 "},"
3630 "\"required\":[\"errorStr\"]"
3631 "}"
3632 "}"
3633 "],"
3634 "\"definitions\":{"
3635 "\"field\":{"
3636 "\"type\":\"object\","
3637 "\"pos\":{"
3638 "\"type\":\"integer\","
3639 "\"minimum\":0"
3640 "},"
3641 "\"name\":{"
3642 "\"type\":\"string\""
3643 "},"
3644 "\"required\":[\"pos\",\"name\"]"
3645 "},"
3646 "\"processNum\":{"
3647 "\"type\":\"integer\","
3648 "\"minimum\":1"
3649 "},"
3650 "\"tags\":{"
3651 "\"type\":\"object\","
3652 "\"origin\":{"
3653 "\"type\":\"string\","
3654 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3655 "\"Config\",\"Product\",\"Unknown\"]"
3656 "},"
3657 "\"nature\":{"
3658 "\"type\":\"string\","
3659 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3660 "\"Rate\",\"Counter\",\"Duration\","
3661 "\"Age\",\"Time\",\"Name\",\"Output\","
3662 "\"Avg\", \"Unknown\"]"
3663 "},"
3664 "\"scope\":{"
3665 "\"type\":\"string\","
3666 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3667 "\"System\",\"Unknown\"]"
3668 "},"
3669 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3670 "},"
3671 "\"typedValue\":{"
3672 "\"type\":\"object\","
3673 "\"oneOf\":["
3674 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3675 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3676 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3677 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3678 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3679 "],"
3680 "\"definitions\":{"
3681 "\"s32Value\":{"
3682 "\"properties\":{"
3683 "\"type\":{"
3684 "\"type\":\"string\","
3685 "\"enum\":[\"s32\"]"
3686 "},"
3687 "\"value\":{"
3688 "\"type\":\"integer\","
3689 "\"minimum\":-2147483648,"
3690 "\"maximum\":2147483647"
3691 "}"
3692 "},"
3693 "\"required\":[\"type\",\"value\"]"
3694 "},"
3695 "\"s64Value\":{"
3696 "\"properties\":{"
3697 "\"type\":{"
3698 "\"type\":\"string\","
3699 "\"enum\":[\"s64\"]"
3700 "},"
3701 "\"value\":{"
3702 "\"type\":\"integer\","
3703 "\"minimum\":-9007199254740991,"
3704 "\"maximum\":9007199254740991"
3705 "}"
3706 "},"
3707 "\"required\":[\"type\",\"value\"]"
3708 "},"
3709 "\"u32Value\":{"
3710 "\"properties\":{"
3711 "\"type\":{"
3712 "\"type\":\"string\","
3713 "\"enum\":[\"u32\"]"
3714 "},"
3715 "\"value\":{"
3716 "\"type\":\"integer\","
3717 "\"minimum\":0,"
3718 "\"maximum\":4294967295"
3719 "}"
3720 "},"
3721 "\"required\":[\"type\",\"value\"]"
3722 "},"
3723 "\"u64Value\":{"
3724 "\"properties\":{"
3725 "\"type\":{"
3726 "\"type\":\"string\","
3727 "\"enum\":[\"u64\"]"
3728 "},"
3729 "\"value\":{"
3730 "\"type\":\"integer\","
3731 "\"minimum\":0,"
3732 "\"maximum\":9007199254740991"
3733 "}"
3734 "},"
3735 "\"required\":[\"type\",\"value\"]"
3736 "},"
3737 "\"strValue\":{"
3738 "\"properties\":{"
3739 "\"type\":{"
3740 "\"type\":\"string\","
3741 "\"enum\":[\"str\"]"
3742 "},"
3743 "\"value\":{\"type\":\"string\"}"
3744 "},"
3745 "\"required\":[\"type\",\"value\"]"
3746 "},"
3747 "\"unknownValue\":{"
3748 "\"properties\":{"
3749 "\"type\":{"
3750 "\"type\":\"integer\","
3751 "\"minimum\":0"
3752 "},"
3753 "\"value\":{"
3754 "\"type\":\"string\","
3755 "\"enum\":[\"unknown\"]"
3756 "}"
3757 "},"
3758 "\"required\":[\"type\",\"value\"]"
3759 "}"
3760 "}"
3761 "}"
3762 "}"
3763 "}");
3764
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003765 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003766 chunk_reset(out);
3767 chunk_appendf(out,
3768 "{\"errorStr\":\"output buffer too short\"}");
3769 }
3770}
3771
3772/* This function dumps the schema onto the stream interface's read buffer.
3773 * It returns 0 as long as it does not complete, non-zero upon completion.
3774 * No state is used.
3775 */
3776static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3777{
3778 chunk_reset(&trash);
3779
3780 stats_dump_json_schema(&trash);
3781
Willy Tarreau06d80a92017-10-19 14:32:15 +02003782 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003783 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003784 return 0;
3785 }
3786
3787 return 1;
3788}
3789
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003790static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003791{
3792 struct proxy *px;
3793 struct server *sv;
3794 struct listener *li;
3795 int clrall = 0;
3796
3797 if (strcmp(args[2], "all") == 0)
3798 clrall = 1;
3799
3800 /* check permissions */
3801 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3802 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3803 return 1;
3804
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003805 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003806 if (clrall) {
3807 memset(&px->be_counters, 0, sizeof(px->be_counters));
3808 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3809 }
3810 else {
3811 px->be_counters.conn_max = 0;
3812 px->be_counters.p.http.rps_max = 0;
3813 px->be_counters.sps_max = 0;
3814 px->be_counters.cps_max = 0;
3815 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003816 px->be_counters.qtime_max = 0;
3817 px->be_counters.ctime_max = 0;
3818 px->be_counters.dtime_max = 0;
3819 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003820
3821 px->fe_counters.conn_max = 0;
3822 px->fe_counters.p.http.rps_max = 0;
3823 px->fe_counters.sps_max = 0;
3824 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003825 }
3826
3827 for (sv = px->srv; sv; sv = sv->next)
3828 if (clrall)
3829 memset(&sv->counters, 0, sizeof(sv->counters));
3830 else {
3831 sv->counters.cur_sess_max = 0;
3832 sv->counters.nbpend_max = 0;
3833 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003834 sv->counters.qtime_max = 0;
3835 sv->counters.ctime_max = 0;
3836 sv->counters.dtime_max = 0;
3837 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003838 }
3839
3840 list_for_each_entry(li, &px->conf.listeners, by_fe)
3841 if (li->counters) {
3842 if (clrall)
3843 memset(li->counters, 0, sizeof(*li->counters));
3844 else
3845 li->counters->conn_max = 0;
3846 }
3847 }
3848
3849 global.cps_max = 0;
3850 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003851 global.ssl_max = 0;
3852 global.ssl_fe_keys_max = 0;
3853 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003854
3855 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003856 return 1;
3857}
3858
3859
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003860static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003861{
Willy Tarreau2f397382019-10-09 11:43:59 +02003862 int arg = 2;
3863
Willy Tarreaud25fc792016-12-16 12:33:47 +01003864 appctx->ctx.stats.scope_str = 0;
3865 appctx->ctx.stats.scope_len = 0;
3866 appctx->ctx.stats.flags = 0;
3867
Willy Tarreau2f397382019-10-09 11:43:59 +02003868 while (*args[arg]) {
3869 if (strcmp(args[arg], "typed") == 0)
3870 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3871 else if (strcmp(args[arg], "json") == 0)
3872 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3873 else if (strcmp(args[arg], "desc") == 0)
3874 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3875 arg++;
3876 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003877 return 0;
3878}
3879
3880
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003881static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003882{
Willy Tarreau2f397382019-10-09 11:43:59 +02003883 int arg = 2;
3884
Willy Tarreaud25fc792016-12-16 12:33:47 +01003885 appctx->ctx.stats.scope_str = 0;
3886 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003887 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3888
3889 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3890 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003891
Willy Tarreau2f397382019-10-09 11:43:59 +02003892 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003893 struct proxy *px;
3894
Willy Tarreau2f397382019-10-09 11:43:59 +02003895 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003896 if (px)
3897 appctx->ctx.stats.iid = px->uuid;
3898 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003899 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003900
Willy Tarreau9d008692019-08-09 11:21:01 +02003901 if (!appctx->ctx.stats.iid)
3902 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003903
Willy Tarreau2b812e22016-11-22 16:18:05 +01003904 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003905 appctx->ctx.stats.type = atoi(args[arg+1]);
3906 appctx->ctx.stats.sid = atoi(args[arg+2]);
3907 arg += 3;
3908 }
3909
3910 while (*args[arg]) {
3911 if (strcmp(args[arg], "typed") == 0)
3912 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3913 else if (strcmp(args[arg], "json") == 0)
3914 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3915 else if (strcmp(args[arg], "desc") == 0)
3916 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3917 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003918 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003919
Willy Tarreau2b812e22016-11-22 16:18:05 +01003920 return 0;
3921}
3922
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003923static int cli_io_handler_dump_info(struct appctx *appctx)
3924{
3925 return stats_dump_info_to_buffer(appctx->owner);
3926}
3927
Willy Tarreau2b812e22016-11-22 16:18:05 +01003928/* This I/O handler runs as an applet embedded in a stream interface. It is
3929 * used to send raw stats over a socket.
3930 */
3931static int cli_io_handler_dump_stat(struct appctx *appctx)
3932{
Christopher Fauletef779222018-10-31 08:47:01 +01003933 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003934}
3935
Simon Horman6f6bb382017-01-04 09:37:26 +01003936static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3937{
3938 return stats_dump_json_schema_to_buffer(appctx->owner);
3939}
3940
Willy Tarreau2b812e22016-11-22 16:18:05 +01003941/* register cli keywords */
3942static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003943 { { "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 +02003944 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3945 { { "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 +01003946 { { "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 +01003947 {{},}
3948}};
3949
Willy Tarreau0108d902018-11-25 19:14:37 +01003950INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3951
William Lallemand74c24fb2016-11-21 17:18:36 +01003952struct applet http_stats_applet = {
3953 .obj_type = OBJ_TYPE_APPLET,
3954 .name = "<STATS>", /* used for logging */
3955 .fct = http_stats_io_handler,
3956 .release = NULL,
3957};
3958
William Lallemand74c24fb2016-11-21 17:18:36 +01003959/*
3960 * Local variables:
3961 * c-indent-level: 8
3962 * c-basic-offset: 8
3963 * End:
3964 */