blob: 72268742e580767adc4c705f0b8fb3821804d32a [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020028#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020035#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020036#include <haproxy/debug.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020037#include <haproxy/dns.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020044#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020045#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020046#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020047#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020048#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020049#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020050#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020051#include <haproxy/pipe.h>
52#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020053#include <haproxy/proxy.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020054#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020055#include <haproxy/session.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020056#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020057#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020058#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020059#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020060#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020061#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020062#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020063#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020064#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020065#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066
William Lallemand74c24fb2016-11-21 17:18:36 +010067
Christopher Faulet2b9b6782019-02-27 16:42:58 +010068/* status codes available for the stats admin page (strictly 4 chars length) */
69const char *stat_status_codes[STAT_STATUS_SIZE] = {
70 [STAT_STATUS_DENY] = "DENY",
71 [STAT_STATUS_DONE] = "DONE",
72 [STAT_STATUS_ERRP] = "ERRP",
73 [STAT_STATUS_EXCD] = "EXCD",
74 [STAT_STATUS_NONE] = "NONE",
75 [STAT_STATUS_PART] = "PART",
76 [STAT_STATUS_UNKN] = "UNKN",
77 [STAT_STATUS_IVAL] = "IVAL",
78};
79
Willy Tarreau0baac8c2016-11-22 16:36:53 +010080/* These are the field names for each INF_* field position. Please pay attention
81 * to always use the exact same name except that the strings for new names must
82 * be lower case or CamelCase while the enum entries must be upper case.
83 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020084const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020085 [INF_NAME] = { .name = "Name", .desc = "Product name" },
86 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
87 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
88 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
89 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
90 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
91 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
92 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
93 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
94 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
95 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
96 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
97 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
98 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
99 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
100 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
101 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
102 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
103 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
104 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
105 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
106 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
107 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
108 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
109 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
110 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
111 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
112 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
113 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
114 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
115 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
116 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
117 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
118 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
119 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
120 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
121 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
122 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
123 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
124 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
125 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
126 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
127 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
128 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
129 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
130 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
131 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
132 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
133 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
134 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
135 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
136 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
137 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
138 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
139 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
140 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
141 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
142 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
143 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
144 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
145 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
146 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
147 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200148 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100149};
150
Willy Tarreaueaa55372019-10-09 07:39:11 +0200151const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200152 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
153 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
154 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
155 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
156 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
157 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
158 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
159 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
160 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
161 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
162 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
163 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
164 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
165 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
166 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
167 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
168 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
169 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
170 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
171 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
172 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
173 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
174 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
175 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
176 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
177 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
178 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
179 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
180 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
181 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
182 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
183 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
184 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
185 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
186 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
187 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
188 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
189 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
190 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
191 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
192 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
193 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
194 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
195 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
196 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
197 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
198 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
199 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
200 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
201 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
202 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
203 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
204 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
205 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
206 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
207 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
208 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
209 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
210 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
211 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
212 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
213 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
214 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
215 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
216 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
217 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
218 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
219 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
220 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
221 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
222 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
223 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
224 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
225 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
226 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
227 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
228 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
229 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
230 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
231 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
232 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
233 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
234 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
235 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
236 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
237 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
238 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
239 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
240 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
241 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100242 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
243 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
244 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
245 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100246 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100247};
248
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100249/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100250static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100251/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100252static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100253
Christopher Faulet6338a082019-09-09 15:50:54 +0200254static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100255
Christopher Fauletef779222018-10-31 08:47:01 +0100256static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
257{
258 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100259 if (chk->data >= channel_htx_recv_max(chn, htx))
260 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200261 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100262 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100263 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100264 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100265 }
266 else {
267 if (ci_putchk(chn, chk) == -1)
268 return 0;
269 }
270 return 1;
271}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100272
Christopher Fauleted7a0662019-01-14 11:07:34 +0100273static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
274{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200275 struct channel *req = si_oc(si);
276 struct htx *htx = htxbuf(&req->buf);
277 struct htx_blk *blk;
278 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100279
Christopher Fauletb7f88902019-07-15 21:56:43 +0200280 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100281 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200282 ALREADY_CHECKED(blk);
283 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
284 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100285}
286
William Lallemand74c24fb2016-11-21 17:18:36 +0100287/*
288 * http_stats_io_handler()
289 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
290 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100291 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100292 * -> stats_dump_html_head() // emits the HTML headers
293 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
294 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
295 * -> stats_dump_html_px_hdr()
296 * -> stats_dump_fe_stats()
297 * -> stats_dump_li_stats()
298 * -> stats_dump_sv_stats()
299 * -> stats_dump_be_stats()
300 * -> stats_dump_html_px_end()
301 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100302 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100303 */
304
305
William Lallemand74c24fb2016-11-21 17:18:36 +0100306/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
307 * for clearing it if needed.
308 * NOTE: Some tools happen to rely on the field position instead of its name,
309 * so please only append new fields at the end, never in the middle.
310 */
311static void stats_dump_csv_header()
312{
313 int field;
314
315 chunk_appendf(&trash, "# ");
316 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200317 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100318
319 chunk_appendf(&trash, "\n");
320}
321
322
323/* Emits a stats field without any surrounding element and properly encoded to
324 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
325 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200326int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100327{
328 switch (field_format(f, 0)) {
329 case FF_EMPTY: return 1;
330 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
331 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
332 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
333 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200334 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100335 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
336 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
337 }
338}
339
340/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
341 * output is supposed to be used on its own line. Returns non-zero on success, 0
342 * if the buffer is full.
343 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200344int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100345{
346 switch (field_format(f, 0)) {
347 case FF_EMPTY: return 1;
348 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
349 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
350 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
351 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200352 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100353 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
354 default: return chunk_appendf(out, "%08x:?", f->type);
355 }
356}
357
Simon Horman05ee2132017-01-04 09:37:25 +0100358/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
359 * the recommendation for interoperable integers in section 6 of RFC 7159.
360 */
361#define JSON_INT_MAX ((1ULL << 53) - 1)
362#define JSON_INT_MIN (0 - JSON_INT_MAX)
363
364/* Emits a stats field value and its type in JSON.
365 * Returns non-zero on success, 0 on error.
366 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200367int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100368{
369 int old_len;
370 char buf[20];
371 const char *type, *value = buf, *quote = "";
372
373 switch (field_format(f, 0)) {
374 case FF_EMPTY: return 1;
375 case FF_S32: type = "\"s32\"";
376 snprintf(buf, sizeof(buf), "%d", f->u.s32);
377 break;
378 case FF_U32: type = "\"u32\"";
379 snprintf(buf, sizeof(buf), "%u", f->u.u32);
380 break;
381 case FF_S64: type = "\"s64\"";
382 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
383 return 0;
384 type = "\"s64\"";
385 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
386 break;
387 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
388 return 0;
389 type = "\"u64\"";
390 snprintf(buf, sizeof(buf), "%llu",
391 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200392 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200393 case FF_FLT: type = "\"flt\"";
394 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100395 break;
396 case FF_STR: type = "\"str\"";
397 value = field_str(f, 0);
398 quote = "\"";
399 break;
400 default: snprintf(buf, sizeof(buf), "%u", f->type);
401 type = buf;
402 value = "unknown";
403 quote = "\"";
404 break;
405 }
406
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200407 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100408 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
409 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200410 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100411}
412
William Lallemand74c24fb2016-11-21 17:18:36 +0100413/* Emits an encoding of the field type on 3 characters followed by a delimiter.
414 * Returns non-zero on success, 0 if the buffer is full.
415 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200416int stats_emit_field_tags(struct buffer *out, const struct field *f,
417 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100418{
419 char origin, nature, scope;
420
421 switch (field_origin(f, 0)) {
422 case FO_METRIC: origin = 'M'; break;
423 case FO_STATUS: origin = 'S'; break;
424 case FO_KEY: origin = 'K'; break;
425 case FO_CONFIG: origin = 'C'; break;
426 case FO_PRODUCT: origin = 'P'; break;
427 default: origin = '?'; break;
428 }
429
430 switch (field_nature(f, 0)) {
431 case FN_GAUGE: nature = 'G'; break;
432 case FN_LIMIT: nature = 'L'; break;
433 case FN_MIN: nature = 'm'; break;
434 case FN_MAX: nature = 'M'; break;
435 case FN_RATE: nature = 'R'; break;
436 case FN_COUNTER: nature = 'C'; break;
437 case FN_DURATION: nature = 'D'; break;
438 case FN_AGE: nature = 'A'; break;
439 case FN_TIME: nature = 'T'; break;
440 case FN_NAME: nature = 'N'; break;
441 case FN_OUTPUT: nature = 'O'; break;
442 case FN_AVG: nature = 'a'; break;
443 default: nature = '?'; break;
444 }
445
446 switch (field_scope(f, 0)) {
447 case FS_PROCESS: scope = 'P'; break;
448 case FS_SERVICE: scope = 'S'; break;
449 case FS_SYSTEM: scope = 's'; break;
450 case FS_CLUSTER: scope = 'C'; break;
451 default: scope = '?'; break;
452 }
453
454 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
455}
456
Simon Horman05ee2132017-01-04 09:37:25 +0100457/* Emits an encoding of the field type as JSON.
458 * Returns non-zero on success, 0 if the buffer is full.
459 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200460int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100461{
462 const char *origin, *nature, *scope;
463 int old_len;
464
465 switch (field_origin(f, 0)) {
466 case FO_METRIC: origin = "Metric"; break;
467 case FO_STATUS: origin = "Status"; break;
468 case FO_KEY: origin = "Key"; break;
469 case FO_CONFIG: origin = "Config"; break;
470 case FO_PRODUCT: origin = "Product"; break;
471 default: origin = "Unknown"; break;
472 }
473
474 switch (field_nature(f, 0)) {
475 case FN_GAUGE: nature = "Gauge"; break;
476 case FN_LIMIT: nature = "Limit"; break;
477 case FN_MIN: nature = "Min"; break;
478 case FN_MAX: nature = "Max"; break;
479 case FN_RATE: nature = "Rate"; break;
480 case FN_COUNTER: nature = "Counter"; break;
481 case FN_DURATION: nature = "Duration"; break;
482 case FN_AGE: nature = "Age"; break;
483 case FN_TIME: nature = "Time"; break;
484 case FN_NAME: nature = "Name"; break;
485 case FN_OUTPUT: nature = "Output"; break;
486 case FN_AVG: nature = "Avg"; break;
487 default: nature = "Unknown"; break;
488 }
489
490 switch (field_scope(f, 0)) {
491 case FS_PROCESS: scope = "Process"; break;
492 case FS_SERVICE: scope = "Service"; break;
493 case FS_SYSTEM: scope = "System"; break;
494 case FS_CLUSTER: scope = "Cluster"; break;
495 default: scope = "Unknown"; break;
496 }
497
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200498 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100499 chunk_appendf(out, "\"tags\":{"
500 "\"origin\":\"%s\","
501 "\"nature\":\"%s\","
502 "\"scope\":\"%s\""
503 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200504 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100505}
William Lallemand74c24fb2016-11-21 17:18:36 +0100506
507/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200508static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200509 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100510{
511 int field;
512
513 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
514 if (!stats_emit_raw_data_field(out, &stats[field]))
515 return 0;
516 if (!chunk_strcat(out, ","))
517 return 0;
518 }
519 chunk_strcat(out, "\n");
520 return 1;
521}
522
523/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200524static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200525 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100526{
527 int field;
528
529 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
530 if (!stats[field].type)
531 continue;
532
533 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
534 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
535 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
536 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
537 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
538 '?',
539 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200540 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100541
542 if (!stats_emit_field_tags(out, &stats[field], ':'))
543 return 0;
544 if (!stats_emit_typed_data_field(out, &stats[field]))
545 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200546 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
547 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100548 if (!chunk_strcat(out, "\n"))
549 return 0;
550 }
551 return 1;
552}
553
Simon Horman05ee2132017-01-04 09:37:25 +0100554/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200555static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200556 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100557{
558 int field;
559 int started = 0;
560
561 if (!chunk_strcat(out, "["))
562 return 0;
563
564 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
565 int old_len;
566
567 if (!field_format(info, field))
568 continue;
569
570 if (started && !chunk_strcat(out, ","))
571 goto err;
572 started = 1;
573
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200574 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100575 chunk_appendf(out,
576 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
577 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200578 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100579 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200580 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100581 goto err;
582
583 if (!stats_emit_json_field_tags(out, &info[field]))
584 goto err;
585
586 if (!stats_emit_json_data_field(out, &info[field]))
587 goto err;
588
589 if (!chunk_strcat(out, "}"))
590 goto err;
591 }
592
593 if (!chunk_strcat(out, "]"))
594 goto err;
595 return 1;
596
597err:
598 chunk_reset(out);
599 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
600 return 0;
601}
602
603/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200604static int stats_dump_fields_json(struct buffer *out,
605 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200606 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100607{
608 int field;
609 int started = 0;
610
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200611 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100612 return 0;
613 if (!chunk_strcat(out, "["))
614 return 0;
615
616 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
617 const char *obj_type;
618 int old_len;
619
620 if (!stats[field].type)
621 continue;
622
623 if (started && !chunk_strcat(out, ","))
624 goto err;
625 started = 1;
626
627 switch (stats[ST_F_TYPE].u.u32) {
628 case STATS_TYPE_FE: obj_type = "Frontend"; break;
629 case STATS_TYPE_BE: obj_type = "Backend"; break;
630 case STATS_TYPE_SO: obj_type = "Listener"; break;
631 case STATS_TYPE_SV: obj_type = "Server"; break;
632 default: obj_type = "Unknown"; break;
633 }
634
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200635 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100636 chunk_appendf(out,
637 "{"
638 "\"objType\":\"%s\","
639 "\"proxyId\":%d,"
640 "\"id\":%d,"
641 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
642 "\"processNum\":%u,",
643 obj_type, stats[ST_F_IID].u.u32,
644 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200645 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200646 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100647 goto err;
648
649 if (!stats_emit_json_field_tags(out, &stats[field]))
650 goto err;
651
652 if (!stats_emit_json_data_field(out, &stats[field]))
653 goto err;
654
655 if (!chunk_strcat(out, "}"))
656 goto err;
657 }
658
659 if (!chunk_strcat(out, "]"))
660 goto err;
661
662 return 1;
663
664err:
665 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200666 if (flags & STAT_STARTED)
667 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100668 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
669 return 0;
670}
671
William Lallemand74c24fb2016-11-21 17:18:36 +0100672/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200673 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200674 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100675 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200676static int stats_dump_fields_html(struct buffer *out,
677 const struct field *stats,
678 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100679{
Willy Tarreau83061a82018-07-13 11:56:34 +0200680 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100681
682 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
683 chunk_appendf(out,
684 /* name, queue */
685 "<tr class=\"frontend\">");
686
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200687 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100688 /* Column sub-heading for Enable or Disable server */
689 chunk_appendf(out, "<td></td>");
690 }
691
692 chunk_appendf(out,
693 "<td class=ac>"
694 "<a name=\"%s/Frontend\"></a>"
695 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
696 "<td colspan=3></td>"
697 "",
698 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
699
700 chunk_appendf(out,
701 /* sessions rate : current */
702 "<td><u>%s<div class=tips><table class=det>"
703 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
704 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
705 "",
706 U2H(stats[ST_F_RATE].u.u32),
707 U2H(stats[ST_F_CONN_RATE].u.u32),
708 U2H(stats[ST_F_RATE].u.u32));
709
710 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
711 chunk_appendf(out,
712 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
713 U2H(stats[ST_F_REQ_RATE].u.u32));
714
715 chunk_appendf(out,
716 "</table></div></u></td>"
717 /* sessions rate : max */
718 "<td><u>%s<div class=tips><table class=det>"
719 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
720 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
721 "",
722 U2H(stats[ST_F_RATE_MAX].u.u32),
723 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
724 U2H(stats[ST_F_RATE_MAX].u.u32));
725
726 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
727 chunk_appendf(out,
728 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
729 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
730
731 chunk_appendf(out,
732 "</table></div></u></td>"
733 /* sessions rate : limit */
734 "<td>%s</td>",
735 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
736
737 chunk_appendf(out,
738 /* sessions: current, max, limit, total */
739 "<td>%s</td><td>%s</td><td>%s</td>"
740 "<td><u>%s<div class=tips><table class=det>"
741 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
742 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
743 "",
744 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
745 U2H(stats[ST_F_STOT].u.u64),
746 U2H(stats[ST_F_CONN_TOT].u.u64),
747 U2H(stats[ST_F_STOT].u.u64));
748
749 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
750 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
751 chunk_appendf(out,
752 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
753 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
754 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
755 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
756 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
757 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
758 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
759 "<tr><th>- other responses:</th><td>%s</td></tr>"
760 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100761 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
762 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200763 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100764 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100765 "",
766 U2H(stats[ST_F_REQ_TOT].u.u64),
767 U2H(stats[ST_F_HRSP_1XX].u.u64),
768 U2H(stats[ST_F_HRSP_2XX].u.u64),
769 U2H(stats[ST_F_COMP_RSP].u.u64),
770 stats[ST_F_HRSP_2XX].u.u64 ?
771 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
772 U2H(stats[ST_F_HRSP_3XX].u.u64),
773 U2H(stats[ST_F_HRSP_4XX].u.u64),
774 U2H(stats[ST_F_HRSP_5XX].u.u64),
775 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200776 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100777 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
778 U2H(stats[ST_F_CACHE_HITS].u.u64),
779 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
780 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100781 U2H(stats[ST_F_WREW].u.u64),
782 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100783 }
784
785 chunk_appendf(out,
786 "</table></div></u></td>"
787 /* sessions: lbtot, lastsess */
788 "<td></td><td></td>"
789 /* bytes : in */
790 "<td>%s</td>"
791 "",
792 U2H(stats[ST_F_BIN].u.u64));
793
794 chunk_appendf(out,
795 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
796 "<td>%s%s<div class=tips><table class=det>"
797 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
798 "<tr><th>Compression in:</th><td>%s</td></tr>"
799 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
800 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
801 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
802 "</table></div>%s</td>",
803 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
804 U2H(stats[ST_F_BOUT].u.u64),
805 U2H(stats[ST_F_BOUT].u.u64),
806 U2H(stats[ST_F_COMP_IN].u.u64),
807 U2H(stats[ST_F_COMP_OUT].u.u64),
808 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
809 U2H(stats[ST_F_COMP_BYP].u.u64),
810 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
811 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,
812 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
813
814 chunk_appendf(out,
815 /* denied: req, resp */
816 "<td>%s</td><td>%s</td>"
817 /* errors : request, connect, response */
818 "<td>%s</td><td></td><td></td>"
819 /* warnings: retries, redispatches */
820 "<td></td><td></td>"
821 /* server status : reflect frontend status */
822 "<td class=ac>%s</td>"
823 /* rest of server: nothing */
824 "<td class=ac colspan=8></td></tr>"
825 "",
826 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
827 U2H(stats[ST_F_EREQ].u.u64),
828 field_str(stats, ST_F_STATUS));
829 }
830 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
831 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200832 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100833 /* Column sub-heading for Enable or Disable server */
834 chunk_appendf(out, "<td></td>");
835 }
836
837 chunk_appendf(out,
838 /* frontend name, listener name */
839 "<td class=ac><a name=\"%s/+%s\"></a>%s"
840 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
841 "",
842 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200843 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100844 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
845
Willy Tarreau708c4162019-10-09 10:19:16 +0200846 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100847 chunk_appendf(out, "<div class=tips>");
848
Willy Tarreau90807112020-02-25 08:16:33 +0100849 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100850 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
851 else if (*field_str(stats, ST_F_ADDR) == '[')
852 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
853 else if (*field_str(stats, ST_F_ADDR))
854 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
855
856 /* id */
857 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
858 }
859
860 chunk_appendf(out,
861 /* queue */
862 "%s</td><td colspan=3></td>"
863 /* sessions rate: current, max, limit */
864 "<td colspan=3>&nbsp;</td>"
865 /* sessions: current, max, limit, total, lbtot, lastsess */
866 "<td>%s</td><td>%s</td><td>%s</td>"
867 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
868 /* bytes: in, out */
869 "<td>%s</td><td>%s</td>"
870 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200871 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100872 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
873 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
874
875 chunk_appendf(out,
876 /* denied: req, resp */
877 "<td>%s</td><td>%s</td>"
878 /* errors: request, connect, response */
879 "<td>%s</td><td></td><td></td>"
880 /* warnings: retries, redispatches */
881 "<td></td><td></td>"
882 /* server status: reflect listener status */
883 "<td class=ac>%s</td>"
884 /* rest of server: nothing */
885 "<td class=ac colspan=8></td></tr>"
886 "",
887 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
888 U2H(stats[ST_F_EREQ].u.u64),
889 field_str(stats, ST_F_STATUS));
890 }
891 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
892 const char *style;
893
894 /* determine the style to use depending on the server's state,
895 * its health and weight. There isn't a 1-to-1 mapping between
896 * state and styles for the cases where the server is (still)
897 * up. The reason is that we don't want to report nolb and
898 * drain with the same color.
899 */
900
901 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
902 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
903 style = "down";
904 }
905 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
906 style = "going_up";
907 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400908 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
909 style = "draining";
910 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100911 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
912 style = "going_down";
913 }
914 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
915 style = "nolb";
916 }
917 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
918 style = "no_check";
919 }
920 else if (!stats[ST_F_CHKFAIL].type ||
921 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
922 /* no check or max health = UP */
923 if (stats[ST_F_WEIGHT].u.u32)
924 style = "up";
925 else
926 style = "draining";
927 }
928 else {
929 style = "going_down";
930 }
931
932 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
933 chunk_appendf(out, "<tr class=\"maintain\">");
934 else
935 chunk_appendf(out,
936 "<tr class=\"%s_%s\">",
937 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
938
939
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200940 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100941 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000942 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
943 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100944 field_str(stats, ST_F_SVNAME));
945
946 chunk_appendf(out,
947 "<td class=ac><a name=\"%s/%s\"></a>%s"
948 "<a class=lfsb href=\"#%s/%s\">%s</a>"
949 "",
950 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200951 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100952 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
953
Willy Tarreau708c4162019-10-09 10:19:16 +0200954 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100955 chunk_appendf(out, "<div class=tips>");
956
Willy Tarreau90807112020-02-25 08:16:33 +0100957 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100958 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
959 else if (*field_str(stats, ST_F_ADDR) == '[')
960 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
961 else if (*field_str(stats, ST_F_ADDR))
962 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
963
964 /* id */
965 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
966
967 /* cookie */
968 if (stats[ST_F_COOKIE].type) {
969 chunk_appendf(out, ", cookie: '");
970 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
971 chunk_htmlencode(out, &src);
972 chunk_appendf(out, "'");
973 }
974
975 chunk_appendf(out, "</div>");
976 }
977
978 chunk_appendf(out,
979 /* queue : current, max, limit */
980 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
981 /* sessions rate : current, max, limit */
982 "<td>%s</td><td>%s</td><td></td>"
983 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200984 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100985 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
986 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
987
988 chunk_appendf(out,
989 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200990 "<td><u>%s<div class=tips>"
991 "<table class=det>"
992 "<tr><th>Current active connections:</th><td>%s</td></tr>"
993 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
994 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
995 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
996 "</table></div></u>"
997 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100998 "<td><u>%s<div class=tips><table class=det>"
999 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1000 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001001 U2H(stats[ST_F_SCUR].u.u32),
1002 U2H(stats[ST_F_SCUR].u.u32),
1003 U2H(stats[ST_F_SRV_ICUR].u.u32),
1004 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1005 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1006 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001007 U2H(stats[ST_F_STOT].u.u64),
1008 U2H(stats[ST_F_STOT].u.u64));
1009
1010 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1011 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001012 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001013 "<tr><th>New connections:</th><td>%s</td></tr>"
1014 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001015 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001016 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1017 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1018 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1019 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1020 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1021 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001022 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001023 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001024 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001025 U2H(stats[ST_F_CONNECT].u.u64),
1026 U2H(stats[ST_F_REUSE].u.u64),
1027 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1028 (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 +02001029 U2H(stats[ST_F_REQ_TOT].u.u64),
1030 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1031 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1032 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1033 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1034 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1035 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1036 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1037 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1038 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1039 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1040 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1041 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001042 U2H(stats[ST_F_WREW].u.u64),
1043 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001044 }
1045
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001046 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1047 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1048 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1049 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1050 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001051 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001052 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1053 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1054 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1055 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001056
1057 chunk_appendf(out,
1058 "</table></div></u></td>"
1059 /* sessions: lbtot, last */
1060 "<td>%s</td><td>%s</td>",
1061 U2H(stats[ST_F_LBTOT].u.u64),
1062 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1063
1064 chunk_appendf(out,
1065 /* bytes : in, out */
1066 "<td>%s</td><td>%s</td>"
1067 /* denied: req, resp */
1068 "<td></td><td>%s</td>"
1069 /* errors : request, connect */
1070 "<td></td><td>%s</td>"
1071 /* errors : response */
1072 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1073 /* warnings: retries, redispatches */
1074 "<td>%lld</td><td>%lld</td>"
1075 "",
1076 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1077 U2H(stats[ST_F_DRESP].u.u64),
1078 U2H(stats[ST_F_ECON].u.u64),
1079 U2H(stats[ST_F_ERESP].u.u64),
1080 (long long)stats[ST_F_CLI_ABRT].u.u64,
1081 (long long)stats[ST_F_SRV_ABRT].u.u64,
1082 (long long)stats[ST_F_WRETR].u.u64,
1083 (long long)stats[ST_F_WREDIS].u.u64);
1084
1085 /* status, last change */
1086 chunk_appendf(out, "<td class=ac>");
1087
1088 /* FIXME!!!!
1089 * LASTCHG should contain the last change for *this* server and must be computed
1090 * properly above, as was done below, ie: this server if maint, otherwise ref server
1091 * if tracking. Note that ref is either local or remote depending on tracking.
1092 */
1093
1094
1095 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1096 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1097 }
1098 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1099 chunk_strcat(out, "<i>no check</i>");
1100 }
1101 else {
1102 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1103 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1104 if (stats[ST_F_CHECK_HEALTH].u.u32)
1105 chunk_strcat(out, " &uarr;");
1106 }
1107 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1108 chunk_strcat(out, " &darr;");
1109 }
1110
1111 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1112 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1113 chunk_appendf(out,
1114 "</td><td class=ac><u> %s",
1115 field_str(stats, ST_F_AGENT_STATUS));
1116
1117 if (stats[ST_F_AGENT_CODE].type)
1118 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1119
1120 if (stats[ST_F_AGENT_DURATION].type)
1121 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1122
1123 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1124
1125 if (*field_str(stats, ST_F_LAST_AGT)) {
1126 chunk_appendf(out, ": ");
1127 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1128 chunk_htmlencode(out, &src);
1129 }
1130 chunk_appendf(out, "</div></u>");
1131 }
1132 else if (stats[ST_F_CHECK_STATUS].type) {
1133 chunk_appendf(out,
1134 "</td><td class=ac><u> %s",
1135 field_str(stats, ST_F_CHECK_STATUS));
1136
1137 if (stats[ST_F_CHECK_CODE].type)
1138 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1139
1140 if (stats[ST_F_CHECK_DURATION].type)
1141 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1142
1143 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1144
1145 if (*field_str(stats, ST_F_LAST_CHK)) {
1146 chunk_appendf(out, ": ");
1147 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1148 chunk_htmlencode(out, &src);
1149 }
1150 chunk_appendf(out, "</div></u>");
1151 }
1152 else
1153 chunk_appendf(out, "</td><td>");
1154
1155 chunk_appendf(out,
1156 /* weight */
1157 "</td><td class=ac>%d</td>"
1158 /* act, bck */
1159 "<td class=ac>%s</td><td class=ac>%s</td>"
1160 "",
1161 stats[ST_F_WEIGHT].u.u32,
1162 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1163 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1164
1165 /* check failures: unique, fatal, down time */
1166 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1167 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1168 }
1169 else if (stats[ST_F_CHKFAIL].type) {
1170 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1171
1172 if (stats[ST_F_HANAFAIL].type)
1173 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1174
1175 chunk_appendf(out,
1176 "<div class=tips>Failed Health Checks%s</div></u></td>"
1177 "<td>%lld</td><td>%s</td>"
1178 "",
1179 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1180 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1181 }
1182 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1183 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1184 chunk_appendf(out,
1185 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1186 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1187 }
1188 else
1189 chunk_appendf(out, "<td colspan=3></td>");
1190
1191 /* throttle */
1192 if (stats[ST_F_THROTTLE].type)
1193 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1194 else
1195 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1196 }
1197 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1198 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001199 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001200 /* Column sub-heading for Enable or Disable server */
1201 chunk_appendf(out, "<td></td>");
1202 }
1203 chunk_appendf(out,
1204 "<td class=ac>"
1205 /* name */
1206 "%s<a name=\"%s/Backend\"></a>"
1207 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1208 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001209 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001210 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1211
Willy Tarreau708c4162019-10-09 10:19:16 +02001212 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001213 /* balancing */
1214 chunk_appendf(out, "<div class=tips>balancing: %s",
1215 field_str(stats, ST_F_ALGO));
1216
1217 /* cookie */
1218 if (stats[ST_F_COOKIE].type) {
1219 chunk_appendf(out, ", cookie: '");
1220 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1221 chunk_htmlencode(out, &src);
1222 chunk_appendf(out, "'");
1223 }
1224 chunk_appendf(out, "</div>");
1225 }
1226
1227 chunk_appendf(out,
1228 "%s</td>"
1229 /* queue : current, max */
1230 "<td>%s</td><td>%s</td><td></td>"
1231 /* sessions rate : current, max, limit */
1232 "<td>%s</td><td>%s</td><td></td>"
1233 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001234 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001235 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1236 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1237
1238 chunk_appendf(out,
1239 /* sessions: current, max, limit, total */
1240 "<td>%s</td><td>%s</td><td>%s</td>"
1241 "<td><u>%s<div class=tips><table class=det>"
1242 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1243 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001244 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 +01001245 U2H(stats[ST_F_STOT].u.u64),
1246 U2H(stats[ST_F_STOT].u.u64));
1247
1248 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1249 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1250 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001251 "<tr><th>New connections:</th><td>%s</td></tr>"
1252 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001253 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1254 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1255 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1256 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1257 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1258 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1259 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1260 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001261 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1262 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001263 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001264 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001265 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001266 U2H(stats[ST_F_CONNECT].u.u64),
1267 U2H(stats[ST_F_REUSE].u.u64),
1268 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1269 (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 +01001270 U2H(stats[ST_F_REQ_TOT].u.u64),
1271 U2H(stats[ST_F_HRSP_1XX].u.u64),
1272 U2H(stats[ST_F_HRSP_2XX].u.u64),
1273 U2H(stats[ST_F_COMP_RSP].u.u64),
1274 stats[ST_F_HRSP_2XX].u.u64 ?
1275 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1276 U2H(stats[ST_F_HRSP_3XX].u.u64),
1277 U2H(stats[ST_F_HRSP_4XX].u.u64),
1278 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001279 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001280 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1281 U2H(stats[ST_F_CACHE_HITS].u.u64),
1282 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1283 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001284 U2H(stats[ST_F_WREW].u.u64),
1285 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001286 }
1287
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001288 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1289 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1290 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1291 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1292 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001293 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001294 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1295 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1296 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1297 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001298
1299 chunk_appendf(out,
1300 "</table></div></u></td>"
1301 /* sessions: lbtot, last */
1302 "<td>%s</td><td>%s</td>"
1303 /* bytes: in */
1304 "<td>%s</td>"
1305 "",
1306 U2H(stats[ST_F_LBTOT].u.u64),
1307 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1308 U2H(stats[ST_F_BIN].u.u64));
1309
1310 chunk_appendf(out,
1311 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1312 "<td>%s%s<div class=tips><table class=det>"
1313 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1314 "<tr><th>Compression in:</th><td>%s</td></tr>"
1315 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1316 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1317 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1318 "</table></div>%s</td>",
1319 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1320 U2H(stats[ST_F_BOUT].u.u64),
1321 U2H(stats[ST_F_BOUT].u.u64),
1322 U2H(stats[ST_F_COMP_IN].u.u64),
1323 U2H(stats[ST_F_COMP_OUT].u.u64),
1324 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1325 U2H(stats[ST_F_COMP_BYP].u.u64),
1326 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1327 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,
1328 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1329
1330 chunk_appendf(out,
1331 /* denied: req, resp */
1332 "<td>%s</td><td>%s</td>"
1333 /* errors : request, connect */
1334 "<td></td><td>%s</td>"
1335 /* errors : response */
1336 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1337 /* warnings: retries, redispatches */
1338 "<td>%lld</td><td>%lld</td>"
1339 /* backend status: reflect backend status (up/down): we display UP
1340 * if the backend has known working servers or if it has no server at
1341 * all (eg: for stats). Then we display the total weight, number of
1342 * active and backups. */
1343 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1344 "<td class=ac>%d</td><td class=ac>%d</td>"
1345 "",
1346 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1347 U2H(stats[ST_F_ECON].u.u64),
1348 U2H(stats[ST_F_ERESP].u.u64),
1349 (long long)stats[ST_F_CLI_ABRT].u.u64,
1350 (long long)stats[ST_F_SRV_ABRT].u.u64,
1351 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1352 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1353 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1354 stats[ST_F_WEIGHT].u.u32,
1355 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1356
1357 chunk_appendf(out,
1358 /* rest of backend: nothing, down transitions, total downtime, throttle */
1359 "<td class=ac>&nbsp;</td><td>%d</td>"
1360 "<td>%s</td>"
1361 "<td></td>"
1362 "</tr>",
1363 stats[ST_F_CHKDOWN].u.u32,
1364 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1365 }
1366 return 1;
1367}
1368
Willy Tarreau43241ff2019-10-09 11:27:51 +02001369static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001370{
Simon Horman05ee2132017-01-04 09:37:25 +01001371 int ret;
1372
William Lallemand74c24fb2016-11-21 17:18:36 +01001373 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001374 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001375 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001376 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001377 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001378 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001379 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001380 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001381
1382 if (ret)
1383 appctx->ctx.stats.flags |= STAT_STARTED;
1384
1385 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001386}
1387
1388/* Fill <stats> with the frontend statistics. <stats> is
1389 * preallocated array of length <len>. The length of the array
1390 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1391 * this value, the function returns 0, otherwise, it returns 1.
1392 */
1393int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1394{
1395 if (len < ST_F_TOTAL_FIELDS)
1396 return 0;
1397
1398 memset(stats, 0, sizeof(*stats) * len);
1399
1400 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1401 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1402 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1403 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1404 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1405 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1406 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1407 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1408 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1409 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1410 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1411 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1412 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1413 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1414 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1415 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1416 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1417 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1418 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1419 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1420 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1421 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001422 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001423 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001424
1425 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1426 if (px->mode == PR_MODE_HTTP) {
1427 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1428 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1429 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1430 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1431 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1432 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1433 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001434 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1435 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001436 }
1437
1438 /* requests : req_rate, req_rate_max, req_tot, */
1439 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1440 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1441 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1442
1443 /* compression: in, out, bypassed, responses */
1444 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1445 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1446 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1447 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1448
1449 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1450 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1451 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1452 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1453
1454 return 1;
1455}
1456
1457/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1458 * the state from stream interface <si>. The caller is responsible for clearing
1459 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1460 */
1461static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1462{
1463 struct appctx *appctx = __objt_appctx(si->end);
1464
1465 if (!(px->cap & PR_CAP_FE))
1466 return 0;
1467
1468 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1469 return 0;
1470
1471 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1472 return 0;
1473
Willy Tarreau43241ff2019-10-09 11:27:51 +02001474 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001475}
1476
1477/* Fill <stats> with the listener statistics. <stats> is
1478 * preallocated array of length <len>. The length of the array
1479 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1480 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001481 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001482 */
1483int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1484 struct field *stats, int len)
1485{
Willy Tarreau83061a82018-07-13 11:56:34 +02001486 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001487
1488 if (len < ST_F_TOTAL_FIELDS)
1489 return 0;
1490
1491 if (!l->counters)
1492 return 0;
1493
1494 chunk_reset(out);
1495 memset(stats, 0, sizeof(*stats) * len);
1496
1497 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1498 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1499 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1500 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1501 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1502 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1503 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1504 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1505 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1506 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1507 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1508 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1509 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1510 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001511 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 +01001512 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1513 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1514 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1515 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001516 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001517 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001518
Willy Tarreau708c4162019-10-09 10:19:16 +02001519 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001520 char str[INET6_ADDRSTRLEN];
1521 int port;
1522
1523 port = get_host_port(&l->addr);
1524 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1525 case AF_INET:
1526 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1527 chunk_appendf(out, "%s:%d", str, port);
1528 break;
1529 case AF_INET6:
1530 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1531 chunk_appendf(out, "[%s]:%d", str, port);
1532 break;
1533 case AF_UNIX:
1534 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1535 break;
1536 case -1:
1537 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1538 chunk_strcat(out, strerror(errno));
1539 break;
1540 default: /* address family not supported */
1541 break;
1542 }
1543 }
1544
1545 return 1;
1546}
1547
1548/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001549 * from stream interface <si>. The caller is responsible for clearing the trash
1550 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001551 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001552static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001553{
1554 struct appctx *appctx = __objt_appctx(si->end);
1555
Willy Tarreau43241ff2019-10-09 11:27:51 +02001556 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001557 return 0;
1558
Willy Tarreau43241ff2019-10-09 11:27:51 +02001559 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001560}
1561
1562enum srv_stats_state {
1563 SRV_STATS_STATE_DOWN = 0,
1564 SRV_STATS_STATE_DOWN_AGENT,
1565 SRV_STATS_STATE_GOING_UP,
1566 SRV_STATS_STATE_UP_GOING_DOWN,
1567 SRV_STATS_STATE_UP,
1568 SRV_STATS_STATE_NOLB_GOING_DOWN,
1569 SRV_STATS_STATE_NOLB,
1570 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1571 SRV_STATS_STATE_DRAIN,
1572 SRV_STATS_STATE_DRAIN_AGENT,
1573 SRV_STATS_STATE_NO_CHECK,
1574
1575 SRV_STATS_STATE_COUNT, /* Must be last */
1576};
1577
1578static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1579 [SRV_STATS_STATE_DOWN] = "DOWN",
1580 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1581 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1582 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1583 [SRV_STATS_STATE_UP] = "UP",
1584 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1585 [SRV_STATS_STATE_NOLB] = "NOLB",
1586 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1587 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1588 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1589 [SRV_STATS_STATE_NO_CHECK] = "no check"
1590};
1591
1592/* Fill <stats> with the server statistics. <stats> is
1593 * preallocated array of length <len>. The length of the array
1594 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1595 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001596 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001597 */
1598int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1599 struct field *stats, int len)
1600{
1601 struct server *via, *ref;
1602 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001603 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001604 enum srv_stats_state state;
1605 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001606 long long srv_samples_counter;
1607 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001608
1609 if (len < ST_F_TOTAL_FIELDS)
1610 return 0;
1611
1612 memset(stats, 0, sizeof(*stats) * len);
1613
1614 /* we have "via" which is the tracked server as described in the configuration,
1615 * and "ref" which is the checked server and the end of the chain.
1616 */
1617 via = sv->track ? sv->track : sv;
1618 ref = via;
1619 while (ref->track)
1620 ref = ref->track;
1621
Emeric Brun52a91d32017-08-31 14:41:55 +02001622 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001623 if ((ref->check.state & CHK_ST_ENABLED) &&
1624 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1625 state = SRV_STATS_STATE_UP_GOING_DOWN;
1626 } else {
1627 state = SRV_STATS_STATE_UP;
1628 }
1629
Emeric Brun52a91d32017-08-31 14:41:55 +02001630 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001631 if (ref->agent.state & CHK_ST_ENABLED)
1632 state = SRV_STATS_STATE_DRAIN_AGENT;
1633 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1634 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1635 else
1636 state = SRV_STATS_STATE_DRAIN;
1637 }
1638
1639 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1640 state = SRV_STATS_STATE_NO_CHECK;
1641 }
1642 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001643 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001644 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1645 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1646 state = SRV_STATS_STATE_NOLB;
1647 } else {
1648 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1649 }
1650 }
1651 else { /* stopped */
1652 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1653 state = SRV_STATS_STATE_DOWN_AGENT;
1654 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1655 state = SRV_STATS_STATE_DOWN; /* DOWN */
1656 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1657 state = SRV_STATS_STATE_GOING_UP;
1658 } else {
1659 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1660 }
1661 }
1662
1663 chunk_reset(out);
1664
1665 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1666 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1667 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1668 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1669 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1670 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1671 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1672
1673 if (sv->maxconn)
1674 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1675
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001676 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1677 if (sv->max_idle_conns != -1)
1678 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1679
William Lallemand74c24fb2016-11-21 17:18:36 +01001680 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1681 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1682 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001683 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001684 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1685 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1686 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1687 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001688 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001689 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001690 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1691 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001692
1693 /* status */
1694 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001695 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001696 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001697 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001698 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001699 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001700 chunk_appendf(out, "MAINT");
1701 else
1702 chunk_appendf(out,
1703 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001704 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1705 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001706
1707 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1708 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001709 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 +01001710 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1711 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1712
1713 /* check failures: unique, fatal; last change, total downtime */
1714 if (sv->check.state & CHK_ST_ENABLED) {
1715 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1716 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1717 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1718 }
1719
1720 if (sv->maxqueue)
1721 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1722
1723 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1724 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1725 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1726
Emeric Brun52a91d32017-08-31 14:41:55 +02001727 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001728 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1729
1730 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1731
1732 if (sv->track) {
1733 char *fld_track = chunk_newstr(out);
1734
1735 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1736 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1737 }
1738
1739 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1740 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1741 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1742
1743 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1744 const char *fld_chksts;
1745
1746 fld_chksts = chunk_newstr(out);
1747 chunk_strcat(out, "* "); // for check in progress
1748 chunk_strcat(out, get_check_status_info(sv->check.status));
1749 if (!(sv->check.state & CHK_ST_INPROGRESS))
1750 fld_chksts += 2; // skip "* "
1751 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1752
1753 if (sv->check.status >= HCHK_STATUS_L57DATA)
1754 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1755
1756 if (sv->check.status >= HCHK_STATUS_CHECKED)
1757 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1758
1759 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1760 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1761 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1762 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1763 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1764 }
1765
1766 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1767 const char *fld_chksts;
1768
1769 fld_chksts = chunk_newstr(out);
1770 chunk_strcat(out, "* "); // for check in progress
1771 chunk_strcat(out, get_check_status_info(sv->agent.status));
1772 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1773 fld_chksts += 2; // skip "* "
1774 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1775
1776 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1777 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1778
1779 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1780 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1781
1782 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1783 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1784 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1785 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1786 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1787 }
1788
1789 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1790 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001791 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001792 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1793 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1794 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1795 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1796 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1797 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1798 }
1799
1800 if (ref->observe)
1801 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1802
1803 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1804 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1805 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1806
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001807 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1808 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1809 srv_samples_window = srv_samples_counter;
1810
1811 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1812 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1813 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1814 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001815
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001816 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1817 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1818 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1819 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1820
Willy Tarreau708c4162019-10-09 10:19:16 +02001821 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001822 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1823 case AF_INET:
1824 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001825 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001826 break;
1827 case AF_INET6:
1828 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001829 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001830 break;
1831 case AF_UNIX:
1832 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1833 break;
1834 case -1:
1835 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1836 chunk_strcat(out, strerror(errno));
1837 break;
1838 default: /* address family not supported */
1839 break;
1840 }
1841
1842 if (sv->cookie)
1843 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1844 }
1845
1846 return 1;
1847}
1848
1849/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001850 * from stream interface <si>, and server state <state>. The caller is
1851 * responsible for clearing the trash if needed. Returns non-zero if it emits
1852 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001853 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001854static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001855{
1856 struct appctx *appctx = __objt_appctx(si->end);
1857
Willy Tarreau43241ff2019-10-09 11:27:51 +02001858 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001859 return 0;
1860
Willy Tarreau43241ff2019-10-09 11:27:51 +02001861 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001862}
1863
1864/* Fill <stats> with the backend statistics. <stats> is
1865 * preallocated array of length <len>. The length of the array
1866 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1867 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001868 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001869 */
1870int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1871{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001872 long long be_samples_counter;
1873 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1874
William Lallemand74c24fb2016-11-21 17:18:36 +01001875 if (len < ST_F_TOTAL_FIELDS)
1876 return 0;
1877
1878 memset(stats, 0, sizeof(*stats) * len);
1879
1880 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1881 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1882 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1883 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1884 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001885 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001886 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1887 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1888 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1889 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1890 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1891 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1892 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1893 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1894 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1895 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1896 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001897 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001898 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001899 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1900 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001901 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1902 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1903 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1904 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1905 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1906 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1907 if (px->srv)
1908 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1909
1910 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1911 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1912 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1913 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1914 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1915 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1916 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1917
Willy Tarreau708c4162019-10-09 10:19:16 +02001918 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001919 if (px->cookie_name)
1920 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1921 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1922 }
1923
1924 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1925 if (px->mode == PR_MODE_HTTP) {
1926 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1927 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1928 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1929 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1930 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1931 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1932 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001933 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1934 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001935 }
1936
1937 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1938 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1939
1940 /* compression: in, out, bypassed, responses */
1941 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1942 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1943 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1944 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1945 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1946
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001947 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1948 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1949 be_samples_window = be_samples_counter;
1950
1951 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1952 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1953 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1954 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 +01001955
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001956 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1957 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1958 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1959 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1960
William Lallemand74c24fb2016-11-21 17:18:36 +01001961 return 1;
1962}
1963
1964/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001965 * interface <si>. The caller is responsible for clearing the trash if needed.
1966 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001967 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001968static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001969{
1970 struct appctx *appctx = __objt_appctx(si->end);
1971
1972 if (!(px->cap & PR_CAP_BE))
1973 return 0;
1974
1975 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1976 return 0;
1977
Willy Tarreau43241ff2019-10-09 11:27:51 +02001978 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001979 return 0;
1980
Willy Tarreau43241ff2019-10-09 11:27:51 +02001981 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001982}
1983
1984/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1985 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1986 * for clearing the trash if needed.
1987 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001988static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001989{
1990 struct appctx *appctx = __objt_appctx(si->end);
1991 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1992
1993 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1994 /* A form to enable/disable this proxy servers */
1995
1996 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1997 scope_txt[0] = 0;
1998 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001999 const char *scope_ptr = stats_scope_ptr(appctx, si);
2000
William Lallemand74c24fb2016-11-21 17:18:36 +01002001 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002002 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002003 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2004 }
2005
2006 chunk_appendf(&trash,
2007 "<form method=\"post\">");
2008 }
2009
2010 /* print a new table */
2011 chunk_appendf(&trash,
2012 "<table class=\"tbl\" width=\"100%%\">\n"
2013 "<tr class=\"titre\">"
2014 "<th class=\"pxname\" width=\"10%%\">");
2015
2016 chunk_appendf(&trash,
2017 "<a name=\"%s\"></a>%s"
2018 "<a class=px href=\"#%s\">%s</a>",
2019 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002020 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002021 px->id, px->id);
2022
Willy Tarreau676c29e2019-10-09 10:50:01 +02002023 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002024 /* cap, mode, id */
2025 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2026 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2027 px->uuid);
2028 chunk_appendf(&trash, "</div>");
2029 }
2030
2031 chunk_appendf(&trash,
2032 "%s</th>"
2033 "<th class=\"%s\" width=\"90%%\">%s</th>"
2034 "</tr>\n"
2035 "</table>\n"
2036 "<table class=\"tbl\" width=\"100%%\">\n"
2037 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002038 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002039 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2040
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002041 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002042 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002043 if ((px->cap & PR_CAP_BE) && px->srv)
2044 chunk_appendf(&trash,
2045 "<th rowspan=2 width=1><input type=\"checkbox\" "
2046 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2047 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2048 px->id,
2049 px->id);
2050 else
2051 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002052 }
2053
2054 chunk_appendf(&trash,
2055 "<th rowspan=2></th>"
2056 "<th colspan=3>Queue</th>"
2057 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2058 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2059 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2060 "<th colspan=9>Server</th>"
2061 "</tr>\n"
2062 "<tr class=\"titre\">"
2063 "<th>Cur</th><th>Max</th><th>Limit</th>"
2064 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2065 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2066 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2067 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2068 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2069 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2070 "<th>Thrtle</th>\n"
2071 "</tr>");
2072}
2073
2074/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2075 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2076 */
2077static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2078{
2079 struct appctx *appctx = __objt_appctx(si->end);
2080 chunk_appendf(&trash, "</table>");
2081
2082 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2083 /* close the form used to enable/disable this proxy servers */
2084 chunk_appendf(&trash,
2085 "Choose the action to perform on the checked servers : "
2086 "<select name=action>"
2087 "<option value=\"\"></option>"
2088 "<option value=\"ready\">Set state to READY</option>"
2089 "<option value=\"drain\">Set state to DRAIN</option>"
2090 "<option value=\"maint\">Set state to MAINT</option>"
2091 "<option value=\"dhlth\">Health: disable checks</option>"
2092 "<option value=\"ehlth\">Health: enable checks</option>"
2093 "<option value=\"hrunn\">Health: force UP</option>"
2094 "<option value=\"hnolb\">Health: force NOLB</option>"
2095 "<option value=\"hdown\">Health: force DOWN</option>"
2096 "<option value=\"dagent\">Agent: disable checks</option>"
2097 "<option value=\"eagent\">Agent: enable checks</option>"
2098 "<option value=\"arunn\">Agent: force UP</option>"
2099 "<option value=\"adown\">Agent: force DOWN</option>"
2100 "<option value=\"shutdown\">Kill Sessions</option>"
2101 "</select>"
2102 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2103 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2104 "</form>",
2105 px->uuid);
2106 }
2107
2108 chunk_appendf(&trash, "<p>\n");
2109}
2110
2111/*
2112 * Dumps statistics for a proxy. The output is sent to the stream interface's
2113 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2114 * buffer space, or non-zero if everything completed. This function is used
2115 * both by the CLI and the HTTP entry points, and is able to dump the output
2116 * in HTML or CSV formats. If the later, <uri> must be NULL.
2117 */
Christopher Fauletef779222018-10-31 08:47:01 +01002118int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2119 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002120{
2121 struct appctx *appctx = __objt_appctx(si->end);
2122 struct stream *s = si_strm(si);
2123 struct channel *rep = si_ic(si);
2124 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2125 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002126
2127 chunk_reset(&trash);
2128
2129 switch (appctx->ctx.stats.px_st) {
2130 case STAT_PX_ST_INIT:
2131 /* we are on a new proxy */
2132 if (uri && uri->scope) {
2133 /* we have a limited scope, we have to check the proxy name */
2134 struct stat_scope *scope;
2135 int len;
2136
2137 len = strlen(px->id);
2138 scope = uri->scope;
2139
2140 while (scope) {
2141 /* match exact proxy name */
2142 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2143 break;
2144
2145 /* match '.' which means 'self' proxy */
2146 if (!strcmp(scope->px_id, ".") && px == s->be)
2147 break;
2148 scope = scope->next;
2149 }
2150
2151 /* proxy name not found : don't dump anything */
2152 if (scope == NULL)
2153 return 1;
2154 }
2155
2156 /* if the user has requested a limited output and the proxy
2157 * name does not match, skip it.
2158 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002159 if (appctx->ctx.stats.scope_len) {
2160 const char *scope_ptr = stats_scope_ptr(appctx, si);
2161
2162 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2163 return 1;
2164 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002165
2166 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2167 (appctx->ctx.stats.iid != -1) &&
2168 (px->uuid != appctx->ctx.stats.iid))
2169 return 1;
2170
2171 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2172 /* fall through */
2173
2174 case STAT_PX_ST_TH:
2175 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002176 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002177 if (!stats_putchk(rep, htx, &trash))
2178 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002179 }
2180
2181 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2182 /* fall through */
2183
2184 case STAT_PX_ST_FE:
2185 /* print the frontend */
2186 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002187 if (!stats_putchk(rep, htx, &trash))
2188 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002189 }
2190
2191 appctx->ctx.stats.l = px->conf.listeners.n;
2192 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2193 /* fall through */
2194
2195 case STAT_PX_ST_LI:
2196 /* stats.l has been initialized above */
2197 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002198 if (htx) {
2199 if (htx_almost_full(htx))
2200 goto full;
2201 }
2202 else {
2203 if (buffer_almost_full(&rep->buf))
2204 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002205 }
2206
2207 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2208 if (!l->counters)
2209 continue;
2210
2211 if (appctx->ctx.stats.flags & STAT_BOUND) {
2212 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2213 break;
2214
2215 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2216 continue;
2217 }
2218
2219 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002220 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002221 if (!stats_putchk(rep, htx, &trash))
2222 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002223 }
2224 }
2225
2226 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2227 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2228 /* fall through */
2229
2230 case STAT_PX_ST_SV:
2231 /* stats.sv has been initialized above */
2232 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002233 if (htx) {
2234 if (htx_almost_full(htx))
2235 goto full;
2236 }
2237 else {
2238 if (buffer_almost_full(&rep->buf))
2239 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002240 }
2241
2242 sv = appctx->ctx.stats.sv;
2243
2244 if (appctx->ctx.stats.flags & STAT_BOUND) {
2245 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2246 break;
2247
2248 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2249 continue;
2250 }
2251
2252 svs = sv;
2253 while (svs->track)
2254 svs = svs->track;
2255
2256 /* do not report servers which are DOWN and not changing state */
2257 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002258 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2259 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002260 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2261 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2262 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2263 continue;
2264 }
2265
Willy Tarreau43241ff2019-10-09 11:27:51 +02002266 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002267 if (!stats_putchk(rep, htx, &trash))
2268 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002269 }
2270 } /* for sv */
2271
2272 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2273 /* fall through */
2274
2275 case STAT_PX_ST_BE:
2276 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002277 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002278 if (!stats_putchk(rep, htx, &trash))
2279 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002280 }
2281
2282 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2283 /* fall through */
2284
2285 case STAT_PX_ST_END:
2286 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2287 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002288 if (!stats_putchk(rep, htx, &trash))
2289 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002290 }
2291
2292 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2293 /* fall through */
2294
2295 case STAT_PX_ST_FIN:
2296 return 1;
2297
2298 default:
2299 /* unknown state, we should put an abort() here ! */
2300 return 1;
2301 }
Christopher Fauletef779222018-10-31 08:47:01 +01002302
2303 full:
2304 si_rx_room_blk(si);
2305 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002306}
2307
2308/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2309 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2310 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002311static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002312{
2313 /* WARNING! This must fit in the first buffer !!! */
2314 chunk_appendf(&trash,
2315 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2316 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2317 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2318 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2319 "<style type=\"text/css\"><!--\n"
2320 "body {"
2321 " font-family: arial, helvetica, sans-serif;"
2322 " font-size: 12px;"
2323 " font-weight: normal;"
2324 " color: black;"
2325 " background: white;"
2326 "}\n"
2327 "th,td {"
2328 " font-size: 10px;"
2329 "}\n"
2330 "h1 {"
2331 " font-size: x-large;"
2332 " margin-bottom: 0.5em;"
2333 "}\n"
2334 "h2 {"
2335 " font-family: helvetica, arial;"
2336 " font-size: x-large;"
2337 " font-weight: bold;"
2338 " font-style: italic;"
2339 " color: #6020a0;"
2340 " margin-top: 0em;"
2341 " margin-bottom: 0em;"
2342 "}\n"
2343 "h3 {"
2344 " font-family: helvetica, arial;"
2345 " font-size: 16px;"
2346 " font-weight: bold;"
2347 " color: #b00040;"
2348 " background: #e8e8d0;"
2349 " margin-top: 0em;"
2350 " margin-bottom: 0em;"
2351 "}\n"
2352 "li {"
2353 " margin-top: 0.25em;"
2354 " margin-right: 2em;"
2355 "}\n"
2356 ".hr {margin-top: 0.25em;"
2357 " border-color: black;"
2358 " border-bottom-style: solid;"
2359 "}\n"
2360 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2361 ".total {background: #20D0D0;color: #ffff80;}\n"
2362 ".frontend {background: #e8e8d0;}\n"
2363 ".socket {background: #d0d0d0;}\n"
2364 ".backend {background: #e8e8d0;}\n"
2365 ".active_down {background: #ff9090;}\n"
2366 ".active_going_up {background: #ffd020;}\n"
2367 ".active_going_down {background: #ffffa0;}\n"
2368 ".active_up {background: #c0ffc0;}\n"
2369 ".active_nolb {background: #20a0ff;}\n"
2370 ".active_draining {background: #20a0FF;}\n"
2371 ".active_no_check {background: #e0e0e0;}\n"
2372 ".backup_down {background: #ff9090;}\n"
2373 ".backup_going_up {background: #ff80ff;}\n"
2374 ".backup_going_down {background: #c060ff;}\n"
2375 ".backup_up {background: #b0d0ff;}\n"
2376 ".backup_nolb {background: #90b0e0;}\n"
2377 ".backup_draining {background: #cc9900;}\n"
2378 ".backup_no_check {background: #e0e0e0;}\n"
2379 ".maintain {background: #c07820;}\n"
2380 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2381 "\n"
2382 "a.px:link {color: #ffff40; text-decoration: none;}"
2383 "a.px:visited {color: #ffff40; text-decoration: none;}"
2384 "a.px:hover {color: #ffffff; text-decoration: none;}"
2385 "a.lfsb:link {color: #000000; text-decoration: none;}"
2386 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2387 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2388 "\n"
2389 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2390 "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"
2391 "table.tbl td.ac { text-align: center;}\n"
2392 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2393 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2394 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2395 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2396 "\n"
2397 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2398 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2399 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2400 "table.det { border-collapse: collapse; border-style: none; }\n"
2401 "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"
2402 "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"
2403 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2404 "div.tips {\n"
2405 " display:block;\n"
2406 " visibility:hidden;\n"
2407 " z-index:2147483647;\n"
2408 " position:absolute;\n"
2409 " padding:2px 4px 3px;\n"
2410 " background:#f0f060; color:#000000;\n"
2411 " border:1px solid #7040c0;\n"
2412 " white-space:nowrap;\n"
2413 " font-style:normal;font-size:11px;font-weight:normal;\n"
2414 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2415 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2416 "}\n"
2417 "u:hover div.tips {visibility:visible;}\n"
2418 "-->\n"
2419 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002420 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002421 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002422 );
2423}
2424
2425/* Dumps the HTML stats information block to the trash for and uses the state from
2426 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2427 * for clearing the trash if needed.
2428 */
2429static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2430{
2431 struct appctx *appctx = __objt_appctx(si->end);
2432 unsigned int up = (now.tv_sec - start_date.tv_sec);
2433 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002434 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002435 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2436
2437 /* Turn the bytes per second to bits per second and take care of the
2438 * usual ethernet overhead in order to help figure how far we are from
2439 * interface saturation since it's the only case which usually matters.
2440 * For this we count the total size of an Ethernet frame on the wire
2441 * including preamble and IFG (1538) for the largest TCP segment it
2442 * transports (1448 with TCP timestamps). This is not valid for smaller
2443 * packets (under-estimated), but it gives a reasonably accurate
2444 * estimation of how far we are from uplink saturation.
2445 */
2446 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002447
2448 /* WARNING! this has to fit the first packet too.
2449 * We are around 3.5 kB, add adding entries will
2450 * become tricky if we want to support 4kB buffers !
2451 */
2452 chunk_appendf(&trash,
2453 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2454 PRODUCT_NAME "%s</a></h1>\n"
2455 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2456 "<hr width=\"100%%\" class=\"hr\">\n"
2457 "<h3>&gt; General process information</h3>\n"
2458 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002459 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002460 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2461 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2462 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002463 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002464 "Running tasks: %d/%d; idle = %d %%<br>\n"
2465 "</td><td align=\"center\" nowrap>\n"
2466 "<table class=\"lgd\"><tr>\n"
2467 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2468 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2469 "</tr><tr>\n"
2470 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2471 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2472 "</tr><tr>\n"
2473 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2474 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2475 "</tr><tr>\n"
2476 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2477 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2478 "</tr><tr>\n"
2479 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2480 "</tr><tr>\n"
2481 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2482 "</tr></table>\n"
2483 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2484 "</td>"
2485 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2486 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2487 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002488 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2489 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2490 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2491 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2492 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002493 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002494 up / 86400, (up % 86400) / 3600,
2495 (up % 3600) / 60, (up % 60),
2496 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2497 global.rlimit_memmax ? " MB" : "",
2498 global.rlimit_nofile,
2499 global.maxsock, global.maxconn, global.maxpipes,
2500 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002501 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2502 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002503 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002504 );
2505
2506 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002507 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002508 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2509
2510 chunk_appendf(&trash,
2511 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2512 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2513 STAT_SCOPE_TXT_MAXLEN);
2514
2515 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2516 scope_txt[0] = 0;
2517 if (appctx->ctx.stats.scope_len) {
2518 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002519 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002520 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2521 }
2522
2523 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2524 chunk_appendf(&trash,
2525 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2526 uri->uri_prefix,
2527 "",
2528 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2529 scope_txt);
2530 else
2531 chunk_appendf(&trash,
2532 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2533 uri->uri_prefix,
2534 ";up",
2535 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2536 scope_txt);
2537
2538 if (uri->refresh > 0) {
2539 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2540 chunk_appendf(&trash,
2541 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2542 uri->uri_prefix,
2543 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2544 "",
2545 scope_txt);
2546 else
2547 chunk_appendf(&trash,
2548 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2549 uri->uri_prefix,
2550 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2551 ";norefresh",
2552 scope_txt);
2553 }
2554
2555 chunk_appendf(&trash,
2556 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2557 uri->uri_prefix,
2558 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2559 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2560 scope_txt);
2561
2562 chunk_appendf(&trash,
2563 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2564 uri->uri_prefix,
2565 (uri->refresh > 0) ? ";norefresh" : "",
2566 scope_txt);
2567
2568 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002569 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2570 uri->uri_prefix,
2571 (uri->refresh > 0) ? ";norefresh" : "",
2572 scope_txt, uri->uri_prefix);
2573
2574 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002575 "</ul></td>"
2576 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2577 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2578 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2579 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2580 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2581 "</ul>"
2582 "</td>"
2583 "</tr></table>\n"
2584 ""
2585 );
2586
2587 if (appctx->ctx.stats.st_code) {
2588 switch (appctx->ctx.stats.st_code) {
2589 case STAT_STATUS_DONE:
2590 chunk_appendf(&trash,
2591 "<p><div class=active_up>"
2592 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2593 "Action processed successfully."
2594 "</div>\n", uri->uri_prefix,
2595 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2596 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2597 scope_txt);
2598 break;
2599 case STAT_STATUS_NONE:
2600 chunk_appendf(&trash,
2601 "<p><div class=active_going_down>"
2602 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2603 "Nothing has changed."
2604 "</div>\n", uri->uri_prefix,
2605 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2606 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2607 scope_txt);
2608 break;
2609 case STAT_STATUS_PART:
2610 chunk_appendf(&trash,
2611 "<p><div class=active_going_down>"
2612 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2613 "Action partially processed.<br>"
2614 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2615 "</div>\n", uri->uri_prefix,
2616 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2617 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2618 scope_txt);
2619 break;
2620 case STAT_STATUS_ERRP:
2621 chunk_appendf(&trash,
2622 "<p><div class=active_down>"
2623 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2624 "Action not processed because of invalid parameters."
2625 "<ul>"
2626 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002627 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002628 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2629 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2630 "</ul>"
2631 "</div>\n", uri->uri_prefix,
2632 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2633 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2634 scope_txt);
2635 break;
2636 case STAT_STATUS_EXCD:
2637 chunk_appendf(&trash,
2638 "<p><div class=active_down>"
2639 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2640 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2641 "You should retry with less servers at a time.</b>"
2642 "</div>\n", uri->uri_prefix,
2643 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2644 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2645 scope_txt);
2646 break;
2647 case STAT_STATUS_DENY:
2648 chunk_appendf(&trash,
2649 "<p><div class=active_down>"
2650 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2651 "<b>Action denied.</b>"
2652 "</div>\n", uri->uri_prefix,
2653 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2654 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2655 scope_txt);
2656 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002657 case STAT_STATUS_IVAL:
2658 chunk_appendf(&trash,
2659 "<p><div class=active_down>"
2660 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2661 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2662 "</div>\n", uri->uri_prefix,
2663 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2664 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2665 scope_txt);
2666 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002667 default:
2668 chunk_appendf(&trash,
2669 "<p><div class=active_no_check>"
2670 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2671 "Unexpected result."
2672 "</div>\n", uri->uri_prefix,
2673 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2674 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2675 scope_txt);
2676 }
2677 chunk_appendf(&trash, "<p>\n");
2678 }
2679}
2680
2681/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2682 * for clearing the trash if needed.
2683 */
2684static void stats_dump_html_end()
2685{
2686 chunk_appendf(&trash, "</body></html>\n");
2687}
2688
Simon Horman05ee2132017-01-04 09:37:25 +01002689/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2690 * for clearing it if needed.
2691 */
2692static void stats_dump_json_header()
2693{
2694 chunk_strcat(&trash, "[");
2695}
2696
2697
2698/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2699 * for clearing the trash if needed.
2700 */
2701static void stats_dump_json_end()
2702{
2703 chunk_strcat(&trash, "]");
2704}
2705
William Lallemand74c24fb2016-11-21 17:18:36 +01002706/* This function dumps statistics onto the stream interface's read buffer in
2707 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2708 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2709 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2710 * and the stream must be closed, or -1 in case of any error. This function is
2711 * used by both the CLI and the HTTP handlers.
2712 */
Christopher Fauletef779222018-10-31 08:47:01 +01002713static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2714 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002715{
2716 struct appctx *appctx = __objt_appctx(si->end);
2717 struct channel *rep = si_ic(si);
2718 struct proxy *px;
2719
2720 chunk_reset(&trash);
2721
2722 switch (appctx->st2) {
2723 case STAT_ST_INIT:
2724 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2725 /* fall through */
2726
2727 case STAT_ST_HEAD:
2728 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002729 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002730 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2731 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002732 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002733 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002734 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2735 stats_dump_csv_header();
2736
Christopher Fauletef779222018-10-31 08:47:01 +01002737 if (!stats_putchk(rep, htx, &trash))
2738 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002739
Christopher Faulet6338a082019-09-09 15:50:54 +02002740 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2741 appctx->st2 = STAT_ST_FIN;
2742 return 1;
2743 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002744 appctx->st2 = STAT_ST_INFO;
2745 /* fall through */
2746
2747 case STAT_ST_INFO:
2748 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2749 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002750 if (!stats_putchk(rep, htx, &trash))
2751 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002752 }
2753
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002754 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002755 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2756 appctx->st2 = STAT_ST_LIST;
2757 /* fall through */
2758
2759 case STAT_ST_LIST:
2760 /* dump proxies */
2761 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002762 if (htx) {
2763 if (htx_almost_full(htx))
2764 goto full;
2765 }
2766 else {
2767 if (buffer_almost_full(&rep->buf))
2768 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002769 }
2770
2771 px = appctx->ctx.stats.px;
2772 /* skip the disabled proxies, global frontend and non-networked ones */
2773 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002774 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002775 return 0;
2776
2777 appctx->ctx.stats.px = px->next;
2778 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2779 }
2780 /* here, we just have reached the last proxy */
2781
2782 appctx->st2 = STAT_ST_END;
2783 /* fall through */
2784
2785 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002786 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2787 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2788 stats_dump_html_end();
2789 else
2790 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002791 if (!stats_putchk(rep, htx, &trash))
2792 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002793 }
2794
2795 appctx->st2 = STAT_ST_FIN;
2796 /* fall through */
2797
2798 case STAT_ST_FIN:
2799 return 1;
2800
2801 default:
2802 /* unknown state ! */
2803 appctx->st2 = STAT_ST_FIN;
2804 return -1;
2805 }
Christopher Fauletef779222018-10-31 08:47:01 +01002806
2807 full:
2808 si_rx_room_blk(si);
2809 return 0;
2810
William Lallemand74c24fb2016-11-21 17:18:36 +01002811}
2812
2813/* We reached the stats page through a POST request. The appctx is
2814 * expected to have already been allocated by the caller.
2815 * Parse the posted data and enable/disable servers if necessary.
2816 * Returns 1 if request was parsed or zero if it needs more data.
2817 */
2818static int stats_process_http_post(struct stream_interface *si)
2819{
2820 struct stream *s = si_strm(si);
2821 struct appctx *appctx = objt_appctx(si->end);
2822
2823 struct proxy *px = NULL;
2824 struct server *sv = NULL;
2825
2826 char key[LINESIZE];
2827 int action = ST_ADM_ACTION_NONE;
2828 int reprocess = 0;
2829
2830 int total_servers = 0;
2831 int altered_servers = 0;
2832
2833 char *first_param, *cur_param, *next_param, *end_params;
2834 char *st_cur_param = NULL;
2835 char *st_next_param = NULL;
2836
Christopher Fauleta3618372018-12-13 21:59:56 +01002837 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002838
Christopher Fauletb7f88902019-07-15 21:56:43 +02002839 struct htx *htx = htxbuf(&s->req.buf);
2840 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002841
Christopher Fauletb7f88902019-07-15 21:56:43 +02002842 /* we need more data */
2843 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2844 /* check if we can receive more */
2845 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2846 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2847 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002848 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002849 goto wait;
2850 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002851
Christopher Fauletb7f88902019-07-15 21:56:43 +02002852 /* The request was fully received. Copy data */
2853 blk = htx_get_head_blk(htx);
2854 while (blk) {
2855 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002856
Christopher Fauletb7f88902019-07-15 21:56:43 +02002857 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2858 break;
2859 if (type == HTX_BLK_DATA) {
2860 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002861
Christopher Fauletb7f88902019-07-15 21:56:43 +02002862 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002863 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2864 goto out;
2865 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002866 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002867 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002868 }
2869
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002870 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002871 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002872 cur_param = next_param = end_params;
2873 *end_params = '\0';
2874
2875 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2876
2877 /*
2878 * Parse the parameters in reverse order to only store the last value.
2879 * From the html form, the backend and the action are at the end.
2880 */
2881 while (cur_param > first_param) {
2882 char *value;
2883 int poffset, plen;
2884
2885 cur_param--;
2886
2887 if ((*cur_param == '&') || (cur_param == first_param)) {
2888 reprocess_servers:
2889 /* Parse the key */
2890 poffset = (cur_param != first_param ? 1 : 0);
2891 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2892 if ((plen > 0) && (plen <= sizeof(key))) {
2893 strncpy(key, cur_param + poffset, plen);
2894 key[plen - 1] = '\0';
2895 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002896 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002897 goto out;
2898 }
2899
2900 /* Parse the value */
2901 value = key;
2902 while (*value != '\0' && *value != '=') {
2903 value++;
2904 }
2905 if (*value == '=') {
2906 /* Ok, a value is found, we can mark the end of the key */
2907 *value++ = '\0';
2908 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002909 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002910 break;
2911
2912 /* Now we can check the key to see what to do */
2913 if (!px && (strcmp(key, "b") == 0)) {
2914 if ((px = proxy_be_by_name(value)) == NULL) {
2915 /* the backend name is unknown or ambiguous (duplicate names) */
2916 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2917 goto out;
2918 }
2919 }
2920 else if (!action && (strcmp(key, "action") == 0)) {
2921 if (strcmp(value, "ready") == 0) {
2922 action = ST_ADM_ACTION_READY;
2923 }
2924 else if (strcmp(value, "drain") == 0) {
2925 action = ST_ADM_ACTION_DRAIN;
2926 }
2927 else if (strcmp(value, "maint") == 0) {
2928 action = ST_ADM_ACTION_MAINT;
2929 }
2930 else if (strcmp(value, "shutdown") == 0) {
2931 action = ST_ADM_ACTION_SHUTDOWN;
2932 }
2933 else if (strcmp(value, "dhlth") == 0) {
2934 action = ST_ADM_ACTION_DHLTH;
2935 }
2936 else if (strcmp(value, "ehlth") == 0) {
2937 action = ST_ADM_ACTION_EHLTH;
2938 }
2939 else if (strcmp(value, "hrunn") == 0) {
2940 action = ST_ADM_ACTION_HRUNN;
2941 }
2942 else if (strcmp(value, "hnolb") == 0) {
2943 action = ST_ADM_ACTION_HNOLB;
2944 }
2945 else if (strcmp(value, "hdown") == 0) {
2946 action = ST_ADM_ACTION_HDOWN;
2947 }
2948 else if (strcmp(value, "dagent") == 0) {
2949 action = ST_ADM_ACTION_DAGENT;
2950 }
2951 else if (strcmp(value, "eagent") == 0) {
2952 action = ST_ADM_ACTION_EAGENT;
2953 }
2954 else if (strcmp(value, "arunn") == 0) {
2955 action = ST_ADM_ACTION_ARUNN;
2956 }
2957 else if (strcmp(value, "adown") == 0) {
2958 action = ST_ADM_ACTION_ADOWN;
2959 }
2960 /* else these are the old supported methods */
2961 else if (strcmp(value, "disable") == 0) {
2962 action = ST_ADM_ACTION_DISABLE;
2963 }
2964 else if (strcmp(value, "enable") == 0) {
2965 action = ST_ADM_ACTION_ENABLE;
2966 }
2967 else if (strcmp(value, "stop") == 0) {
2968 action = ST_ADM_ACTION_STOP;
2969 }
2970 else if (strcmp(value, "start") == 0) {
2971 action = ST_ADM_ACTION_START;
2972 }
2973 else {
2974 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2975 goto out;
2976 }
2977 }
2978 else if (strcmp(key, "s") == 0) {
2979 if (!(px && action)) {
2980 /*
2981 * Indicates that we'll need to reprocess the parameters
2982 * as soon as backend and action are known
2983 */
2984 if (!reprocess) {
2985 st_cur_param = cur_param;
2986 st_next_param = next_param;
2987 }
2988 reprocess = 1;
2989 }
2990 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002991 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002992 switch (action) {
2993 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002994 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002995 altered_servers++;
2996 total_servers++;
2997 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2998 }
2999 break;
3000 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003001 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003002 altered_servers++;
3003 total_servers++;
3004 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3005 }
3006 break;
3007 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003008 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003009 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3010 altered_servers++;
3011 total_servers++;
3012 }
3013 break;
3014 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003015 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003016 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3017 altered_servers++;
3018 total_servers++;
3019 }
3020 break;
3021 case ST_ADM_ACTION_DHLTH:
3022 if (sv->check.state & CHK_ST_CONFIGURED) {
3023 sv->check.state &= ~CHK_ST_ENABLED;
3024 altered_servers++;
3025 total_servers++;
3026 }
3027 break;
3028 case ST_ADM_ACTION_EHLTH:
3029 if (sv->check.state & CHK_ST_CONFIGURED) {
3030 sv->check.state |= CHK_ST_ENABLED;
3031 altered_servers++;
3032 total_servers++;
3033 }
3034 break;
3035 case ST_ADM_ACTION_HRUNN:
3036 if (!(sv->track)) {
3037 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003038 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003039 altered_servers++;
3040 total_servers++;
3041 }
3042 break;
3043 case ST_ADM_ACTION_HNOLB:
3044 if (!(sv->track)) {
3045 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003046 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003047 altered_servers++;
3048 total_servers++;
3049 }
3050 break;
3051 case ST_ADM_ACTION_HDOWN:
3052 if (!(sv->track)) {
3053 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003054 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003055 altered_servers++;
3056 total_servers++;
3057 }
3058 break;
3059 case ST_ADM_ACTION_DAGENT:
3060 if (sv->agent.state & CHK_ST_CONFIGURED) {
3061 sv->agent.state &= ~CHK_ST_ENABLED;
3062 altered_servers++;
3063 total_servers++;
3064 }
3065 break;
3066 case ST_ADM_ACTION_EAGENT:
3067 if (sv->agent.state & CHK_ST_CONFIGURED) {
3068 sv->agent.state |= CHK_ST_ENABLED;
3069 altered_servers++;
3070 total_servers++;
3071 }
3072 break;
3073 case ST_ADM_ACTION_ARUNN:
3074 if (sv->agent.state & CHK_ST_ENABLED) {
3075 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003076 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003077 altered_servers++;
3078 total_servers++;
3079 }
3080 break;
3081 case ST_ADM_ACTION_ADOWN:
3082 if (sv->agent.state & CHK_ST_ENABLED) {
3083 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003084 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003085 altered_servers++;
3086 total_servers++;
3087 }
3088 break;
3089 case ST_ADM_ACTION_READY:
3090 srv_adm_set_ready(sv);
3091 altered_servers++;
3092 total_servers++;
3093 break;
3094 case ST_ADM_ACTION_DRAIN:
3095 srv_adm_set_drain(sv);
3096 altered_servers++;
3097 total_servers++;
3098 break;
3099 case ST_ADM_ACTION_MAINT:
3100 srv_adm_set_maint(sv);
3101 altered_servers++;
3102 total_servers++;
3103 break;
3104 case ST_ADM_ACTION_SHUTDOWN:
3105 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003106 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003107 altered_servers++;
3108 total_servers++;
3109 }
3110 break;
3111 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003112 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003113 } else {
3114 /* the server name is unknown or ambiguous (duplicate names) */
3115 total_servers++;
3116 }
3117 }
3118 if (reprocess && px && action) {
3119 /* Now, we know the backend and the action chosen by the user.
3120 * We can safely restart from the first server parameter
3121 * to reprocess them
3122 */
3123 cur_param = st_cur_param;
3124 next_param = st_next_param;
3125 reprocess = 0;
3126 goto reprocess_servers;
3127 }
3128
3129 next_param = cur_param;
3130 }
3131 }
3132
3133 if (total_servers == 0) {
3134 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3135 }
3136 else if (altered_servers == 0) {
3137 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3138 }
3139 else if (altered_servers == total_servers) {
3140 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3141 }
3142 else {
3143 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3144 }
3145 out:
3146 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003147 wait:
3148 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3149 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003150}
3151
3152
Christopher Fauletb7f88902019-07-15 21:56:43 +02003153static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003154{
3155 struct stream *s = si_strm(si);
3156 struct uri_auth *uri = s->be->uri_auth;
3157 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003158 struct htx_sl *sl;
3159 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003160
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003161 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);
3162 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3163 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003164 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003165 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003166
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003167 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003168 goto full;
3169 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3170 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3171 goto full;
3172 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003173 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3174 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3175 goto full;
3176 }
Christopher Fauletef779222018-10-31 08:47:01 +01003177 else {
3178 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3179 goto full;
3180 }
3181
3182 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3183 const char *refresh = U2A(uri->refresh);
3184 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3185 goto full;
3186 }
3187
3188 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3189 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3190 goto full;
3191 }
3192
3193 if (!htx_add_endof(htx, HTX_BLK_EOH))
3194 goto full;
3195
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003196 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003197 return 1;
3198
3199 full:
3200 htx_reset(htx);
3201 si_rx_room_blk(si);
3202 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003203}
3204
Christopher Fauletef779222018-10-31 08:47:01 +01003205
Christopher Fauletb7f88902019-07-15 21:56:43 +02003206static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003207{
3208 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3209 struct stream *s = si_strm(si);
3210 struct uri_auth *uri = s->be->uri_auth;
3211 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003212 struct htx_sl *sl;
3213 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003214
3215 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3216 scope_txt[0] = 0;
3217 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003218 const char *scope_ptr = stats_scope_ptr(appctx, si);
3219
Christopher Fauletef779222018-10-31 08:47:01 +01003220 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003221 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003222 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3223 }
3224
3225 /* We don't want to land on the posted stats page because a refresh will
3226 * repost the data. We don't want this to happen on accident so we redirect
3227 * the browse to the stats page with a GET.
3228 */
3229 chunk_printf(&trash, "%s;st=%s%s%s%s",
3230 uri->uri_prefix,
3231 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3232 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3233 stat_status_codes[appctx->ctx.stats.st_code]) ?
3234 stat_status_codes[appctx->ctx.stats.st_code] :
3235 stat_status_codes[STAT_STATUS_UNKN],
3236 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3237 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3238 scope_txt);
3239
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003240 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3241 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3242 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003243 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003244 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003245
3246 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003247 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3248 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3249 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3250 goto full;
3251
3252 if (!htx_add_endof(htx, HTX_BLK_EOH))
3253 goto full;
3254
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003255 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003256 return 1;
3257
3258full:
3259 htx_reset(htx);
3260 si_rx_room_blk(si);
3261 return 0;
3262}
William Lallemand74c24fb2016-11-21 17:18:36 +01003263
Simon Horman05ee2132017-01-04 09:37:25 +01003264
William Lallemand74c24fb2016-11-21 17:18:36 +01003265/* This I/O handler runs as an applet embedded in a stream interface. It is
3266 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3267 * appctx->st0 contains the operation in progress (dump, done). The handler
3268 * automatically unregisters itself once transfer is complete.
3269 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003270static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003271{
3272 struct stream_interface *si = appctx->owner;
3273 struct stream *s = si_strm(si);
3274 struct channel *req = si_oc(si);
3275 struct channel *res = si_ic(si);
3276 struct htx *req_htx, *res_htx;
3277
3278 res_htx = htx_from_buf(&res->buf);
3279
3280 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3281 goto out;
3282
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003283 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003284 if (!b_size(&res->buf)) {
3285 si_rx_room_blk(si);
3286 goto out;
3287 }
3288
3289 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003290 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3291 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003292
3293 /* all states are processed in sequence */
3294 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003295 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003296 if (s->txn->meth == HTTP_METH_HEAD)
3297 appctx->st0 = STAT_HTTP_DONE;
3298 else
3299 appctx->st0 = STAT_HTTP_DUMP;
3300 }
3301 }
3302
3303 if (appctx->st0 == STAT_HTTP_DUMP) {
3304 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3305 appctx->st0 = STAT_HTTP_DONE;
3306 }
3307
3308 if (appctx->st0 == STAT_HTTP_POST) {
3309 if (stats_process_http_post(si))
3310 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003311 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003312 appctx->st0 = STAT_HTTP_DONE;
3313 }
3314
3315 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003316 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003317 appctx->st0 = STAT_HTTP_DONE;
3318 }
3319
3320 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003321 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003322 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3323 si_rx_room_blk(si);
3324 goto out;
3325 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003326 channel_add_input(&s->res, 1);
3327 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003328 }
3329
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003330 if (appctx->st0 == STAT_HTTP_END) {
3331 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003332 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003333 si_shutr(si);
3334 }
3335
3336 /* eat the whole request */
3337 if (co_data(req)) {
3338 req_htx = htx_from_buf(&req->buf);
3339 co_htx_skip(req, req_htx, co_data(req));
3340 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003341 }
3342 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003343
Christopher Fauletef779222018-10-31 08:47:01 +01003344 out:
3345 /* we have left the request in the buffer for the case where we
3346 * process a POST, and this automatically re-enables activity on
3347 * read. It's better to indicate that we want to stop reading when
3348 * we're sending, so that we know there's at most one direction
3349 * deciding to wake the applet up. It saves it from looping when
3350 * emitting large blocks into small TCP windows.
3351 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003352 htx_to_buf(res_htx, &res->buf);
3353 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003354 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003355}
3356
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003357/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003358static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003359 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003360{
3361 int field;
3362
3363 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3364 if (!field_format(info, field))
3365 continue;
3366
Willy Tarreaueaa55372019-10-09 07:39:11 +02003367 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003368 return 0;
3369 if (!stats_emit_raw_data_field(out, &info[field]))
3370 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003371 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3372 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003373 if (!chunk_strcat(out, "\n"))
3374 return 0;
3375 }
3376 return 1;
3377}
3378
3379/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003380static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003381 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003382{
3383 int field;
3384
3385 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3386 if (!field_format(info, field))
3387 continue;
3388
Willy Tarreaueaa55372019-10-09 07:39:11 +02003389 if (!chunk_appendf(out, "%d.%s.%u:", field, info_fields[field].name, info[INF_PROCESS_NUM].u.u32))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003390 return 0;
3391 if (!stats_emit_field_tags(out, &info[field], ':'))
3392 return 0;
3393 if (!stats_emit_typed_data_field(out, &info[field]))
3394 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003395 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3396 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003397 if (!chunk_strcat(out, "\n"))
3398 return 0;
3399 }
3400 return 1;
3401}
3402
3403/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003404 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003405 * INF_TOTAL_FIELDS. If this length is less then this value, the
3406 * function returns 0, otherwise, it returns 1.
3407 */
3408int stats_fill_info(struct field *info, int len)
3409{
3410 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003411 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003412
3413#ifdef USE_OPENSSL
3414 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3415 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3416 int ssl_reuse = 0;
3417
3418 if (ssl_key_rate < ssl_sess_rate) {
3419 /* count the ssl reuse ratio and avoid overflows in both directions */
3420 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3421 }
3422#endif
3423
3424 if (len < INF_TOTAL_FIELDS)
3425 return 0;
3426
3427 chunk_reset(out);
3428 memset(info, 0, sizeof(*info) * len);
3429
3430 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003431 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3432 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003433
Yves Lafon95317282018-02-26 11:10:37 +01003434 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003435 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3436 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3437 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3438
3439 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3440 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3441
3442 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3443 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3444 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3445 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3446 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3447 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3448 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3449 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3450 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3451 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3452 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3453 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3454#ifdef USE_OPENSSL
3455 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3456 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3457 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3458#endif
3459 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3460 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3461 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3462 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3463 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3464 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3465 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3466 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3467 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3468
3469#ifdef USE_OPENSSL
3470 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3471 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3472 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3473 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3474 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3475 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3476 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3477 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3478 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3479 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3480#endif
3481 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3482 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3483 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3484#ifdef USE_ZLIB
3485 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3486 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3487#endif
3488 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003489 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003490 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003491 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3492 if (global.desc)
3493 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003494 info[INF_STOPPING] = mkf_u32(0, stopping);
3495 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003496 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003497 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003498 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003499 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003500 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003501 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003502 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003503 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3504 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 +02003505 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003506
3507 return 1;
3508}
3509
3510/* This function dumps information onto the stream interface's read buffer.
3511 * It returns 0 as long as it does not complete, non-zero upon completion.
3512 * No state is used.
3513 */
3514static int stats_dump_info_to_buffer(struct stream_interface *si)
3515{
3516 struct appctx *appctx = __objt_appctx(si->end);
3517
3518 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3519 return 0;
3520
3521 chunk_reset(&trash);
3522
3523 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003524 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003525 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003526 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003527 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003528 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003529
Willy Tarreau06d80a92017-10-19 14:32:15 +02003530 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003531 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003532 return 0;
3533 }
3534
3535 return 1;
3536}
3537
Simon Horman6f6bb382017-01-04 09:37:26 +01003538/* This function dumps the schema onto the stream interface's read buffer.
3539 * It returns 0 as long as it does not complete, non-zero upon completion.
3540 * No state is used.
3541 *
3542 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3543 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3544 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003545static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003546{
3547
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003548 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003549
3550 chunk_strcat(out,
3551 "{"
3552 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3553 "\"oneOf\":["
3554 "{"
3555 "\"title\":\"Info\","
3556 "\"type\":\"array\","
3557 "\"items\":{"
3558 "\"properties\":{"
3559 "\"title\":\"InfoItem\","
3560 "\"type\":\"object\","
3561 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3562 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3563 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3564 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3565 "},"
3566 "\"required\":[\"field\",\"processNum\",\"tags\","
3567 "\"value\"]"
3568 "}"
3569 "},"
3570 "{"
3571 "\"title\":\"Stat\","
3572 "\"type\":\"array\","
3573 "\"items\":{"
3574 "\"title\":\"InfoItem\","
3575 "\"type\":\"object\","
3576 "\"properties\":{"
3577 "\"objType\":{"
3578 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3579 "\"Server\",\"Unknown\"]"
3580 "},"
3581 "\"proxyId\":{"
3582 "\"type\":\"integer\","
3583 "\"minimum\":0"
3584 "},"
3585 "\"id\":{"
3586 "\"type\":\"integer\","
3587 "\"minimum\":0"
3588 "},"
3589 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3590 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3591 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3592 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3593 "},"
3594 "\"required\":[\"objType\",\"proxyId\",\"id\","
3595 "\"field\",\"processNum\",\"tags\","
3596 "\"value\"]"
3597 "}"
3598 "},"
3599 "{"
3600 "\"title\":\"Error\","
3601 "\"type\":\"object\","
3602 "\"properties\":{"
3603 "\"errorStr\":{"
3604 "\"type\":\"string\""
3605 "},"
3606 "\"required\":[\"errorStr\"]"
3607 "}"
3608 "}"
3609 "],"
3610 "\"definitions\":{"
3611 "\"field\":{"
3612 "\"type\":\"object\","
3613 "\"pos\":{"
3614 "\"type\":\"integer\","
3615 "\"minimum\":0"
3616 "},"
3617 "\"name\":{"
3618 "\"type\":\"string\""
3619 "},"
3620 "\"required\":[\"pos\",\"name\"]"
3621 "},"
3622 "\"processNum\":{"
3623 "\"type\":\"integer\","
3624 "\"minimum\":1"
3625 "},"
3626 "\"tags\":{"
3627 "\"type\":\"object\","
3628 "\"origin\":{"
3629 "\"type\":\"string\","
3630 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3631 "\"Config\",\"Product\",\"Unknown\"]"
3632 "},"
3633 "\"nature\":{"
3634 "\"type\":\"string\","
3635 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3636 "\"Rate\",\"Counter\",\"Duration\","
3637 "\"Age\",\"Time\",\"Name\",\"Output\","
3638 "\"Avg\", \"Unknown\"]"
3639 "},"
3640 "\"scope\":{"
3641 "\"type\":\"string\","
3642 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3643 "\"System\",\"Unknown\"]"
3644 "},"
3645 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3646 "},"
3647 "\"typedValue\":{"
3648 "\"type\":\"object\","
3649 "\"oneOf\":["
3650 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3651 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3652 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3653 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3654 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3655 "],"
3656 "\"definitions\":{"
3657 "\"s32Value\":{"
3658 "\"properties\":{"
3659 "\"type\":{"
3660 "\"type\":\"string\","
3661 "\"enum\":[\"s32\"]"
3662 "},"
3663 "\"value\":{"
3664 "\"type\":\"integer\","
3665 "\"minimum\":-2147483648,"
3666 "\"maximum\":2147483647"
3667 "}"
3668 "},"
3669 "\"required\":[\"type\",\"value\"]"
3670 "},"
3671 "\"s64Value\":{"
3672 "\"properties\":{"
3673 "\"type\":{"
3674 "\"type\":\"string\","
3675 "\"enum\":[\"s64\"]"
3676 "},"
3677 "\"value\":{"
3678 "\"type\":\"integer\","
3679 "\"minimum\":-9007199254740991,"
3680 "\"maximum\":9007199254740991"
3681 "}"
3682 "},"
3683 "\"required\":[\"type\",\"value\"]"
3684 "},"
3685 "\"u32Value\":{"
3686 "\"properties\":{"
3687 "\"type\":{"
3688 "\"type\":\"string\","
3689 "\"enum\":[\"u32\"]"
3690 "},"
3691 "\"value\":{"
3692 "\"type\":\"integer\","
3693 "\"minimum\":0,"
3694 "\"maximum\":4294967295"
3695 "}"
3696 "},"
3697 "\"required\":[\"type\",\"value\"]"
3698 "},"
3699 "\"u64Value\":{"
3700 "\"properties\":{"
3701 "\"type\":{"
3702 "\"type\":\"string\","
3703 "\"enum\":[\"u64\"]"
3704 "},"
3705 "\"value\":{"
3706 "\"type\":\"integer\","
3707 "\"minimum\":0,"
3708 "\"maximum\":9007199254740991"
3709 "}"
3710 "},"
3711 "\"required\":[\"type\",\"value\"]"
3712 "},"
3713 "\"strValue\":{"
3714 "\"properties\":{"
3715 "\"type\":{"
3716 "\"type\":\"string\","
3717 "\"enum\":[\"str\"]"
3718 "},"
3719 "\"value\":{\"type\":\"string\"}"
3720 "},"
3721 "\"required\":[\"type\",\"value\"]"
3722 "},"
3723 "\"unknownValue\":{"
3724 "\"properties\":{"
3725 "\"type\":{"
3726 "\"type\":\"integer\","
3727 "\"minimum\":0"
3728 "},"
3729 "\"value\":{"
3730 "\"type\":\"string\","
3731 "\"enum\":[\"unknown\"]"
3732 "}"
3733 "},"
3734 "\"required\":[\"type\",\"value\"]"
3735 "}"
3736 "}"
3737 "}"
3738 "}"
3739 "}");
3740
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003741 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003742 chunk_reset(out);
3743 chunk_appendf(out,
3744 "{\"errorStr\":\"output buffer too short\"}");
3745 }
3746}
3747
3748/* This function dumps the schema onto the stream interface's read buffer.
3749 * It returns 0 as long as it does not complete, non-zero upon completion.
3750 * No state is used.
3751 */
3752static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3753{
3754 chunk_reset(&trash);
3755
3756 stats_dump_json_schema(&trash);
3757
Willy Tarreau06d80a92017-10-19 14:32:15 +02003758 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003759 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003760 return 0;
3761 }
3762
3763 return 1;
3764}
3765
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003766static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003767{
3768 struct proxy *px;
3769 struct server *sv;
3770 struct listener *li;
3771 int clrall = 0;
3772
3773 if (strcmp(args[2], "all") == 0)
3774 clrall = 1;
3775
3776 /* check permissions */
3777 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3778 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3779 return 1;
3780
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003781 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003782 if (clrall) {
3783 memset(&px->be_counters, 0, sizeof(px->be_counters));
3784 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3785 }
3786 else {
3787 px->be_counters.conn_max = 0;
3788 px->be_counters.p.http.rps_max = 0;
3789 px->be_counters.sps_max = 0;
3790 px->be_counters.cps_max = 0;
3791 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003792 px->be_counters.qtime_max = 0;
3793 px->be_counters.ctime_max = 0;
3794 px->be_counters.dtime_max = 0;
3795 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003796
3797 px->fe_counters.conn_max = 0;
3798 px->fe_counters.p.http.rps_max = 0;
3799 px->fe_counters.sps_max = 0;
3800 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003801 }
3802
3803 for (sv = px->srv; sv; sv = sv->next)
3804 if (clrall)
3805 memset(&sv->counters, 0, sizeof(sv->counters));
3806 else {
3807 sv->counters.cur_sess_max = 0;
3808 sv->counters.nbpend_max = 0;
3809 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003810 sv->counters.qtime_max = 0;
3811 sv->counters.ctime_max = 0;
3812 sv->counters.dtime_max = 0;
3813 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003814 }
3815
3816 list_for_each_entry(li, &px->conf.listeners, by_fe)
3817 if (li->counters) {
3818 if (clrall)
3819 memset(li->counters, 0, sizeof(*li->counters));
3820 else
3821 li->counters->conn_max = 0;
3822 }
3823 }
3824
3825 global.cps_max = 0;
3826 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003827 global.ssl_max = 0;
3828 global.ssl_fe_keys_max = 0;
3829 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003830
3831 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003832 return 1;
3833}
3834
3835
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003836static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003837{
Willy Tarreau2f397382019-10-09 11:43:59 +02003838 int arg = 2;
3839
Willy Tarreaud25fc792016-12-16 12:33:47 +01003840 appctx->ctx.stats.scope_str = 0;
3841 appctx->ctx.stats.scope_len = 0;
3842 appctx->ctx.stats.flags = 0;
3843
Willy Tarreau2f397382019-10-09 11:43:59 +02003844 while (*args[arg]) {
3845 if (strcmp(args[arg], "typed") == 0)
3846 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3847 else if (strcmp(args[arg], "json") == 0)
3848 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3849 else if (strcmp(args[arg], "desc") == 0)
3850 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3851 arg++;
3852 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003853 return 0;
3854}
3855
3856
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003857static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003858{
Willy Tarreau2f397382019-10-09 11:43:59 +02003859 int arg = 2;
3860
Willy Tarreaud25fc792016-12-16 12:33:47 +01003861 appctx->ctx.stats.scope_str = 0;
3862 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003863 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3864
3865 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3866 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003867
Willy Tarreau2f397382019-10-09 11:43:59 +02003868 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003869 struct proxy *px;
3870
Willy Tarreau2f397382019-10-09 11:43:59 +02003871 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003872 if (px)
3873 appctx->ctx.stats.iid = px->uuid;
3874 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003875 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003876
Willy Tarreau9d008692019-08-09 11:21:01 +02003877 if (!appctx->ctx.stats.iid)
3878 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003879
Willy Tarreau2b812e22016-11-22 16:18:05 +01003880 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003881 appctx->ctx.stats.type = atoi(args[arg+1]);
3882 appctx->ctx.stats.sid = atoi(args[arg+2]);
3883 arg += 3;
3884 }
3885
3886 while (*args[arg]) {
3887 if (strcmp(args[arg], "typed") == 0)
3888 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3889 else if (strcmp(args[arg], "json") == 0)
3890 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3891 else if (strcmp(args[arg], "desc") == 0)
3892 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3893 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003894 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003895
Willy Tarreau2b812e22016-11-22 16:18:05 +01003896 return 0;
3897}
3898
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003899static int cli_io_handler_dump_info(struct appctx *appctx)
3900{
3901 return stats_dump_info_to_buffer(appctx->owner);
3902}
3903
Willy Tarreau2b812e22016-11-22 16:18:05 +01003904/* This I/O handler runs as an applet embedded in a stream interface. It is
3905 * used to send raw stats over a socket.
3906 */
3907static int cli_io_handler_dump_stat(struct appctx *appctx)
3908{
Christopher Fauletef779222018-10-31 08:47:01 +01003909 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003910}
3911
Simon Horman6f6bb382017-01-04 09:37:26 +01003912static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3913{
3914 return stats_dump_json_schema_to_buffer(appctx->owner);
3915}
3916
Willy Tarreau2b812e22016-11-22 16:18:05 +01003917/* register cli keywords */
3918static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003919 { { "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 +02003920 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3921 { { "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 +01003922 { { "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 +01003923 {{},}
3924}};
3925
Willy Tarreau0108d902018-11-25 19:14:37 +01003926INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3927
William Lallemand74c24fb2016-11-21 17:18:36 +01003928struct applet http_stats_applet = {
3929 .obj_type = OBJ_TYPE_APPLET,
3930 .name = "<STATS>", /* used for logging */
3931 .fct = http_stats_io_handler,
3932 .release = NULL,
3933};
3934
William Lallemand74c24fb2016-11-21 17:18:36 +01003935/*
3936 * Local variables:
3937 * c-indent-level: 8
3938 * c-basic-offset: 8
3939 * End:
3940 */