blob: e8ea831a7e6c982cddc1c568c8d0d4aacbcb4729 [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)" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100150};
151
Willy Tarreaueaa55372019-10-09 07:39:11 +0200152const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200153 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
154 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
155 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
156 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
157 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
158 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
159 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
160 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
161 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
162 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
163 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
164 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
165 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
166 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
167 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
168 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
169 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
170 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
171 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
172 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
173 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
174 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
175 [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" },
176 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
177 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
178 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
179 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
180 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
181 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
182 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
183 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
184 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
185 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
186 [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)" },
187 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
188 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
189 [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" },
190 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
191 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
192 [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" },
193 [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" },
194 [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" },
195 [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" },
196 [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" },
197 [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)" },
198 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
199 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
200 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
201 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
202 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
203 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
204 [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" },
205 [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" },
206 [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)" },
207 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
208 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
209 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
210 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
211 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
212 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
213 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
214 [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)" },
215 [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" },
216 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
217 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
218 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
219 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
220 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
221 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
222 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
223 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
224 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
225 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
226 [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" },
227 [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" },
228 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
229 [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" },
230 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
231 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
232 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
233 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
234 [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" },
235 [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" },
236 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
237 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
238 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
239 [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" },
240 [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" },
241 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
242 [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 +0100243 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
244 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
245 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
246 [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 +0100247 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200248 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
249 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
250 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200251 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100252};
253
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100254/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100255static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100256/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100257static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100258
Christopher Faulet6338a082019-09-09 15:50:54 +0200259static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100260
Christopher Fauletef779222018-10-31 08:47:01 +0100261static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
262{
263 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100264 if (chk->data >= channel_htx_recv_max(chn, htx))
265 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200266 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100267 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100268 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100269 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100270 }
271 else {
272 if (ci_putchk(chn, chk) == -1)
273 return 0;
274 }
275 return 1;
276}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100277
Christopher Fauleted7a0662019-01-14 11:07:34 +0100278static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
279{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200280 struct channel *req = si_oc(si);
281 struct htx *htx = htxbuf(&req->buf);
282 struct htx_blk *blk;
283 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100284
Christopher Fauletb7f88902019-07-15 21:56:43 +0200285 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100286 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200287 ALREADY_CHECKED(blk);
288 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
289 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100290}
291
William Lallemand74c24fb2016-11-21 17:18:36 +0100292/*
293 * http_stats_io_handler()
294 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
295 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100296 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100297 * -> stats_dump_html_head() // emits the HTML headers
298 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
299 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
300 * -> stats_dump_html_px_hdr()
301 * -> stats_dump_fe_stats()
302 * -> stats_dump_li_stats()
303 * -> stats_dump_sv_stats()
304 * -> stats_dump_be_stats()
305 * -> stats_dump_html_px_end()
306 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100307 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100308 */
309
310
William Lallemand74c24fb2016-11-21 17:18:36 +0100311/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
312 * for clearing it if needed.
313 * NOTE: Some tools happen to rely on the field position instead of its name,
314 * so please only append new fields at the end, never in the middle.
315 */
316static void stats_dump_csv_header()
317{
318 int field;
319
320 chunk_appendf(&trash, "# ");
321 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200322 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100323
324 chunk_appendf(&trash, "\n");
325}
326
327
328/* Emits a stats field without any surrounding element and properly encoded to
329 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
330 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200331int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100332{
333 switch (field_format(f, 0)) {
334 case FF_EMPTY: return 1;
335 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
336 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
337 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
338 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200339 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100340 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
341 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
342 }
343}
344
345/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
346 * output is supposed to be used on its own line. Returns non-zero on success, 0
347 * if the buffer is full.
348 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200349int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100350{
351 switch (field_format(f, 0)) {
352 case FF_EMPTY: return 1;
353 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
354 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
355 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
356 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200357 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100358 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
359 default: return chunk_appendf(out, "%08x:?", f->type);
360 }
361}
362
Simon Horman05ee2132017-01-04 09:37:25 +0100363/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
364 * the recommendation for interoperable integers in section 6 of RFC 7159.
365 */
366#define JSON_INT_MAX ((1ULL << 53) - 1)
367#define JSON_INT_MIN (0 - JSON_INT_MAX)
368
369/* Emits a stats field value and its type in JSON.
370 * Returns non-zero on success, 0 on error.
371 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200372int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100373{
374 int old_len;
375 char buf[20];
376 const char *type, *value = buf, *quote = "";
377
378 switch (field_format(f, 0)) {
379 case FF_EMPTY: return 1;
380 case FF_S32: type = "\"s32\"";
381 snprintf(buf, sizeof(buf), "%d", f->u.s32);
382 break;
383 case FF_U32: type = "\"u32\"";
384 snprintf(buf, sizeof(buf), "%u", f->u.u32);
385 break;
386 case FF_S64: type = "\"s64\"";
387 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
388 return 0;
389 type = "\"s64\"";
390 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
391 break;
392 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
393 return 0;
394 type = "\"u64\"";
395 snprintf(buf, sizeof(buf), "%llu",
396 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200397 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200398 case FF_FLT: type = "\"flt\"";
399 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100400 break;
401 case FF_STR: type = "\"str\"";
402 value = field_str(f, 0);
403 quote = "\"";
404 break;
405 default: snprintf(buf, sizeof(buf), "%u", f->type);
406 type = buf;
407 value = "unknown";
408 quote = "\"";
409 break;
410 }
411
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200412 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100413 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
414 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200415 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100416}
417
William Lallemand74c24fb2016-11-21 17:18:36 +0100418/* Emits an encoding of the field type on 3 characters followed by a delimiter.
419 * Returns non-zero on success, 0 if the buffer is full.
420 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200421int stats_emit_field_tags(struct buffer *out, const struct field *f,
422 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100423{
424 char origin, nature, scope;
425
426 switch (field_origin(f, 0)) {
427 case FO_METRIC: origin = 'M'; break;
428 case FO_STATUS: origin = 'S'; break;
429 case FO_KEY: origin = 'K'; break;
430 case FO_CONFIG: origin = 'C'; break;
431 case FO_PRODUCT: origin = 'P'; break;
432 default: origin = '?'; break;
433 }
434
435 switch (field_nature(f, 0)) {
436 case FN_GAUGE: nature = 'G'; break;
437 case FN_LIMIT: nature = 'L'; break;
438 case FN_MIN: nature = 'm'; break;
439 case FN_MAX: nature = 'M'; break;
440 case FN_RATE: nature = 'R'; break;
441 case FN_COUNTER: nature = 'C'; break;
442 case FN_DURATION: nature = 'D'; break;
443 case FN_AGE: nature = 'A'; break;
444 case FN_TIME: nature = 'T'; break;
445 case FN_NAME: nature = 'N'; break;
446 case FN_OUTPUT: nature = 'O'; break;
447 case FN_AVG: nature = 'a'; break;
448 default: nature = '?'; break;
449 }
450
451 switch (field_scope(f, 0)) {
452 case FS_PROCESS: scope = 'P'; break;
453 case FS_SERVICE: scope = 'S'; break;
454 case FS_SYSTEM: scope = 's'; break;
455 case FS_CLUSTER: scope = 'C'; break;
456 default: scope = '?'; break;
457 }
458
459 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
460}
461
Simon Horman05ee2132017-01-04 09:37:25 +0100462/* Emits an encoding of the field type as JSON.
463 * Returns non-zero on success, 0 if the buffer is full.
464 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200465int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100466{
467 const char *origin, *nature, *scope;
468 int old_len;
469
470 switch (field_origin(f, 0)) {
471 case FO_METRIC: origin = "Metric"; break;
472 case FO_STATUS: origin = "Status"; break;
473 case FO_KEY: origin = "Key"; break;
474 case FO_CONFIG: origin = "Config"; break;
475 case FO_PRODUCT: origin = "Product"; break;
476 default: origin = "Unknown"; break;
477 }
478
479 switch (field_nature(f, 0)) {
480 case FN_GAUGE: nature = "Gauge"; break;
481 case FN_LIMIT: nature = "Limit"; break;
482 case FN_MIN: nature = "Min"; break;
483 case FN_MAX: nature = "Max"; break;
484 case FN_RATE: nature = "Rate"; break;
485 case FN_COUNTER: nature = "Counter"; break;
486 case FN_DURATION: nature = "Duration"; break;
487 case FN_AGE: nature = "Age"; break;
488 case FN_TIME: nature = "Time"; break;
489 case FN_NAME: nature = "Name"; break;
490 case FN_OUTPUT: nature = "Output"; break;
491 case FN_AVG: nature = "Avg"; break;
492 default: nature = "Unknown"; break;
493 }
494
495 switch (field_scope(f, 0)) {
496 case FS_PROCESS: scope = "Process"; break;
497 case FS_SERVICE: scope = "Service"; break;
498 case FS_SYSTEM: scope = "System"; break;
499 case FS_CLUSTER: scope = "Cluster"; break;
500 default: scope = "Unknown"; break;
501 }
502
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100504 chunk_appendf(out, "\"tags\":{"
505 "\"origin\":\"%s\","
506 "\"nature\":\"%s\","
507 "\"scope\":\"%s\""
508 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100510}
William Lallemand74c24fb2016-11-21 17:18:36 +0100511
512/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200513static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200514 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100515{
516 int field;
517
518 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
519 if (!stats_emit_raw_data_field(out, &stats[field]))
520 return 0;
521 if (!chunk_strcat(out, ","))
522 return 0;
523 }
524 chunk_strcat(out, "\n");
525 return 1;
526}
527
528/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200529static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200530 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100531{
532 int field;
533
534 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
535 if (!stats[field].type)
536 continue;
537
538 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
539 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
540 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
541 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
542 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
543 '?',
544 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200545 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100546
547 if (!stats_emit_field_tags(out, &stats[field], ':'))
548 return 0;
549 if (!stats_emit_typed_data_field(out, &stats[field]))
550 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200551 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
552 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100553 if (!chunk_strcat(out, "\n"))
554 return 0;
555 }
556 return 1;
557}
558
Simon Horman05ee2132017-01-04 09:37:25 +0100559/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200560static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200561 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100562{
563 int field;
564 int started = 0;
565
566 if (!chunk_strcat(out, "["))
567 return 0;
568
569 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
570 int old_len;
571
572 if (!field_format(info, field))
573 continue;
574
575 if (started && !chunk_strcat(out, ","))
576 goto err;
577 started = 1;
578
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200579 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100580 chunk_appendf(out,
581 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
582 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200583 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100584 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200585 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100586 goto err;
587
588 if (!stats_emit_json_field_tags(out, &info[field]))
589 goto err;
590
591 if (!stats_emit_json_data_field(out, &info[field]))
592 goto err;
593
594 if (!chunk_strcat(out, "}"))
595 goto err;
596 }
597
598 if (!chunk_strcat(out, "]"))
599 goto err;
600 return 1;
601
602err:
603 chunk_reset(out);
604 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
605 return 0;
606}
607
608/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200609static int stats_dump_fields_json(struct buffer *out,
610 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200611 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100612{
613 int field;
614 int started = 0;
615
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200616 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100617 return 0;
618 if (!chunk_strcat(out, "["))
619 return 0;
620
621 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
622 const char *obj_type;
623 int old_len;
624
625 if (!stats[field].type)
626 continue;
627
628 if (started && !chunk_strcat(out, ","))
629 goto err;
630 started = 1;
631
632 switch (stats[ST_F_TYPE].u.u32) {
633 case STATS_TYPE_FE: obj_type = "Frontend"; break;
634 case STATS_TYPE_BE: obj_type = "Backend"; break;
635 case STATS_TYPE_SO: obj_type = "Listener"; break;
636 case STATS_TYPE_SV: obj_type = "Server"; break;
637 default: obj_type = "Unknown"; break;
638 }
639
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200640 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100641 chunk_appendf(out,
642 "{"
643 "\"objType\":\"%s\","
644 "\"proxyId\":%d,"
645 "\"id\":%d,"
646 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
647 "\"processNum\":%u,",
648 obj_type, stats[ST_F_IID].u.u32,
649 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200650 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200651 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100652 goto err;
653
654 if (!stats_emit_json_field_tags(out, &stats[field]))
655 goto err;
656
657 if (!stats_emit_json_data_field(out, &stats[field]))
658 goto err;
659
660 if (!chunk_strcat(out, "}"))
661 goto err;
662 }
663
664 if (!chunk_strcat(out, "]"))
665 goto err;
666
667 return 1;
668
669err:
670 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200671 if (flags & STAT_STARTED)
672 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100673 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
674 return 0;
675}
676
William Lallemand74c24fb2016-11-21 17:18:36 +0100677/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200678 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200679 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100680 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200681static int stats_dump_fields_html(struct buffer *out,
682 const struct field *stats,
683 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100684{
Willy Tarreau83061a82018-07-13 11:56:34 +0200685 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100686
687 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
688 chunk_appendf(out,
689 /* name, queue */
690 "<tr class=\"frontend\">");
691
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200692 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100693 /* Column sub-heading for Enable or Disable server */
694 chunk_appendf(out, "<td></td>");
695 }
696
697 chunk_appendf(out,
698 "<td class=ac>"
699 "<a name=\"%s/Frontend\"></a>"
700 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
701 "<td colspan=3></td>"
702 "",
703 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
704
705 chunk_appendf(out,
706 /* sessions rate : current */
707 "<td><u>%s<div class=tips><table class=det>"
708 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
709 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
710 "",
711 U2H(stats[ST_F_RATE].u.u32),
712 U2H(stats[ST_F_CONN_RATE].u.u32),
713 U2H(stats[ST_F_RATE].u.u32));
714
715 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
716 chunk_appendf(out,
717 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
718 U2H(stats[ST_F_REQ_RATE].u.u32));
719
720 chunk_appendf(out,
721 "</table></div></u></td>"
722 /* sessions rate : max */
723 "<td><u>%s<div class=tips><table class=det>"
724 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
725 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
726 "",
727 U2H(stats[ST_F_RATE_MAX].u.u32),
728 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
729 U2H(stats[ST_F_RATE_MAX].u.u32));
730
731 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
732 chunk_appendf(out,
733 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
734 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
735
736 chunk_appendf(out,
737 "</table></div></u></td>"
738 /* sessions rate : limit */
739 "<td>%s</td>",
740 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
741
742 chunk_appendf(out,
743 /* sessions: current, max, limit, total */
744 "<td>%s</td><td>%s</td><td>%s</td>"
745 "<td><u>%s<div class=tips><table class=det>"
746 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
747 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
748 "",
749 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
750 U2H(stats[ST_F_STOT].u.u64),
751 U2H(stats[ST_F_CONN_TOT].u.u64),
752 U2H(stats[ST_F_STOT].u.u64));
753
754 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
755 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
756 chunk_appendf(out,
757 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
758 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
759 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
760 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
761 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
762 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
763 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
764 "<tr><th>- other responses:</th><td>%s</td></tr>"
765 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100766 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
767 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200768 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100769 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100770 "",
771 U2H(stats[ST_F_REQ_TOT].u.u64),
772 U2H(stats[ST_F_HRSP_1XX].u.u64),
773 U2H(stats[ST_F_HRSP_2XX].u.u64),
774 U2H(stats[ST_F_COMP_RSP].u.u64),
775 stats[ST_F_HRSP_2XX].u.u64 ?
776 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
777 U2H(stats[ST_F_HRSP_3XX].u.u64),
778 U2H(stats[ST_F_HRSP_4XX].u.u64),
779 U2H(stats[ST_F_HRSP_5XX].u.u64),
780 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200781 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100782 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
783 U2H(stats[ST_F_CACHE_HITS].u.u64),
784 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
785 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100786 U2H(stats[ST_F_WREW].u.u64),
787 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100788 }
789
790 chunk_appendf(out,
791 "</table></div></u></td>"
792 /* sessions: lbtot, lastsess */
793 "<td></td><td></td>"
794 /* bytes : in */
795 "<td>%s</td>"
796 "",
797 U2H(stats[ST_F_BIN].u.u64));
798
799 chunk_appendf(out,
800 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
801 "<td>%s%s<div class=tips><table class=det>"
802 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
803 "<tr><th>Compression in:</th><td>%s</td></tr>"
804 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
805 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
806 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
807 "</table></div>%s</td>",
808 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
809 U2H(stats[ST_F_BOUT].u.u64),
810 U2H(stats[ST_F_BOUT].u.u64),
811 U2H(stats[ST_F_COMP_IN].u.u64),
812 U2H(stats[ST_F_COMP_OUT].u.u64),
813 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
814 U2H(stats[ST_F_COMP_BYP].u.u64),
815 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
816 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,
817 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
818
819 chunk_appendf(out,
820 /* denied: req, resp */
821 "<td>%s</td><td>%s</td>"
822 /* errors : request, connect, response */
823 "<td>%s</td><td></td><td></td>"
824 /* warnings: retries, redispatches */
825 "<td></td><td></td>"
826 /* server status : reflect frontend status */
827 "<td class=ac>%s</td>"
828 /* rest of server: nothing */
829 "<td class=ac colspan=8></td></tr>"
830 "",
831 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
832 U2H(stats[ST_F_EREQ].u.u64),
833 field_str(stats, ST_F_STATUS));
834 }
835 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
836 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200837 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100838 /* Column sub-heading for Enable or Disable server */
839 chunk_appendf(out, "<td></td>");
840 }
841
842 chunk_appendf(out,
843 /* frontend name, listener name */
844 "<td class=ac><a name=\"%s/+%s\"></a>%s"
845 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
846 "",
847 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200848 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100849 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
850
Willy Tarreau708c4162019-10-09 10:19:16 +0200851 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100852 chunk_appendf(out, "<div class=tips>");
853
Willy Tarreau90807112020-02-25 08:16:33 +0100854 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100855 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
856 else if (*field_str(stats, ST_F_ADDR) == '[')
857 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
858 else if (*field_str(stats, ST_F_ADDR))
859 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
860
861 /* id */
862 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
863 }
864
865 chunk_appendf(out,
866 /* queue */
867 "%s</td><td colspan=3></td>"
868 /* sessions rate: current, max, limit */
869 "<td colspan=3>&nbsp;</td>"
870 /* sessions: current, max, limit, total, lbtot, lastsess */
871 "<td>%s</td><td>%s</td><td>%s</td>"
872 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
873 /* bytes: in, out */
874 "<td>%s</td><td>%s</td>"
875 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200876 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100877 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
878 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
879
880 chunk_appendf(out,
881 /* denied: req, resp */
882 "<td>%s</td><td>%s</td>"
883 /* errors: request, connect, response */
884 "<td>%s</td><td></td><td></td>"
885 /* warnings: retries, redispatches */
886 "<td></td><td></td>"
887 /* server status: reflect listener status */
888 "<td class=ac>%s</td>"
889 /* rest of server: nothing */
890 "<td class=ac colspan=8></td></tr>"
891 "",
892 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
893 U2H(stats[ST_F_EREQ].u.u64),
894 field_str(stats, ST_F_STATUS));
895 }
896 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
897 const char *style;
898
899 /* determine the style to use depending on the server's state,
900 * its health and weight. There isn't a 1-to-1 mapping between
901 * state and styles for the cases where the server is (still)
902 * up. The reason is that we don't want to report nolb and
903 * drain with the same color.
904 */
905
906 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
907 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
908 style = "down";
909 }
910 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
911 style = "going_up";
912 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400913 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
914 style = "draining";
915 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100916 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
917 style = "going_down";
918 }
919 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
920 style = "nolb";
921 }
922 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
923 style = "no_check";
924 }
925 else if (!stats[ST_F_CHKFAIL].type ||
926 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
927 /* no check or max health = UP */
928 if (stats[ST_F_WEIGHT].u.u32)
929 style = "up";
930 else
931 style = "draining";
932 }
933 else {
934 style = "going_down";
935 }
936
937 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
938 chunk_appendf(out, "<tr class=\"maintain\">");
939 else
940 chunk_appendf(out,
941 "<tr class=\"%s_%s\">",
942 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
943
944
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200945 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100946 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000947 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
948 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100949 field_str(stats, ST_F_SVNAME));
950
951 chunk_appendf(out,
952 "<td class=ac><a name=\"%s/%s\"></a>%s"
953 "<a class=lfsb href=\"#%s/%s\">%s</a>"
954 "",
955 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200956 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100957 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
958
Willy Tarreau708c4162019-10-09 10:19:16 +0200959 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100960 chunk_appendf(out, "<div class=tips>");
961
Willy Tarreau90807112020-02-25 08:16:33 +0100962 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100963 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
964 else if (*field_str(stats, ST_F_ADDR) == '[')
965 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
966 else if (*field_str(stats, ST_F_ADDR))
967 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
968
969 /* id */
970 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
971
972 /* cookie */
973 if (stats[ST_F_COOKIE].type) {
974 chunk_appendf(out, ", cookie: '");
975 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
976 chunk_htmlencode(out, &src);
977 chunk_appendf(out, "'");
978 }
979
980 chunk_appendf(out, "</div>");
981 }
982
983 chunk_appendf(out,
984 /* queue : current, max, limit */
985 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
986 /* sessions rate : current, max, limit */
987 "<td>%s</td><td>%s</td><td></td>"
988 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200989 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100990 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
991 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
992
993 chunk_appendf(out,
994 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200995 "<td><u>%s<div class=tips>"
996 "<table class=det>"
997 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200998 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200999 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001000 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1001 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001002 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001003 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1004 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1005 "</table></div></u>"
1006 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001007 "<td><u>%s<div class=tips><table class=det>"
1008 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1009 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001010 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001011 U2H(stats[ST_F_SCUR].u.u32),
1012 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1013 U2H(stats[ST_F_SRV_ICUR].u.u32),
1014 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1015 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001016 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001017
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001018 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1019 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1020 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001021 U2H(stats[ST_F_STOT].u.u64),
1022 U2H(stats[ST_F_STOT].u.u64));
1023
1024 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1025 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001026 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001027 "<tr><th>New connections:</th><td>%s</td></tr>"
1028 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001029 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001030 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1031 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1032 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1033 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1034 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1035 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001036 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001037 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001038 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001039 U2H(stats[ST_F_CONNECT].u.u64),
1040 U2H(stats[ST_F_REUSE].u.u64),
1041 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1042 (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 +02001043 U2H(stats[ST_F_REQ_TOT].u.u64),
1044 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1045 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1046 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1047 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1048 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1049 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1050 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1051 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1052 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1053 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1054 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1055 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001056 U2H(stats[ST_F_WREW].u.u64),
1057 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001058 }
1059
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001060 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1061 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1062 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1063 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1064 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001065 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001066 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1067 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1068 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1069 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001070
1071 chunk_appendf(out,
1072 "</table></div></u></td>"
1073 /* sessions: lbtot, last */
1074 "<td>%s</td><td>%s</td>",
1075 U2H(stats[ST_F_LBTOT].u.u64),
1076 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1077
1078 chunk_appendf(out,
1079 /* bytes : in, out */
1080 "<td>%s</td><td>%s</td>"
1081 /* denied: req, resp */
1082 "<td></td><td>%s</td>"
1083 /* errors : request, connect */
1084 "<td></td><td>%s</td>"
1085 /* errors : response */
1086 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1087 /* warnings: retries, redispatches */
1088 "<td>%lld</td><td>%lld</td>"
1089 "",
1090 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1091 U2H(stats[ST_F_DRESP].u.u64),
1092 U2H(stats[ST_F_ECON].u.u64),
1093 U2H(stats[ST_F_ERESP].u.u64),
1094 (long long)stats[ST_F_CLI_ABRT].u.u64,
1095 (long long)stats[ST_F_SRV_ABRT].u.u64,
1096 (long long)stats[ST_F_WRETR].u.u64,
1097 (long long)stats[ST_F_WREDIS].u.u64);
1098
1099 /* status, last change */
1100 chunk_appendf(out, "<td class=ac>");
1101
1102 /* FIXME!!!!
1103 * LASTCHG should contain the last change for *this* server and must be computed
1104 * properly above, as was done below, ie: this server if maint, otherwise ref server
1105 * if tracking. Note that ref is either local or remote depending on tracking.
1106 */
1107
1108
1109 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1110 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1111 }
1112 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1113 chunk_strcat(out, "<i>no check</i>");
1114 }
1115 else {
1116 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1117 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1118 if (stats[ST_F_CHECK_HEALTH].u.u32)
1119 chunk_strcat(out, " &uarr;");
1120 }
1121 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1122 chunk_strcat(out, " &darr;");
1123 }
1124
1125 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1126 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1127 chunk_appendf(out,
1128 "</td><td class=ac><u> %s",
1129 field_str(stats, ST_F_AGENT_STATUS));
1130
1131 if (stats[ST_F_AGENT_CODE].type)
1132 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1133
1134 if (stats[ST_F_AGENT_DURATION].type)
1135 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1136
1137 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1138
1139 if (*field_str(stats, ST_F_LAST_AGT)) {
1140 chunk_appendf(out, ": ");
1141 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1142 chunk_htmlencode(out, &src);
1143 }
1144 chunk_appendf(out, "</div></u>");
1145 }
1146 else if (stats[ST_F_CHECK_STATUS].type) {
1147 chunk_appendf(out,
1148 "</td><td class=ac><u> %s",
1149 field_str(stats, ST_F_CHECK_STATUS));
1150
1151 if (stats[ST_F_CHECK_CODE].type)
1152 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1153
1154 if (stats[ST_F_CHECK_DURATION].type)
1155 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1156
1157 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1158
1159 if (*field_str(stats, ST_F_LAST_CHK)) {
1160 chunk_appendf(out, ": ");
1161 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1162 chunk_htmlencode(out, &src);
1163 }
1164 chunk_appendf(out, "</div></u>");
1165 }
1166 else
1167 chunk_appendf(out, "</td><td>");
1168
1169 chunk_appendf(out,
1170 /* weight */
1171 "</td><td class=ac>%d</td>"
1172 /* act, bck */
1173 "<td class=ac>%s</td><td class=ac>%s</td>"
1174 "",
1175 stats[ST_F_WEIGHT].u.u32,
1176 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1177 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1178
1179 /* check failures: unique, fatal, down time */
1180 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1181 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1182 }
1183 else if (stats[ST_F_CHKFAIL].type) {
1184 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1185
1186 if (stats[ST_F_HANAFAIL].type)
1187 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1188
1189 chunk_appendf(out,
1190 "<div class=tips>Failed Health Checks%s</div></u></td>"
1191 "<td>%lld</td><td>%s</td>"
1192 "",
1193 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1194 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1195 }
1196 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1197 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1198 chunk_appendf(out,
1199 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1200 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1201 }
1202 else
1203 chunk_appendf(out, "<td colspan=3></td>");
1204
1205 /* throttle */
1206 if (stats[ST_F_THROTTLE].type)
1207 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1208 else
1209 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1210 }
1211 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1212 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001213 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001214 /* Column sub-heading for Enable or Disable server */
1215 chunk_appendf(out, "<td></td>");
1216 }
1217 chunk_appendf(out,
1218 "<td class=ac>"
1219 /* name */
1220 "%s<a name=\"%s/Backend\"></a>"
1221 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1222 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001223 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001224 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1225
Willy Tarreau708c4162019-10-09 10:19:16 +02001226 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001227 /* balancing */
1228 chunk_appendf(out, "<div class=tips>balancing: %s",
1229 field_str(stats, ST_F_ALGO));
1230
1231 /* cookie */
1232 if (stats[ST_F_COOKIE].type) {
1233 chunk_appendf(out, ", cookie: '");
1234 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1235 chunk_htmlencode(out, &src);
1236 chunk_appendf(out, "'");
1237 }
1238 chunk_appendf(out, "</div>");
1239 }
1240
1241 chunk_appendf(out,
1242 "%s</td>"
1243 /* queue : current, max */
1244 "<td>%s</td><td>%s</td><td></td>"
1245 /* sessions rate : current, max, limit */
1246 "<td>%s</td><td>%s</td><td></td>"
1247 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001248 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001249 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1250 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1251
1252 chunk_appendf(out,
1253 /* sessions: current, max, limit, total */
1254 "<td>%s</td><td>%s</td><td>%s</td>"
1255 "<td><u>%s<div class=tips><table class=det>"
1256 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1257 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001258 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 +01001259 U2H(stats[ST_F_STOT].u.u64),
1260 U2H(stats[ST_F_STOT].u.u64));
1261
1262 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1263 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1264 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001265 "<tr><th>New connections:</th><td>%s</td></tr>"
1266 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001267 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1268 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1269 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1270 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1271 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1272 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1273 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1274 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001275 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1276 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001277 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001278 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001279 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001280 U2H(stats[ST_F_CONNECT].u.u64),
1281 U2H(stats[ST_F_REUSE].u.u64),
1282 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1283 (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 +01001284 U2H(stats[ST_F_REQ_TOT].u.u64),
1285 U2H(stats[ST_F_HRSP_1XX].u.u64),
1286 U2H(stats[ST_F_HRSP_2XX].u.u64),
1287 U2H(stats[ST_F_COMP_RSP].u.u64),
1288 stats[ST_F_HRSP_2XX].u.u64 ?
1289 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1290 U2H(stats[ST_F_HRSP_3XX].u.u64),
1291 U2H(stats[ST_F_HRSP_4XX].u.u64),
1292 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001293 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001294 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1295 U2H(stats[ST_F_CACHE_HITS].u.u64),
1296 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1297 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001298 U2H(stats[ST_F_WREW].u.u64),
1299 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001300 }
1301
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001302 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1303 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1304 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1305 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1306 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001307 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001308 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1309 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1310 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1311 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001312
1313 chunk_appendf(out,
1314 "</table></div></u></td>"
1315 /* sessions: lbtot, last */
1316 "<td>%s</td><td>%s</td>"
1317 /* bytes: in */
1318 "<td>%s</td>"
1319 "",
1320 U2H(stats[ST_F_LBTOT].u.u64),
1321 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1322 U2H(stats[ST_F_BIN].u.u64));
1323
1324 chunk_appendf(out,
1325 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1326 "<td>%s%s<div class=tips><table class=det>"
1327 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1328 "<tr><th>Compression in:</th><td>%s</td></tr>"
1329 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1330 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1331 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1332 "</table></div>%s</td>",
1333 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1334 U2H(stats[ST_F_BOUT].u.u64),
1335 U2H(stats[ST_F_BOUT].u.u64),
1336 U2H(stats[ST_F_COMP_IN].u.u64),
1337 U2H(stats[ST_F_COMP_OUT].u.u64),
1338 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1339 U2H(stats[ST_F_COMP_BYP].u.u64),
1340 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1341 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,
1342 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1343
1344 chunk_appendf(out,
1345 /* denied: req, resp */
1346 "<td>%s</td><td>%s</td>"
1347 /* errors : request, connect */
1348 "<td></td><td>%s</td>"
1349 /* errors : response */
1350 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1351 /* warnings: retries, redispatches */
1352 "<td>%lld</td><td>%lld</td>"
1353 /* backend status: reflect backend status (up/down): we display UP
1354 * if the backend has known working servers or if it has no server at
1355 * all (eg: for stats). Then we display the total weight, number of
1356 * active and backups. */
1357 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1358 "<td class=ac>%d</td><td class=ac>%d</td>"
1359 "",
1360 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1361 U2H(stats[ST_F_ECON].u.u64),
1362 U2H(stats[ST_F_ERESP].u.u64),
1363 (long long)stats[ST_F_CLI_ABRT].u.u64,
1364 (long long)stats[ST_F_SRV_ABRT].u.u64,
1365 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1366 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1367 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1368 stats[ST_F_WEIGHT].u.u32,
1369 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1370
1371 chunk_appendf(out,
1372 /* rest of backend: nothing, down transitions, total downtime, throttle */
1373 "<td class=ac>&nbsp;</td><td>%d</td>"
1374 "<td>%s</td>"
1375 "<td></td>"
1376 "</tr>",
1377 stats[ST_F_CHKDOWN].u.u32,
1378 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1379 }
1380 return 1;
1381}
1382
Willy Tarreau43241ff2019-10-09 11:27:51 +02001383static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001384{
Simon Horman05ee2132017-01-04 09:37:25 +01001385 int ret;
1386
William Lallemand74c24fb2016-11-21 17:18:36 +01001387 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001388 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001389 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001390 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001391 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001392 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001393 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001394 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001395
1396 if (ret)
1397 appctx->ctx.stats.flags |= STAT_STARTED;
1398
1399 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001400}
1401
1402/* Fill <stats> with the frontend statistics. <stats> is
1403 * preallocated array of length <len>. The length of the array
1404 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1405 * this value, the function returns 0, otherwise, it returns 1.
1406 */
1407int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1408{
1409 if (len < ST_F_TOTAL_FIELDS)
1410 return 0;
1411
1412 memset(stats, 0, sizeof(*stats) * len);
1413
1414 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1415 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1416 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1417 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1418 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1419 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1420 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1421 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1422 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1423 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1424 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1425 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1426 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1427 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1428 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1429 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1430 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1431 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1432 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1433 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1434 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1435 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001436 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001437 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001438
1439 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1440 if (px->mode == PR_MODE_HTTP) {
1441 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1442 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1443 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1444 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1445 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1446 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1447 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001448 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1449 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001450 }
1451
1452 /* requests : req_rate, req_rate_max, req_tot, */
1453 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1454 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1455 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1456
1457 /* compression: in, out, bypassed, responses */
1458 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1459 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1460 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1461 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1462
1463 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1464 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1465 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1466 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1467
1468 return 1;
1469}
1470
1471/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1472 * the state from stream interface <si>. The caller is responsible for clearing
1473 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1474 */
1475static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1476{
1477 struct appctx *appctx = __objt_appctx(si->end);
1478
1479 if (!(px->cap & PR_CAP_FE))
1480 return 0;
1481
1482 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1483 return 0;
1484
1485 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1486 return 0;
1487
Willy Tarreau43241ff2019-10-09 11:27:51 +02001488 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001489}
1490
1491/* Fill <stats> with the listener statistics. <stats> is
1492 * preallocated array of length <len>. The length of the array
1493 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1494 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001495 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001496 */
1497int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1498 struct field *stats, int len)
1499{
Willy Tarreau83061a82018-07-13 11:56:34 +02001500 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001501
1502 if (len < ST_F_TOTAL_FIELDS)
1503 return 0;
1504
1505 if (!l->counters)
1506 return 0;
1507
1508 chunk_reset(out);
1509 memset(stats, 0, sizeof(*stats) * len);
1510
1511 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1512 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1513 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1514 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1515 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1516 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1517 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1518 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1519 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1520 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1521 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1522 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1523 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1524 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001525 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 +01001526 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1527 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1528 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1529 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001530 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001531 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001532
Willy Tarreau708c4162019-10-09 10:19:16 +02001533 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001534 char str[INET6_ADDRSTRLEN];
1535 int port;
1536
1537 port = get_host_port(&l->addr);
1538 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1539 case AF_INET:
1540 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1541 chunk_appendf(out, "%s:%d", str, port);
1542 break;
1543 case AF_INET6:
1544 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1545 chunk_appendf(out, "[%s]:%d", str, port);
1546 break;
1547 case AF_UNIX:
1548 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1549 break;
1550 case -1:
1551 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1552 chunk_strcat(out, strerror(errno));
1553 break;
1554 default: /* address family not supported */
1555 break;
1556 }
1557 }
1558
1559 return 1;
1560}
1561
1562/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001563 * from stream interface <si>. The caller is responsible for clearing the trash
1564 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001565 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001566static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001567{
1568 struct appctx *appctx = __objt_appctx(si->end);
1569
Willy Tarreau43241ff2019-10-09 11:27:51 +02001570 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001571 return 0;
1572
Willy Tarreau43241ff2019-10-09 11:27:51 +02001573 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001574}
1575
1576enum srv_stats_state {
1577 SRV_STATS_STATE_DOWN = 0,
1578 SRV_STATS_STATE_DOWN_AGENT,
1579 SRV_STATS_STATE_GOING_UP,
1580 SRV_STATS_STATE_UP_GOING_DOWN,
1581 SRV_STATS_STATE_UP,
1582 SRV_STATS_STATE_NOLB_GOING_DOWN,
1583 SRV_STATS_STATE_NOLB,
1584 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1585 SRV_STATS_STATE_DRAIN,
1586 SRV_STATS_STATE_DRAIN_AGENT,
1587 SRV_STATS_STATE_NO_CHECK,
1588
1589 SRV_STATS_STATE_COUNT, /* Must be last */
1590};
1591
1592static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1593 [SRV_STATS_STATE_DOWN] = "DOWN",
1594 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1595 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1596 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1597 [SRV_STATS_STATE_UP] = "UP",
1598 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1599 [SRV_STATS_STATE_NOLB] = "NOLB",
1600 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1601 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1602 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1603 [SRV_STATS_STATE_NO_CHECK] = "no check"
1604};
1605
1606/* Fill <stats> with the server statistics. <stats> is
1607 * preallocated array of length <len>. The length of the array
1608 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1609 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001610 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001611 */
1612int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1613 struct field *stats, int len)
1614{
1615 struct server *via, *ref;
1616 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001617 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001618 enum srv_stats_state state;
1619 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001620 long long srv_samples_counter;
1621 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001622
1623 if (len < ST_F_TOTAL_FIELDS)
1624 return 0;
1625
1626 memset(stats, 0, sizeof(*stats) * len);
1627
1628 /* we have "via" which is the tracked server as described in the configuration,
1629 * and "ref" which is the checked server and the end of the chain.
1630 */
1631 via = sv->track ? sv->track : sv;
1632 ref = via;
1633 while (ref->track)
1634 ref = ref->track;
1635
Emeric Brun52a91d32017-08-31 14:41:55 +02001636 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001637 if ((ref->check.state & CHK_ST_ENABLED) &&
1638 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1639 state = SRV_STATS_STATE_UP_GOING_DOWN;
1640 } else {
1641 state = SRV_STATS_STATE_UP;
1642 }
1643
Emeric Brun52a91d32017-08-31 14:41:55 +02001644 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001645 if (ref->agent.state & CHK_ST_ENABLED)
1646 state = SRV_STATS_STATE_DRAIN_AGENT;
1647 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1648 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1649 else
1650 state = SRV_STATS_STATE_DRAIN;
1651 }
1652
1653 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1654 state = SRV_STATS_STATE_NO_CHECK;
1655 }
1656 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001657 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001658 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1659 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1660 state = SRV_STATS_STATE_NOLB;
1661 } else {
1662 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1663 }
1664 }
1665 else { /* stopped */
1666 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1667 state = SRV_STATS_STATE_DOWN_AGENT;
1668 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1669 state = SRV_STATS_STATE_DOWN; /* DOWN */
1670 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1671 state = SRV_STATS_STATE_GOING_UP;
1672 } else {
1673 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1674 }
1675 }
1676
1677 chunk_reset(out);
1678
1679 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1680 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1681 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1682 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1683 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1684 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1685 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1686
1687 if (sv->maxconn)
1688 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1689
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001690 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1691 if (sv->max_idle_conns != -1)
1692 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1693
William Lallemand74c24fb2016-11-21 17:18:36 +01001694 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1695 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1696 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001697 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001698 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1699 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1700 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1701 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001702 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001703 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001704 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1705 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001706
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001707 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1708 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1709 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001710 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001711
William Lallemand74c24fb2016-11-21 17:18:36 +01001712 /* status */
1713 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001714 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001715 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001716 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001717 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001718 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001719 chunk_appendf(out, "MAINT");
1720 else
1721 chunk_appendf(out,
1722 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001723 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1724 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001725
1726 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1727 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001728 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 +01001729 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1730 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1731
1732 /* check failures: unique, fatal; last change, total downtime */
1733 if (sv->check.state & CHK_ST_ENABLED) {
1734 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1735 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1736 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1737 }
1738
1739 if (sv->maxqueue)
1740 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1741
1742 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1743 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1744 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1745
Emeric Brun52a91d32017-08-31 14:41:55 +02001746 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001747 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1748
1749 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1750
1751 if (sv->track) {
1752 char *fld_track = chunk_newstr(out);
1753
1754 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1755 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1756 }
1757
1758 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1759 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1760 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1761
1762 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1763 const char *fld_chksts;
1764
1765 fld_chksts = chunk_newstr(out);
1766 chunk_strcat(out, "* "); // for check in progress
1767 chunk_strcat(out, get_check_status_info(sv->check.status));
1768 if (!(sv->check.state & CHK_ST_INPROGRESS))
1769 fld_chksts += 2; // skip "* "
1770 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1771
1772 if (sv->check.status >= HCHK_STATUS_L57DATA)
1773 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1774
1775 if (sv->check.status >= HCHK_STATUS_CHECKED)
1776 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1777
1778 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1779 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1780 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1781 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1782 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1783 }
1784
1785 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1786 const char *fld_chksts;
1787
1788 fld_chksts = chunk_newstr(out);
1789 chunk_strcat(out, "* "); // for check in progress
1790 chunk_strcat(out, get_check_status_info(sv->agent.status));
1791 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1792 fld_chksts += 2; // skip "* "
1793 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1794
1795 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1796 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1797
1798 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1799 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1800
1801 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1802 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1803 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1804 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1805 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1806 }
1807
1808 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1809 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001810 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001811 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1812 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1813 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1814 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1815 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1816 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1817 }
1818
1819 if (ref->observe)
1820 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1821
1822 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1823 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1824 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1825
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001826 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1827 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1828 srv_samples_window = srv_samples_counter;
1829
1830 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1831 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1832 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1833 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001834
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001835 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1836 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1837 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1838 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1839
Willy Tarreau708c4162019-10-09 10:19:16 +02001840 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001841 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1842 case AF_INET:
1843 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001844 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001845 break;
1846 case AF_INET6:
1847 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001848 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001849 break;
1850 case AF_UNIX:
1851 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1852 break;
1853 case -1:
1854 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1855 chunk_strcat(out, strerror(errno));
1856 break;
1857 default: /* address family not supported */
1858 break;
1859 }
1860
1861 if (sv->cookie)
1862 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1863 }
1864
1865 return 1;
1866}
1867
1868/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001869 * from stream interface <si>, and server state <state>. The caller is
1870 * responsible for clearing the trash if needed. Returns non-zero if it emits
1871 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001872 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001873static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001874{
1875 struct appctx *appctx = __objt_appctx(si->end);
1876
Willy Tarreau43241ff2019-10-09 11:27:51 +02001877 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001878 return 0;
1879
Willy Tarreau43241ff2019-10-09 11:27:51 +02001880 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001881}
1882
1883/* Fill <stats> with the backend statistics. <stats> is
1884 * preallocated array of length <len>. The length of the array
1885 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1886 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001887 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001888 */
1889int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1890{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001891 long long be_samples_counter;
1892 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1893
William Lallemand74c24fb2016-11-21 17:18:36 +01001894 if (len < ST_F_TOTAL_FIELDS)
1895 return 0;
1896
1897 memset(stats, 0, sizeof(*stats) * len);
1898
1899 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1900 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1901 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1902 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1903 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001904 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001905 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1906 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1907 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1908 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1909 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1910 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1911 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1912 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1913 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1914 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1915 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001916 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001917 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001918 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1919 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001920 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1921 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1922 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1923 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1924 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1925 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1926 if (px->srv)
1927 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1928
1929 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1930 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1931 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1932 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1933 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1934 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1935 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1936
Willy Tarreau708c4162019-10-09 10:19:16 +02001937 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001938 if (px->cookie_name)
1939 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1940 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1941 }
1942
1943 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1944 if (px->mode == PR_MODE_HTTP) {
1945 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1946 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1947 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1948 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1949 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1950 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1951 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001952 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1953 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001954 }
1955
1956 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1957 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1958
1959 /* compression: in, out, bypassed, responses */
1960 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1961 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1962 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1963 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1964 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1965
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001966 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1967 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1968 be_samples_window = be_samples_counter;
1969
1970 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1971 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1972 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1973 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 +01001974
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001975 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1976 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1977 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1978 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1979
William Lallemand74c24fb2016-11-21 17:18:36 +01001980 return 1;
1981}
1982
1983/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001984 * interface <si>. The caller is responsible for clearing the trash if needed.
1985 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001986 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001987static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001988{
1989 struct appctx *appctx = __objt_appctx(si->end);
1990
1991 if (!(px->cap & PR_CAP_BE))
1992 return 0;
1993
1994 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1995 return 0;
1996
Willy Tarreau43241ff2019-10-09 11:27:51 +02001997 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001998 return 0;
1999
Willy Tarreau43241ff2019-10-09 11:27:51 +02002000 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002001}
2002
2003/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2004 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2005 * for clearing the trash if needed.
2006 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002007static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002008{
2009 struct appctx *appctx = __objt_appctx(si->end);
2010 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2011
2012 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2013 /* A form to enable/disable this proxy servers */
2014
2015 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2016 scope_txt[0] = 0;
2017 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002018 const char *scope_ptr = stats_scope_ptr(appctx, si);
2019
William Lallemand74c24fb2016-11-21 17:18:36 +01002020 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002021 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002022 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2023 }
2024
2025 chunk_appendf(&trash,
2026 "<form method=\"post\">");
2027 }
2028
2029 /* print a new table */
2030 chunk_appendf(&trash,
2031 "<table class=\"tbl\" width=\"100%%\">\n"
2032 "<tr class=\"titre\">"
2033 "<th class=\"pxname\" width=\"10%%\">");
2034
2035 chunk_appendf(&trash,
2036 "<a name=\"%s\"></a>%s"
2037 "<a class=px href=\"#%s\">%s</a>",
2038 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002039 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002040 px->id, px->id);
2041
Willy Tarreau676c29e2019-10-09 10:50:01 +02002042 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002043 /* cap, mode, id */
2044 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2045 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2046 px->uuid);
2047 chunk_appendf(&trash, "</div>");
2048 }
2049
2050 chunk_appendf(&trash,
2051 "%s</th>"
2052 "<th class=\"%s\" width=\"90%%\">%s</th>"
2053 "</tr>\n"
2054 "</table>\n"
2055 "<table class=\"tbl\" width=\"100%%\">\n"
2056 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002057 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002058 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2059
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002060 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002061 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002062 if ((px->cap & PR_CAP_BE) && px->srv)
2063 chunk_appendf(&trash,
2064 "<th rowspan=2 width=1><input type=\"checkbox\" "
2065 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2066 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2067 px->id,
2068 px->id);
2069 else
2070 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002071 }
2072
2073 chunk_appendf(&trash,
2074 "<th rowspan=2></th>"
2075 "<th colspan=3>Queue</th>"
2076 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2077 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2078 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2079 "<th colspan=9>Server</th>"
2080 "</tr>\n"
2081 "<tr class=\"titre\">"
2082 "<th>Cur</th><th>Max</th><th>Limit</th>"
2083 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2084 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2085 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2086 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2087 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2088 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2089 "<th>Thrtle</th>\n"
2090 "</tr>");
2091}
2092
2093/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2094 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2095 */
2096static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2097{
2098 struct appctx *appctx = __objt_appctx(si->end);
2099 chunk_appendf(&trash, "</table>");
2100
2101 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2102 /* close the form used to enable/disable this proxy servers */
2103 chunk_appendf(&trash,
2104 "Choose the action to perform on the checked servers : "
2105 "<select name=action>"
2106 "<option value=\"\"></option>"
2107 "<option value=\"ready\">Set state to READY</option>"
2108 "<option value=\"drain\">Set state to DRAIN</option>"
2109 "<option value=\"maint\">Set state to MAINT</option>"
2110 "<option value=\"dhlth\">Health: disable checks</option>"
2111 "<option value=\"ehlth\">Health: enable checks</option>"
2112 "<option value=\"hrunn\">Health: force UP</option>"
2113 "<option value=\"hnolb\">Health: force NOLB</option>"
2114 "<option value=\"hdown\">Health: force DOWN</option>"
2115 "<option value=\"dagent\">Agent: disable checks</option>"
2116 "<option value=\"eagent\">Agent: enable checks</option>"
2117 "<option value=\"arunn\">Agent: force UP</option>"
2118 "<option value=\"adown\">Agent: force DOWN</option>"
2119 "<option value=\"shutdown\">Kill Sessions</option>"
2120 "</select>"
2121 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2122 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2123 "</form>",
2124 px->uuid);
2125 }
2126
2127 chunk_appendf(&trash, "<p>\n");
2128}
2129
2130/*
2131 * Dumps statistics for a proxy. The output is sent to the stream interface's
2132 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2133 * buffer space, or non-zero if everything completed. This function is used
2134 * both by the CLI and the HTTP entry points, and is able to dump the output
2135 * in HTML or CSV formats. If the later, <uri> must be NULL.
2136 */
Christopher Fauletef779222018-10-31 08:47:01 +01002137int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2138 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002139{
2140 struct appctx *appctx = __objt_appctx(si->end);
2141 struct stream *s = si_strm(si);
2142 struct channel *rep = si_ic(si);
2143 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2144 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002145
2146 chunk_reset(&trash);
2147
2148 switch (appctx->ctx.stats.px_st) {
2149 case STAT_PX_ST_INIT:
2150 /* we are on a new proxy */
2151 if (uri && uri->scope) {
2152 /* we have a limited scope, we have to check the proxy name */
2153 struct stat_scope *scope;
2154 int len;
2155
2156 len = strlen(px->id);
2157 scope = uri->scope;
2158
2159 while (scope) {
2160 /* match exact proxy name */
2161 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2162 break;
2163
2164 /* match '.' which means 'self' proxy */
2165 if (!strcmp(scope->px_id, ".") && px == s->be)
2166 break;
2167 scope = scope->next;
2168 }
2169
2170 /* proxy name not found : don't dump anything */
2171 if (scope == NULL)
2172 return 1;
2173 }
2174
2175 /* if the user has requested a limited output and the proxy
2176 * name does not match, skip it.
2177 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002178 if (appctx->ctx.stats.scope_len) {
2179 const char *scope_ptr = stats_scope_ptr(appctx, si);
2180
2181 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2182 return 1;
2183 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002184
2185 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2186 (appctx->ctx.stats.iid != -1) &&
2187 (px->uuid != appctx->ctx.stats.iid))
2188 return 1;
2189
2190 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2191 /* fall through */
2192
2193 case STAT_PX_ST_TH:
2194 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002195 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002196 if (!stats_putchk(rep, htx, &trash))
2197 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002198 }
2199
2200 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2201 /* fall through */
2202
2203 case STAT_PX_ST_FE:
2204 /* print the frontend */
2205 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002206 if (!stats_putchk(rep, htx, &trash))
2207 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002208 }
2209
2210 appctx->ctx.stats.l = px->conf.listeners.n;
2211 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2212 /* fall through */
2213
2214 case STAT_PX_ST_LI:
2215 /* stats.l has been initialized above */
2216 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002217 if (htx) {
2218 if (htx_almost_full(htx))
2219 goto full;
2220 }
2221 else {
2222 if (buffer_almost_full(&rep->buf))
2223 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002224 }
2225
2226 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2227 if (!l->counters)
2228 continue;
2229
2230 if (appctx->ctx.stats.flags & STAT_BOUND) {
2231 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2232 break;
2233
2234 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2235 continue;
2236 }
2237
2238 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002239 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002240 if (!stats_putchk(rep, htx, &trash))
2241 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002242 }
2243 }
2244
2245 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2246 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2247 /* fall through */
2248
2249 case STAT_PX_ST_SV:
2250 /* stats.sv has been initialized above */
2251 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002252 if (htx) {
2253 if (htx_almost_full(htx))
2254 goto full;
2255 }
2256 else {
2257 if (buffer_almost_full(&rep->buf))
2258 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002259 }
2260
2261 sv = appctx->ctx.stats.sv;
2262
2263 if (appctx->ctx.stats.flags & STAT_BOUND) {
2264 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2265 break;
2266
2267 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2268 continue;
2269 }
2270
2271 svs = sv;
2272 while (svs->track)
2273 svs = svs->track;
2274
2275 /* do not report servers which are DOWN and not changing state */
2276 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002277 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2278 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002279 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2280 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2281 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2282 continue;
2283 }
2284
Willy Tarreau43241ff2019-10-09 11:27:51 +02002285 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002286 if (!stats_putchk(rep, htx, &trash))
2287 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002288 }
2289 } /* for sv */
2290
2291 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2292 /* fall through */
2293
2294 case STAT_PX_ST_BE:
2295 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002296 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002297 if (!stats_putchk(rep, htx, &trash))
2298 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002299 }
2300
2301 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2302 /* fall through */
2303
2304 case STAT_PX_ST_END:
2305 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2306 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002307 if (!stats_putchk(rep, htx, &trash))
2308 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002309 }
2310
2311 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2312 /* fall through */
2313
2314 case STAT_PX_ST_FIN:
2315 return 1;
2316
2317 default:
2318 /* unknown state, we should put an abort() here ! */
2319 return 1;
2320 }
Christopher Fauletef779222018-10-31 08:47:01 +01002321
2322 full:
2323 si_rx_room_blk(si);
2324 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002325}
2326
2327/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2328 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2329 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002330static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002331{
2332 /* WARNING! This must fit in the first buffer !!! */
2333 chunk_appendf(&trash,
2334 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2335 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2336 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2337 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2338 "<style type=\"text/css\"><!--\n"
2339 "body {"
2340 " font-family: arial, helvetica, sans-serif;"
2341 " font-size: 12px;"
2342 " font-weight: normal;"
2343 " color: black;"
2344 " background: white;"
2345 "}\n"
2346 "th,td {"
2347 " font-size: 10px;"
2348 "}\n"
2349 "h1 {"
2350 " font-size: x-large;"
2351 " margin-bottom: 0.5em;"
2352 "}\n"
2353 "h2 {"
2354 " font-family: helvetica, arial;"
2355 " font-size: x-large;"
2356 " font-weight: bold;"
2357 " font-style: italic;"
2358 " color: #6020a0;"
2359 " margin-top: 0em;"
2360 " margin-bottom: 0em;"
2361 "}\n"
2362 "h3 {"
2363 " font-family: helvetica, arial;"
2364 " font-size: 16px;"
2365 " font-weight: bold;"
2366 " color: #b00040;"
2367 " background: #e8e8d0;"
2368 " margin-top: 0em;"
2369 " margin-bottom: 0em;"
2370 "}\n"
2371 "li {"
2372 " margin-top: 0.25em;"
2373 " margin-right: 2em;"
2374 "}\n"
2375 ".hr {margin-top: 0.25em;"
2376 " border-color: black;"
2377 " border-bottom-style: solid;"
2378 "}\n"
2379 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2380 ".total {background: #20D0D0;color: #ffff80;}\n"
2381 ".frontend {background: #e8e8d0;}\n"
2382 ".socket {background: #d0d0d0;}\n"
2383 ".backend {background: #e8e8d0;}\n"
2384 ".active_down {background: #ff9090;}\n"
2385 ".active_going_up {background: #ffd020;}\n"
2386 ".active_going_down {background: #ffffa0;}\n"
2387 ".active_up {background: #c0ffc0;}\n"
2388 ".active_nolb {background: #20a0ff;}\n"
2389 ".active_draining {background: #20a0FF;}\n"
2390 ".active_no_check {background: #e0e0e0;}\n"
2391 ".backup_down {background: #ff9090;}\n"
2392 ".backup_going_up {background: #ff80ff;}\n"
2393 ".backup_going_down {background: #c060ff;}\n"
2394 ".backup_up {background: #b0d0ff;}\n"
2395 ".backup_nolb {background: #90b0e0;}\n"
2396 ".backup_draining {background: #cc9900;}\n"
2397 ".backup_no_check {background: #e0e0e0;}\n"
2398 ".maintain {background: #c07820;}\n"
2399 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2400 "\n"
2401 "a.px:link {color: #ffff40; text-decoration: none;}"
2402 "a.px:visited {color: #ffff40; text-decoration: none;}"
2403 "a.px:hover {color: #ffffff; text-decoration: none;}"
2404 "a.lfsb:link {color: #000000; text-decoration: none;}"
2405 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2406 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2407 "\n"
2408 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2409 "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"
2410 "table.tbl td.ac { text-align: center;}\n"
2411 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2412 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2413 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2414 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2415 "\n"
2416 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2417 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2418 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2419 "table.det { border-collapse: collapse; border-style: none; }\n"
2420 "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"
2421 "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"
2422 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2423 "div.tips {\n"
2424 " display:block;\n"
2425 " visibility:hidden;\n"
2426 " z-index:2147483647;\n"
2427 " position:absolute;\n"
2428 " padding:2px 4px 3px;\n"
2429 " background:#f0f060; color:#000000;\n"
2430 " border:1px solid #7040c0;\n"
2431 " white-space:nowrap;\n"
2432 " font-style:normal;font-size:11px;font-weight:normal;\n"
2433 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2434 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2435 "}\n"
2436 "u:hover div.tips {visibility:visible;}\n"
2437 "-->\n"
2438 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002439 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002440 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002441 );
2442}
2443
2444/* Dumps the HTML stats information block to the trash for and uses the state from
2445 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2446 * for clearing the trash if needed.
2447 */
2448static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2449{
2450 struct appctx *appctx = __objt_appctx(si->end);
2451 unsigned int up = (now.tv_sec - start_date.tv_sec);
2452 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002453 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002454 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2455
2456 /* Turn the bytes per second to bits per second and take care of the
2457 * usual ethernet overhead in order to help figure how far we are from
2458 * interface saturation since it's the only case which usually matters.
2459 * For this we count the total size of an Ethernet frame on the wire
2460 * including preamble and IFG (1538) for the largest TCP segment it
2461 * transports (1448 with TCP timestamps). This is not valid for smaller
2462 * packets (under-estimated), but it gives a reasonably accurate
2463 * estimation of how far we are from uplink saturation.
2464 */
2465 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002466
2467 /* WARNING! this has to fit the first packet too.
2468 * We are around 3.5 kB, add adding entries will
2469 * become tricky if we want to support 4kB buffers !
2470 */
2471 chunk_appendf(&trash,
2472 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2473 PRODUCT_NAME "%s</a></h1>\n"
2474 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2475 "<hr width=\"100%%\" class=\"hr\">\n"
2476 "<h3>&gt; General process information</h3>\n"
2477 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002478 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002479 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2480 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2481 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002482 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002483 "Running tasks: %d/%d; idle = %d %%<br>\n"
2484 "</td><td align=\"center\" nowrap>\n"
2485 "<table class=\"lgd\"><tr>\n"
2486 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2487 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2488 "</tr><tr>\n"
2489 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2490 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2491 "</tr><tr>\n"
2492 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2493 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2494 "</tr><tr>\n"
2495 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2496 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2497 "</tr><tr>\n"
2498 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2499 "</tr><tr>\n"
2500 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2501 "</tr></table>\n"
2502 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2503 "</td>"
2504 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2505 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2506 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002507 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2508 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2509 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2510 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2511 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002512 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002513 up / 86400, (up % 86400) / 3600,
2514 (up % 3600) / 60, (up % 60),
2515 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2516 global.rlimit_memmax ? " MB" : "",
2517 global.rlimit_nofile,
2518 global.maxsock, global.maxconn, global.maxpipes,
2519 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002520 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2521 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002522 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002523 );
2524
2525 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002526 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002527 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2528
2529 chunk_appendf(&trash,
2530 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2531 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2532 STAT_SCOPE_TXT_MAXLEN);
2533
2534 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2535 scope_txt[0] = 0;
2536 if (appctx->ctx.stats.scope_len) {
2537 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002538 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002539 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2540 }
2541
2542 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2543 chunk_appendf(&trash,
2544 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2545 uri->uri_prefix,
2546 "",
2547 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2548 scope_txt);
2549 else
2550 chunk_appendf(&trash,
2551 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2552 uri->uri_prefix,
2553 ";up",
2554 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2555 scope_txt);
2556
2557 if (uri->refresh > 0) {
2558 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2559 chunk_appendf(&trash,
2560 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2561 uri->uri_prefix,
2562 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2563 "",
2564 scope_txt);
2565 else
2566 chunk_appendf(&trash,
2567 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2568 uri->uri_prefix,
2569 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2570 ";norefresh",
2571 scope_txt);
2572 }
2573
2574 chunk_appendf(&trash,
2575 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2576 uri->uri_prefix,
2577 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2578 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2579 scope_txt);
2580
2581 chunk_appendf(&trash,
2582 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2583 uri->uri_prefix,
2584 (uri->refresh > 0) ? ";norefresh" : "",
2585 scope_txt);
2586
2587 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002588 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2589 uri->uri_prefix,
2590 (uri->refresh > 0) ? ";norefresh" : "",
2591 scope_txt, uri->uri_prefix);
2592
2593 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002594 "</ul></td>"
2595 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2596 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2597 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2598 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2599 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2600 "</ul>"
2601 "</td>"
2602 "</tr></table>\n"
2603 ""
2604 );
2605
2606 if (appctx->ctx.stats.st_code) {
2607 switch (appctx->ctx.stats.st_code) {
2608 case STAT_STATUS_DONE:
2609 chunk_appendf(&trash,
2610 "<p><div class=active_up>"
2611 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2612 "Action processed successfully."
2613 "</div>\n", uri->uri_prefix,
2614 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2615 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2616 scope_txt);
2617 break;
2618 case STAT_STATUS_NONE:
2619 chunk_appendf(&trash,
2620 "<p><div class=active_going_down>"
2621 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2622 "Nothing has changed."
2623 "</div>\n", uri->uri_prefix,
2624 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2625 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2626 scope_txt);
2627 break;
2628 case STAT_STATUS_PART:
2629 chunk_appendf(&trash,
2630 "<p><div class=active_going_down>"
2631 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2632 "Action partially processed.<br>"
2633 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2634 "</div>\n", uri->uri_prefix,
2635 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2636 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2637 scope_txt);
2638 break;
2639 case STAT_STATUS_ERRP:
2640 chunk_appendf(&trash,
2641 "<p><div class=active_down>"
2642 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2643 "Action not processed because of invalid parameters."
2644 "<ul>"
2645 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002646 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002647 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2648 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2649 "</ul>"
2650 "</div>\n", uri->uri_prefix,
2651 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2652 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2653 scope_txt);
2654 break;
2655 case STAT_STATUS_EXCD:
2656 chunk_appendf(&trash,
2657 "<p><div class=active_down>"
2658 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2659 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2660 "You should retry with less servers at a time.</b>"
2661 "</div>\n", uri->uri_prefix,
2662 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2663 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2664 scope_txt);
2665 break;
2666 case STAT_STATUS_DENY:
2667 chunk_appendf(&trash,
2668 "<p><div class=active_down>"
2669 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2670 "<b>Action denied.</b>"
2671 "</div>\n", uri->uri_prefix,
2672 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2673 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2674 scope_txt);
2675 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002676 case STAT_STATUS_IVAL:
2677 chunk_appendf(&trash,
2678 "<p><div class=active_down>"
2679 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2680 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2681 "</div>\n", uri->uri_prefix,
2682 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2683 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2684 scope_txt);
2685 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002686 default:
2687 chunk_appendf(&trash,
2688 "<p><div class=active_no_check>"
2689 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2690 "Unexpected result."
2691 "</div>\n", uri->uri_prefix,
2692 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2693 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2694 scope_txt);
2695 }
2696 chunk_appendf(&trash, "<p>\n");
2697 }
2698}
2699
2700/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2701 * for clearing the trash if needed.
2702 */
2703static void stats_dump_html_end()
2704{
2705 chunk_appendf(&trash, "</body></html>\n");
2706}
2707
Simon Horman05ee2132017-01-04 09:37:25 +01002708/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2709 * for clearing it if needed.
2710 */
2711static void stats_dump_json_header()
2712{
2713 chunk_strcat(&trash, "[");
2714}
2715
2716
2717/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2718 * for clearing the trash if needed.
2719 */
2720static void stats_dump_json_end()
2721{
2722 chunk_strcat(&trash, "]");
2723}
2724
William Lallemand74c24fb2016-11-21 17:18:36 +01002725/* This function dumps statistics onto the stream interface's read buffer in
2726 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2727 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2728 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2729 * and the stream must be closed, or -1 in case of any error. This function is
2730 * used by both the CLI and the HTTP handlers.
2731 */
Christopher Fauletef779222018-10-31 08:47:01 +01002732static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2733 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002734{
2735 struct appctx *appctx = __objt_appctx(si->end);
2736 struct channel *rep = si_ic(si);
2737 struct proxy *px;
2738
2739 chunk_reset(&trash);
2740
2741 switch (appctx->st2) {
2742 case STAT_ST_INIT:
2743 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2744 /* fall through */
2745
2746 case STAT_ST_HEAD:
2747 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002748 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002749 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2750 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002751 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002752 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002753 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2754 stats_dump_csv_header();
2755
Christopher Fauletef779222018-10-31 08:47:01 +01002756 if (!stats_putchk(rep, htx, &trash))
2757 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002758
Christopher Faulet6338a082019-09-09 15:50:54 +02002759 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2760 appctx->st2 = STAT_ST_FIN;
2761 return 1;
2762 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002763 appctx->st2 = STAT_ST_INFO;
2764 /* fall through */
2765
2766 case STAT_ST_INFO:
2767 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2768 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002769 if (!stats_putchk(rep, htx, &trash))
2770 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002771 }
2772
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002773 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002774 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2775 appctx->st2 = STAT_ST_LIST;
2776 /* fall through */
2777
2778 case STAT_ST_LIST:
2779 /* dump proxies */
2780 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002781 if (htx) {
2782 if (htx_almost_full(htx))
2783 goto full;
2784 }
2785 else {
2786 if (buffer_almost_full(&rep->buf))
2787 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002788 }
2789
2790 px = appctx->ctx.stats.px;
2791 /* skip the disabled proxies, global frontend and non-networked ones */
2792 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002793 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002794 return 0;
2795
2796 appctx->ctx.stats.px = px->next;
2797 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2798 }
2799 /* here, we just have reached the last proxy */
2800
2801 appctx->st2 = STAT_ST_END;
2802 /* fall through */
2803
2804 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002805 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2806 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2807 stats_dump_html_end();
2808 else
2809 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002810 if (!stats_putchk(rep, htx, &trash))
2811 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002812 }
2813
2814 appctx->st2 = STAT_ST_FIN;
2815 /* fall through */
2816
2817 case STAT_ST_FIN:
2818 return 1;
2819
2820 default:
2821 /* unknown state ! */
2822 appctx->st2 = STAT_ST_FIN;
2823 return -1;
2824 }
Christopher Fauletef779222018-10-31 08:47:01 +01002825
2826 full:
2827 si_rx_room_blk(si);
2828 return 0;
2829
William Lallemand74c24fb2016-11-21 17:18:36 +01002830}
2831
2832/* We reached the stats page through a POST request. The appctx is
2833 * expected to have already been allocated by the caller.
2834 * Parse the posted data and enable/disable servers if necessary.
2835 * Returns 1 if request was parsed or zero if it needs more data.
2836 */
2837static int stats_process_http_post(struct stream_interface *si)
2838{
2839 struct stream *s = si_strm(si);
2840 struct appctx *appctx = objt_appctx(si->end);
2841
2842 struct proxy *px = NULL;
2843 struct server *sv = NULL;
2844
2845 char key[LINESIZE];
2846 int action = ST_ADM_ACTION_NONE;
2847 int reprocess = 0;
2848
2849 int total_servers = 0;
2850 int altered_servers = 0;
2851
2852 char *first_param, *cur_param, *next_param, *end_params;
2853 char *st_cur_param = NULL;
2854 char *st_next_param = NULL;
2855
Christopher Fauleta3618372018-12-13 21:59:56 +01002856 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002857
Christopher Fauletb7f88902019-07-15 21:56:43 +02002858 struct htx *htx = htxbuf(&s->req.buf);
2859 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002860
Christopher Fauletb7f88902019-07-15 21:56:43 +02002861 /* we need more data */
2862 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2863 /* check if we can receive more */
2864 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2865 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2866 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002867 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002868 goto wait;
2869 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002870
Christopher Fauletb7f88902019-07-15 21:56:43 +02002871 /* The request was fully received. Copy data */
2872 blk = htx_get_head_blk(htx);
2873 while (blk) {
2874 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002875
Christopher Fauletb7f88902019-07-15 21:56:43 +02002876 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2877 break;
2878 if (type == HTX_BLK_DATA) {
2879 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002880
Christopher Fauletb7f88902019-07-15 21:56:43 +02002881 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002882 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2883 goto out;
2884 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002885 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002886 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002887 }
2888
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002889 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002890 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002891 cur_param = next_param = end_params;
2892 *end_params = '\0';
2893
2894 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2895
2896 /*
2897 * Parse the parameters in reverse order to only store the last value.
2898 * From the html form, the backend and the action are at the end.
2899 */
2900 while (cur_param > first_param) {
2901 char *value;
2902 int poffset, plen;
2903
2904 cur_param--;
2905
2906 if ((*cur_param == '&') || (cur_param == first_param)) {
2907 reprocess_servers:
2908 /* Parse the key */
2909 poffset = (cur_param != first_param ? 1 : 0);
2910 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2911 if ((plen > 0) && (plen <= sizeof(key))) {
2912 strncpy(key, cur_param + poffset, plen);
2913 key[plen - 1] = '\0';
2914 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002915 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002916 goto out;
2917 }
2918
2919 /* Parse the value */
2920 value = key;
2921 while (*value != '\0' && *value != '=') {
2922 value++;
2923 }
2924 if (*value == '=') {
2925 /* Ok, a value is found, we can mark the end of the key */
2926 *value++ = '\0';
2927 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002928 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002929 break;
2930
2931 /* Now we can check the key to see what to do */
2932 if (!px && (strcmp(key, "b") == 0)) {
2933 if ((px = proxy_be_by_name(value)) == NULL) {
2934 /* the backend name is unknown or ambiguous (duplicate names) */
2935 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2936 goto out;
2937 }
2938 }
2939 else if (!action && (strcmp(key, "action") == 0)) {
2940 if (strcmp(value, "ready") == 0) {
2941 action = ST_ADM_ACTION_READY;
2942 }
2943 else if (strcmp(value, "drain") == 0) {
2944 action = ST_ADM_ACTION_DRAIN;
2945 }
2946 else if (strcmp(value, "maint") == 0) {
2947 action = ST_ADM_ACTION_MAINT;
2948 }
2949 else if (strcmp(value, "shutdown") == 0) {
2950 action = ST_ADM_ACTION_SHUTDOWN;
2951 }
2952 else if (strcmp(value, "dhlth") == 0) {
2953 action = ST_ADM_ACTION_DHLTH;
2954 }
2955 else if (strcmp(value, "ehlth") == 0) {
2956 action = ST_ADM_ACTION_EHLTH;
2957 }
2958 else if (strcmp(value, "hrunn") == 0) {
2959 action = ST_ADM_ACTION_HRUNN;
2960 }
2961 else if (strcmp(value, "hnolb") == 0) {
2962 action = ST_ADM_ACTION_HNOLB;
2963 }
2964 else if (strcmp(value, "hdown") == 0) {
2965 action = ST_ADM_ACTION_HDOWN;
2966 }
2967 else if (strcmp(value, "dagent") == 0) {
2968 action = ST_ADM_ACTION_DAGENT;
2969 }
2970 else if (strcmp(value, "eagent") == 0) {
2971 action = ST_ADM_ACTION_EAGENT;
2972 }
2973 else if (strcmp(value, "arunn") == 0) {
2974 action = ST_ADM_ACTION_ARUNN;
2975 }
2976 else if (strcmp(value, "adown") == 0) {
2977 action = ST_ADM_ACTION_ADOWN;
2978 }
2979 /* else these are the old supported methods */
2980 else if (strcmp(value, "disable") == 0) {
2981 action = ST_ADM_ACTION_DISABLE;
2982 }
2983 else if (strcmp(value, "enable") == 0) {
2984 action = ST_ADM_ACTION_ENABLE;
2985 }
2986 else if (strcmp(value, "stop") == 0) {
2987 action = ST_ADM_ACTION_STOP;
2988 }
2989 else if (strcmp(value, "start") == 0) {
2990 action = ST_ADM_ACTION_START;
2991 }
2992 else {
2993 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2994 goto out;
2995 }
2996 }
2997 else if (strcmp(key, "s") == 0) {
2998 if (!(px && action)) {
2999 /*
3000 * Indicates that we'll need to reprocess the parameters
3001 * as soon as backend and action are known
3002 */
3003 if (!reprocess) {
3004 st_cur_param = cur_param;
3005 st_next_param = next_param;
3006 }
3007 reprocess = 1;
3008 }
3009 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003010 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003011 switch (action) {
3012 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003013 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003014 altered_servers++;
3015 total_servers++;
3016 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3017 }
3018 break;
3019 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003020 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003021 altered_servers++;
3022 total_servers++;
3023 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3024 }
3025 break;
3026 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003027 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003028 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3029 altered_servers++;
3030 total_servers++;
3031 }
3032 break;
3033 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003034 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003035 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3036 altered_servers++;
3037 total_servers++;
3038 }
3039 break;
3040 case ST_ADM_ACTION_DHLTH:
3041 if (sv->check.state & CHK_ST_CONFIGURED) {
3042 sv->check.state &= ~CHK_ST_ENABLED;
3043 altered_servers++;
3044 total_servers++;
3045 }
3046 break;
3047 case ST_ADM_ACTION_EHLTH:
3048 if (sv->check.state & CHK_ST_CONFIGURED) {
3049 sv->check.state |= CHK_ST_ENABLED;
3050 altered_servers++;
3051 total_servers++;
3052 }
3053 break;
3054 case ST_ADM_ACTION_HRUNN:
3055 if (!(sv->track)) {
3056 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003057 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003058 altered_servers++;
3059 total_servers++;
3060 }
3061 break;
3062 case ST_ADM_ACTION_HNOLB:
3063 if (!(sv->track)) {
3064 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003065 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003066 altered_servers++;
3067 total_servers++;
3068 }
3069 break;
3070 case ST_ADM_ACTION_HDOWN:
3071 if (!(sv->track)) {
3072 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003073 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003074 altered_servers++;
3075 total_servers++;
3076 }
3077 break;
3078 case ST_ADM_ACTION_DAGENT:
3079 if (sv->agent.state & CHK_ST_CONFIGURED) {
3080 sv->agent.state &= ~CHK_ST_ENABLED;
3081 altered_servers++;
3082 total_servers++;
3083 }
3084 break;
3085 case ST_ADM_ACTION_EAGENT:
3086 if (sv->agent.state & CHK_ST_CONFIGURED) {
3087 sv->agent.state |= CHK_ST_ENABLED;
3088 altered_servers++;
3089 total_servers++;
3090 }
3091 break;
3092 case ST_ADM_ACTION_ARUNN:
3093 if (sv->agent.state & CHK_ST_ENABLED) {
3094 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003095 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003096 altered_servers++;
3097 total_servers++;
3098 }
3099 break;
3100 case ST_ADM_ACTION_ADOWN:
3101 if (sv->agent.state & CHK_ST_ENABLED) {
3102 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003103 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003104 altered_servers++;
3105 total_servers++;
3106 }
3107 break;
3108 case ST_ADM_ACTION_READY:
3109 srv_adm_set_ready(sv);
3110 altered_servers++;
3111 total_servers++;
3112 break;
3113 case ST_ADM_ACTION_DRAIN:
3114 srv_adm_set_drain(sv);
3115 altered_servers++;
3116 total_servers++;
3117 break;
3118 case ST_ADM_ACTION_MAINT:
3119 srv_adm_set_maint(sv);
3120 altered_servers++;
3121 total_servers++;
3122 break;
3123 case ST_ADM_ACTION_SHUTDOWN:
3124 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003125 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003126 altered_servers++;
3127 total_servers++;
3128 }
3129 break;
3130 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003131 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003132 } else {
3133 /* the server name is unknown or ambiguous (duplicate names) */
3134 total_servers++;
3135 }
3136 }
3137 if (reprocess && px && action) {
3138 /* Now, we know the backend and the action chosen by the user.
3139 * We can safely restart from the first server parameter
3140 * to reprocess them
3141 */
3142 cur_param = st_cur_param;
3143 next_param = st_next_param;
3144 reprocess = 0;
3145 goto reprocess_servers;
3146 }
3147
3148 next_param = cur_param;
3149 }
3150 }
3151
3152 if (total_servers == 0) {
3153 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3154 }
3155 else if (altered_servers == 0) {
3156 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3157 }
3158 else if (altered_servers == total_servers) {
3159 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3160 }
3161 else {
3162 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3163 }
3164 out:
3165 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003166 wait:
3167 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3168 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003169}
3170
3171
Christopher Fauletb7f88902019-07-15 21:56:43 +02003172static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003173{
3174 struct stream *s = si_strm(si);
3175 struct uri_auth *uri = s->be->uri_auth;
3176 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003177 struct htx_sl *sl;
3178 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003179
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003180 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);
3181 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3182 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003183 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003184 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003185
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003186 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003187 goto full;
3188 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3189 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3190 goto full;
3191 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003192 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3193 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3194 goto full;
3195 }
Christopher Fauletef779222018-10-31 08:47:01 +01003196 else {
3197 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3198 goto full;
3199 }
3200
3201 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3202 const char *refresh = U2A(uri->refresh);
3203 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3204 goto full;
3205 }
3206
3207 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3208 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3209 goto full;
3210 }
3211
3212 if (!htx_add_endof(htx, HTX_BLK_EOH))
3213 goto full;
3214
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003215 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003216 return 1;
3217
3218 full:
3219 htx_reset(htx);
3220 si_rx_room_blk(si);
3221 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003222}
3223
Christopher Fauletef779222018-10-31 08:47:01 +01003224
Christopher Fauletb7f88902019-07-15 21:56:43 +02003225static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003226{
3227 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3228 struct stream *s = si_strm(si);
3229 struct uri_auth *uri = s->be->uri_auth;
3230 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003231 struct htx_sl *sl;
3232 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003233
3234 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3235 scope_txt[0] = 0;
3236 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003237 const char *scope_ptr = stats_scope_ptr(appctx, si);
3238
Christopher Fauletef779222018-10-31 08:47:01 +01003239 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003240 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003241 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3242 }
3243
3244 /* We don't want to land on the posted stats page because a refresh will
3245 * repost the data. We don't want this to happen on accident so we redirect
3246 * the browse to the stats page with a GET.
3247 */
3248 chunk_printf(&trash, "%s;st=%s%s%s%s",
3249 uri->uri_prefix,
3250 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3251 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3252 stat_status_codes[appctx->ctx.stats.st_code]) ?
3253 stat_status_codes[appctx->ctx.stats.st_code] :
3254 stat_status_codes[STAT_STATUS_UNKN],
3255 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3256 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3257 scope_txt);
3258
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003259 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3260 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3261 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003262 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003263 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003264
3265 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003266 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3267 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3268 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3269 goto full;
3270
3271 if (!htx_add_endof(htx, HTX_BLK_EOH))
3272 goto full;
3273
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003274 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003275 return 1;
3276
3277full:
3278 htx_reset(htx);
3279 si_rx_room_blk(si);
3280 return 0;
3281}
William Lallemand74c24fb2016-11-21 17:18:36 +01003282
Simon Horman05ee2132017-01-04 09:37:25 +01003283
William Lallemand74c24fb2016-11-21 17:18:36 +01003284/* This I/O handler runs as an applet embedded in a stream interface. It is
3285 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3286 * appctx->st0 contains the operation in progress (dump, done). The handler
3287 * automatically unregisters itself once transfer is complete.
3288 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003289static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003290{
3291 struct stream_interface *si = appctx->owner;
3292 struct stream *s = si_strm(si);
3293 struct channel *req = si_oc(si);
3294 struct channel *res = si_ic(si);
3295 struct htx *req_htx, *res_htx;
3296
3297 res_htx = htx_from_buf(&res->buf);
3298
3299 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3300 goto out;
3301
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003302 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003303 if (!b_size(&res->buf)) {
3304 si_rx_room_blk(si);
3305 goto out;
3306 }
3307
3308 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003309 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3310 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003311
3312 /* all states are processed in sequence */
3313 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003314 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003315 if (s->txn->meth == HTTP_METH_HEAD)
3316 appctx->st0 = STAT_HTTP_DONE;
3317 else
3318 appctx->st0 = STAT_HTTP_DUMP;
3319 }
3320 }
3321
3322 if (appctx->st0 == STAT_HTTP_DUMP) {
3323 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3324 appctx->st0 = STAT_HTTP_DONE;
3325 }
3326
3327 if (appctx->st0 == STAT_HTTP_POST) {
3328 if (stats_process_http_post(si))
3329 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003330 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003331 appctx->st0 = STAT_HTTP_DONE;
3332 }
3333
3334 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003335 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003336 appctx->st0 = STAT_HTTP_DONE;
3337 }
3338
3339 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003340 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003341 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3342 si_rx_room_blk(si);
3343 goto out;
3344 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003345 channel_add_input(&s->res, 1);
3346 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003347 }
3348
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003349 if (appctx->st0 == STAT_HTTP_END) {
3350 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003351 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003352 si_shutr(si);
3353 }
3354
3355 /* eat the whole request */
3356 if (co_data(req)) {
3357 req_htx = htx_from_buf(&req->buf);
3358 co_htx_skip(req, req_htx, co_data(req));
3359 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003360 }
3361 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003362
Christopher Fauletef779222018-10-31 08:47:01 +01003363 out:
3364 /* we have left the request in the buffer for the case where we
3365 * process a POST, and this automatically re-enables activity on
3366 * read. It's better to indicate that we want to stop reading when
3367 * we're sending, so that we know there's at most one direction
3368 * deciding to wake the applet up. It saves it from looping when
3369 * emitting large blocks into small TCP windows.
3370 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003371 htx_to_buf(res_htx, &res->buf);
3372 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003373 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003374}
3375
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003376/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003377static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003378 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003379{
3380 int field;
3381
3382 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3383 if (!field_format(info, field))
3384 continue;
3385
Willy Tarreaueaa55372019-10-09 07:39:11 +02003386 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003387 return 0;
3388 if (!stats_emit_raw_data_field(out, &info[field]))
3389 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003390 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3391 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003392 if (!chunk_strcat(out, "\n"))
3393 return 0;
3394 }
3395 return 1;
3396}
3397
3398/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003399static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003400 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003401{
3402 int field;
3403
3404 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3405 if (!field_format(info, field))
3406 continue;
3407
Willy Tarreaueaa55372019-10-09 07:39:11 +02003408 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 +01003409 return 0;
3410 if (!stats_emit_field_tags(out, &info[field], ':'))
3411 return 0;
3412 if (!stats_emit_typed_data_field(out, &info[field]))
3413 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003414 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3415 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003416 if (!chunk_strcat(out, "\n"))
3417 return 0;
3418 }
3419 return 1;
3420}
3421
3422/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003423 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003424 * INF_TOTAL_FIELDS. If this length is less then this value, the
3425 * function returns 0, otherwise, it returns 1.
3426 */
3427int stats_fill_info(struct field *info, int len)
3428{
3429 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003430 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003431
3432#ifdef USE_OPENSSL
3433 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3434 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3435 int ssl_reuse = 0;
3436
3437 if (ssl_key_rate < ssl_sess_rate) {
3438 /* count the ssl reuse ratio and avoid overflows in both directions */
3439 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3440 }
3441#endif
3442
3443 if (len < INF_TOTAL_FIELDS)
3444 return 0;
3445
3446 chunk_reset(out);
3447 memset(info, 0, sizeof(*info) * len);
3448
3449 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003450 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3451 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003452
Yves Lafon95317282018-02-26 11:10:37 +01003453 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003454 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3455 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3456 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3457
3458 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3459 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3460
3461 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3462 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3463 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3464 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3465 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3466 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3467 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3468 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3469 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3470 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3471 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3472 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3473#ifdef USE_OPENSSL
3474 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3475 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3476 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3477#endif
3478 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3479 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3480 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3481 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3482 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3483 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3484 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3485 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3486 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3487
3488#ifdef USE_OPENSSL
3489 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3490 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3491 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3492 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3493 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3494 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3495 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3496 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3497 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3498 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3499#endif
3500 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3501 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3502 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3503#ifdef USE_ZLIB
3504 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3505 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3506#endif
3507 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003508 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003509 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003510 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3511 if (global.desc)
3512 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003513 info[INF_STOPPING] = mkf_u32(0, stopping);
3514 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003515 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003516 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003517 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003518 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003519 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003520 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003521 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003522 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003523 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003524 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 +02003525 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003526
3527 return 1;
3528}
3529
3530/* This function dumps information onto the stream interface's read buffer.
3531 * It returns 0 as long as it does not complete, non-zero upon completion.
3532 * No state is used.
3533 */
3534static int stats_dump_info_to_buffer(struct stream_interface *si)
3535{
3536 struct appctx *appctx = __objt_appctx(si->end);
3537
3538 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3539 return 0;
3540
3541 chunk_reset(&trash);
3542
3543 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003544 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003545 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003546 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003547 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003548 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003549
Willy Tarreau06d80a92017-10-19 14:32:15 +02003550 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003551 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003552 return 0;
3553 }
3554
3555 return 1;
3556}
3557
Simon Horman6f6bb382017-01-04 09:37:26 +01003558/* This function dumps the schema onto the stream interface's read buffer.
3559 * It returns 0 as long as it does not complete, non-zero upon completion.
3560 * No state is used.
3561 *
3562 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3563 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3564 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003565static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003566{
3567
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003568 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003569
3570 chunk_strcat(out,
3571 "{"
3572 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3573 "\"oneOf\":["
3574 "{"
3575 "\"title\":\"Info\","
3576 "\"type\":\"array\","
3577 "\"items\":{"
3578 "\"properties\":{"
3579 "\"title\":\"InfoItem\","
3580 "\"type\":\"object\","
3581 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3582 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3583 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3584 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3585 "},"
3586 "\"required\":[\"field\",\"processNum\",\"tags\","
3587 "\"value\"]"
3588 "}"
3589 "},"
3590 "{"
3591 "\"title\":\"Stat\","
3592 "\"type\":\"array\","
3593 "\"items\":{"
3594 "\"title\":\"InfoItem\","
3595 "\"type\":\"object\","
3596 "\"properties\":{"
3597 "\"objType\":{"
3598 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3599 "\"Server\",\"Unknown\"]"
3600 "},"
3601 "\"proxyId\":{"
3602 "\"type\":\"integer\","
3603 "\"minimum\":0"
3604 "},"
3605 "\"id\":{"
3606 "\"type\":\"integer\","
3607 "\"minimum\":0"
3608 "},"
3609 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3610 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3611 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3612 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3613 "},"
3614 "\"required\":[\"objType\",\"proxyId\",\"id\","
3615 "\"field\",\"processNum\",\"tags\","
3616 "\"value\"]"
3617 "}"
3618 "},"
3619 "{"
3620 "\"title\":\"Error\","
3621 "\"type\":\"object\","
3622 "\"properties\":{"
3623 "\"errorStr\":{"
3624 "\"type\":\"string\""
3625 "},"
3626 "\"required\":[\"errorStr\"]"
3627 "}"
3628 "}"
3629 "],"
3630 "\"definitions\":{"
3631 "\"field\":{"
3632 "\"type\":\"object\","
3633 "\"pos\":{"
3634 "\"type\":\"integer\","
3635 "\"minimum\":0"
3636 "},"
3637 "\"name\":{"
3638 "\"type\":\"string\""
3639 "},"
3640 "\"required\":[\"pos\",\"name\"]"
3641 "},"
3642 "\"processNum\":{"
3643 "\"type\":\"integer\","
3644 "\"minimum\":1"
3645 "},"
3646 "\"tags\":{"
3647 "\"type\":\"object\","
3648 "\"origin\":{"
3649 "\"type\":\"string\","
3650 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3651 "\"Config\",\"Product\",\"Unknown\"]"
3652 "},"
3653 "\"nature\":{"
3654 "\"type\":\"string\","
3655 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3656 "\"Rate\",\"Counter\",\"Duration\","
3657 "\"Age\",\"Time\",\"Name\",\"Output\","
3658 "\"Avg\", \"Unknown\"]"
3659 "},"
3660 "\"scope\":{"
3661 "\"type\":\"string\","
3662 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3663 "\"System\",\"Unknown\"]"
3664 "},"
3665 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3666 "},"
3667 "\"typedValue\":{"
3668 "\"type\":\"object\","
3669 "\"oneOf\":["
3670 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3671 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3672 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3673 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3674 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3675 "],"
3676 "\"definitions\":{"
3677 "\"s32Value\":{"
3678 "\"properties\":{"
3679 "\"type\":{"
3680 "\"type\":\"string\","
3681 "\"enum\":[\"s32\"]"
3682 "},"
3683 "\"value\":{"
3684 "\"type\":\"integer\","
3685 "\"minimum\":-2147483648,"
3686 "\"maximum\":2147483647"
3687 "}"
3688 "},"
3689 "\"required\":[\"type\",\"value\"]"
3690 "},"
3691 "\"s64Value\":{"
3692 "\"properties\":{"
3693 "\"type\":{"
3694 "\"type\":\"string\","
3695 "\"enum\":[\"s64\"]"
3696 "},"
3697 "\"value\":{"
3698 "\"type\":\"integer\","
3699 "\"minimum\":-9007199254740991,"
3700 "\"maximum\":9007199254740991"
3701 "}"
3702 "},"
3703 "\"required\":[\"type\",\"value\"]"
3704 "},"
3705 "\"u32Value\":{"
3706 "\"properties\":{"
3707 "\"type\":{"
3708 "\"type\":\"string\","
3709 "\"enum\":[\"u32\"]"
3710 "},"
3711 "\"value\":{"
3712 "\"type\":\"integer\","
3713 "\"minimum\":0,"
3714 "\"maximum\":4294967295"
3715 "}"
3716 "},"
3717 "\"required\":[\"type\",\"value\"]"
3718 "},"
3719 "\"u64Value\":{"
3720 "\"properties\":{"
3721 "\"type\":{"
3722 "\"type\":\"string\","
3723 "\"enum\":[\"u64\"]"
3724 "},"
3725 "\"value\":{"
3726 "\"type\":\"integer\","
3727 "\"minimum\":0,"
3728 "\"maximum\":9007199254740991"
3729 "}"
3730 "},"
3731 "\"required\":[\"type\",\"value\"]"
3732 "},"
3733 "\"strValue\":{"
3734 "\"properties\":{"
3735 "\"type\":{"
3736 "\"type\":\"string\","
3737 "\"enum\":[\"str\"]"
3738 "},"
3739 "\"value\":{\"type\":\"string\"}"
3740 "},"
3741 "\"required\":[\"type\",\"value\"]"
3742 "},"
3743 "\"unknownValue\":{"
3744 "\"properties\":{"
3745 "\"type\":{"
3746 "\"type\":\"integer\","
3747 "\"minimum\":0"
3748 "},"
3749 "\"value\":{"
3750 "\"type\":\"string\","
3751 "\"enum\":[\"unknown\"]"
3752 "}"
3753 "},"
3754 "\"required\":[\"type\",\"value\"]"
3755 "}"
3756 "}"
3757 "}"
3758 "}"
3759 "}");
3760
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003761 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003762 chunk_reset(out);
3763 chunk_appendf(out,
3764 "{\"errorStr\":\"output buffer too short\"}");
3765 }
3766}
3767
3768/* This function dumps the schema onto the stream interface's read buffer.
3769 * It returns 0 as long as it does not complete, non-zero upon completion.
3770 * No state is used.
3771 */
3772static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3773{
3774 chunk_reset(&trash);
3775
3776 stats_dump_json_schema(&trash);
3777
Willy Tarreau06d80a92017-10-19 14:32:15 +02003778 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003779 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003780 return 0;
3781 }
3782
3783 return 1;
3784}
3785
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003786static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003787{
3788 struct proxy *px;
3789 struct server *sv;
3790 struct listener *li;
3791 int clrall = 0;
3792
3793 if (strcmp(args[2], "all") == 0)
3794 clrall = 1;
3795
3796 /* check permissions */
3797 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3798 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3799 return 1;
3800
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003801 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003802 if (clrall) {
3803 memset(&px->be_counters, 0, sizeof(px->be_counters));
3804 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3805 }
3806 else {
3807 px->be_counters.conn_max = 0;
3808 px->be_counters.p.http.rps_max = 0;
3809 px->be_counters.sps_max = 0;
3810 px->be_counters.cps_max = 0;
3811 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003812 px->be_counters.qtime_max = 0;
3813 px->be_counters.ctime_max = 0;
3814 px->be_counters.dtime_max = 0;
3815 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003816
3817 px->fe_counters.conn_max = 0;
3818 px->fe_counters.p.http.rps_max = 0;
3819 px->fe_counters.sps_max = 0;
3820 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003821 }
3822
3823 for (sv = px->srv; sv; sv = sv->next)
3824 if (clrall)
3825 memset(&sv->counters, 0, sizeof(sv->counters));
3826 else {
3827 sv->counters.cur_sess_max = 0;
3828 sv->counters.nbpend_max = 0;
3829 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003830 sv->counters.qtime_max = 0;
3831 sv->counters.ctime_max = 0;
3832 sv->counters.dtime_max = 0;
3833 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003834 }
3835
3836 list_for_each_entry(li, &px->conf.listeners, by_fe)
3837 if (li->counters) {
3838 if (clrall)
3839 memset(li->counters, 0, sizeof(*li->counters));
3840 else
3841 li->counters->conn_max = 0;
3842 }
3843 }
3844
3845 global.cps_max = 0;
3846 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003847 global.ssl_max = 0;
3848 global.ssl_fe_keys_max = 0;
3849 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003850
3851 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003852 return 1;
3853}
3854
3855
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003856static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003857{
Willy Tarreau2f397382019-10-09 11:43:59 +02003858 int arg = 2;
3859
Willy Tarreaud25fc792016-12-16 12:33:47 +01003860 appctx->ctx.stats.scope_str = 0;
3861 appctx->ctx.stats.scope_len = 0;
3862 appctx->ctx.stats.flags = 0;
3863
Willy Tarreau2f397382019-10-09 11:43:59 +02003864 while (*args[arg]) {
3865 if (strcmp(args[arg], "typed") == 0)
3866 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3867 else if (strcmp(args[arg], "json") == 0)
3868 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3869 else if (strcmp(args[arg], "desc") == 0)
3870 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3871 arg++;
3872 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003873 return 0;
3874}
3875
3876
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003877static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003878{
Willy Tarreau2f397382019-10-09 11:43:59 +02003879 int arg = 2;
3880
Willy Tarreaud25fc792016-12-16 12:33:47 +01003881 appctx->ctx.stats.scope_str = 0;
3882 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003883 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3884
3885 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3886 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003887
Willy Tarreau2f397382019-10-09 11:43:59 +02003888 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003889 struct proxy *px;
3890
Willy Tarreau2f397382019-10-09 11:43:59 +02003891 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003892 if (px)
3893 appctx->ctx.stats.iid = px->uuid;
3894 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003895 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003896
Willy Tarreau9d008692019-08-09 11:21:01 +02003897 if (!appctx->ctx.stats.iid)
3898 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003899
Willy Tarreau2b812e22016-11-22 16:18:05 +01003900 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003901 appctx->ctx.stats.type = atoi(args[arg+1]);
3902 appctx->ctx.stats.sid = atoi(args[arg+2]);
3903 arg += 3;
3904 }
3905
3906 while (*args[arg]) {
3907 if (strcmp(args[arg], "typed") == 0)
3908 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3909 else if (strcmp(args[arg], "json") == 0)
3910 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3911 else if (strcmp(args[arg], "desc") == 0)
3912 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3913 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003914 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003915
Willy Tarreau2b812e22016-11-22 16:18:05 +01003916 return 0;
3917}
3918
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003919static int cli_io_handler_dump_info(struct appctx *appctx)
3920{
3921 return stats_dump_info_to_buffer(appctx->owner);
3922}
3923
Willy Tarreau2b812e22016-11-22 16:18:05 +01003924/* This I/O handler runs as an applet embedded in a stream interface. It is
3925 * used to send raw stats over a socket.
3926 */
3927static int cli_io_handler_dump_stat(struct appctx *appctx)
3928{
Christopher Fauletef779222018-10-31 08:47:01 +01003929 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003930}
3931
Simon Horman6f6bb382017-01-04 09:37:26 +01003932static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3933{
3934 return stats_dump_json_schema_to_buffer(appctx->owner);
3935}
3936
Willy Tarreau2b812e22016-11-22 16:18:05 +01003937/* register cli keywords */
3938static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003939 { { "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 +02003940 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3941 { { "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 +01003942 { { "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 +01003943 {{},}
3944}};
3945
Willy Tarreau0108d902018-11-25 19:14:37 +01003946INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3947
William Lallemand74c24fb2016-11-21 17:18:36 +01003948struct applet http_stats_applet = {
3949 .obj_type = OBJ_TYPE_APPLET,
3950 .name = "<STATS>", /* used for logging */
3951 .fct = http_stats_io_handler,
3952 .release = NULL,
3953};
3954
William Lallemand74c24fb2016-11-21 17:18:36 +01003955/*
3956 * Local variables:
3957 * c-indent-level: 8
3958 * c-basic-offset: 8
3959 * End:
3960 */