blob: 40ea9c8bea861901ba1eeb74ff3afec412478ade [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 Tarreau3f0f82e2020-06-04 19:42:41 +020027#include <haproxy/applet-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020028#include <haproxy/api.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010029#include <common/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020030#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020031#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020032#include <haproxy/cli.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020033#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020034#include <haproxy/debug.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020035#include <haproxy/dns.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020036#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020037#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020038#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020039#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020040#include <haproxy/htx.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020041#include <haproxy/pool.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020042#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020043#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020044#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020045#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020046#include <haproxy/pattern-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020047#include <haproxy/session.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020048#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020049#include <haproxy/stats.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020050#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020051#include <haproxy/task.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020052#include <haproxy/tools.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020053#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020054#include <haproxy/time.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020055#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020056#include <haproxy/version.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020057#include <haproxy/base64.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010058
William Lallemand74c24fb2016-11-21 17:18:36 +010059#include <proto/backend.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020060#include <haproxy/fd.h>
Willy Tarreau66347942020-06-01 12:18:08 +020061#include <haproxy/freq_ctr.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020062#include <haproxy/pipe.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010063#include <proto/proxy.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010064#include <proto/stream.h>
65#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066
Christopher Faulet2b9b6782019-02-27 16:42:58 +010067/* status codes available for the stats admin page (strictly 4 chars length) */
68const char *stat_status_codes[STAT_STATUS_SIZE] = {
69 [STAT_STATUS_DENY] = "DENY",
70 [STAT_STATUS_DONE] = "DONE",
71 [STAT_STATUS_ERRP] = "ERRP",
72 [STAT_STATUS_EXCD] = "EXCD",
73 [STAT_STATUS_NONE] = "NONE",
74 [STAT_STATUS_PART] = "PART",
75 [STAT_STATUS_UNKN] = "UNKN",
76 [STAT_STATUS_IVAL] = "IVAL",
77};
78
Willy Tarreau0baac8c2016-11-22 16:36:53 +010079/* These are the field names for each INF_* field position. Please pay attention
80 * to always use the exact same name except that the strings for new names must
81 * be lower case or CamelCase while the enum entries must be upper case.
82 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020083const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020084 [INF_NAME] = { .name = "Name", .desc = "Product name" },
85 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
86 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
87 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
88 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
89 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
90 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
91 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
92 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
93 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
94 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
95 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
96 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
97 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
98 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
99 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
100 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
101 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
102 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
103 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
104 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
105 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
106 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
107 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
108 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
109 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
110 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
111 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
112 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
113 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
114 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
115 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
116 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
117 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
118 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
119 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
120 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
121 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
122 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
123 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
124 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
125 [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" },
126 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
127 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
128 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
129 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
130 [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)" },
131 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
132 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
133 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
134 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
135 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
136 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
137 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
138 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
139 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
140 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
141 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
142 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
143 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
144 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
145 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
146 [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 +0200147 [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 +0100148};
149
Willy Tarreaueaa55372019-10-09 07:39:11 +0200150const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200151 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
152 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
153 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
154 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
155 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
156 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
157 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
158 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
159 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
160 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
161 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
162 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
163 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
164 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
165 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
166 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
167 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
168 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
169 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
170 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
171 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
172 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
173 [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" },
174 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
175 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
176 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
177 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
178 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
179 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
180 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
181 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
182 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
183 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
184 [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)" },
185 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
186 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
187 [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" },
188 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
189 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
190 [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" },
191 [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" },
192 [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" },
193 [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" },
194 [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" },
195 [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)" },
196 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
197 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
198 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
199 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
200 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
201 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
202 [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" },
203 [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" },
204 [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)" },
205 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
206 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
207 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
208 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
209 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
210 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
211 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
212 [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)" },
213 [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" },
214 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
215 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
216 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
217 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
218 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
219 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
220 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
221 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
222 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
223 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
224 [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" },
225 [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" },
226 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
227 [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" },
228 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
229 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
230 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
231 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
232 [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" },
233 [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" },
234 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
235 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
236 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
237 [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" },
238 [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" },
239 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
240 [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 +0100241 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
242 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
243 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
244 [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 +0100245 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100246};
247
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100248/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100249static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100250/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100251static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100252
Christopher Faulet6338a082019-09-09 15:50:54 +0200253static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100254
Christopher Fauletef779222018-10-31 08:47:01 +0100255static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
256{
257 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100258 if (chk->data >= channel_htx_recv_max(chn, htx))
259 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200260 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100261 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100262 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100263 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100264 }
265 else {
266 if (ci_putchk(chn, chk) == -1)
267 return 0;
268 }
269 return 1;
270}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100271
Christopher Fauleted7a0662019-01-14 11:07:34 +0100272static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
273{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200274 struct channel *req = si_oc(si);
275 struct htx *htx = htxbuf(&req->buf);
276 struct htx_blk *blk;
277 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100278
Christopher Fauletb7f88902019-07-15 21:56:43 +0200279 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100280 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200281 ALREADY_CHECKED(blk);
282 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
283 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100284}
285
William Lallemand74c24fb2016-11-21 17:18:36 +0100286/*
287 * http_stats_io_handler()
288 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
289 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100290 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100291 * -> stats_dump_html_head() // emits the HTML headers
292 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
293 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
294 * -> stats_dump_html_px_hdr()
295 * -> stats_dump_fe_stats()
296 * -> stats_dump_li_stats()
297 * -> stats_dump_sv_stats()
298 * -> stats_dump_be_stats()
299 * -> stats_dump_html_px_end()
300 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100301 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100302 */
303
304
William Lallemand74c24fb2016-11-21 17:18:36 +0100305/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
306 * for clearing it if needed.
307 * NOTE: Some tools happen to rely on the field position instead of its name,
308 * so please only append new fields at the end, never in the middle.
309 */
310static void stats_dump_csv_header()
311{
312 int field;
313
314 chunk_appendf(&trash, "# ");
315 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200316 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100317
318 chunk_appendf(&trash, "\n");
319}
320
321
322/* Emits a stats field without any surrounding element and properly encoded to
323 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
324 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200325int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100326{
327 switch (field_format(f, 0)) {
328 case FF_EMPTY: return 1;
329 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
330 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
331 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
332 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200333 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100334 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
335 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
336 }
337}
338
339/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
340 * output is supposed to be used on its own line. Returns non-zero on success, 0
341 * if the buffer is full.
342 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200343int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100344{
345 switch (field_format(f, 0)) {
346 case FF_EMPTY: return 1;
347 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
348 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
349 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
350 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200351 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100352 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
353 default: return chunk_appendf(out, "%08x:?", f->type);
354 }
355}
356
Simon Horman05ee2132017-01-04 09:37:25 +0100357/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
358 * the recommendation for interoperable integers in section 6 of RFC 7159.
359 */
360#define JSON_INT_MAX ((1ULL << 53) - 1)
361#define JSON_INT_MIN (0 - JSON_INT_MAX)
362
363/* Emits a stats field value and its type in JSON.
364 * Returns non-zero on success, 0 on error.
365 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200366int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100367{
368 int old_len;
369 char buf[20];
370 const char *type, *value = buf, *quote = "";
371
372 switch (field_format(f, 0)) {
373 case FF_EMPTY: return 1;
374 case FF_S32: type = "\"s32\"";
375 snprintf(buf, sizeof(buf), "%d", f->u.s32);
376 break;
377 case FF_U32: type = "\"u32\"";
378 snprintf(buf, sizeof(buf), "%u", f->u.u32);
379 break;
380 case FF_S64: type = "\"s64\"";
381 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
382 return 0;
383 type = "\"s64\"";
384 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
385 break;
386 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
387 return 0;
388 type = "\"u64\"";
389 snprintf(buf, sizeof(buf), "%llu",
390 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200391 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200392 case FF_FLT: type = "\"flt\"";
393 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100394 break;
395 case FF_STR: type = "\"str\"";
396 value = field_str(f, 0);
397 quote = "\"";
398 break;
399 default: snprintf(buf, sizeof(buf), "%u", f->type);
400 type = buf;
401 value = "unknown";
402 quote = "\"";
403 break;
404 }
405
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200406 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100407 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
408 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200409 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100410}
411
William Lallemand74c24fb2016-11-21 17:18:36 +0100412/* Emits an encoding of the field type on 3 characters followed by a delimiter.
413 * Returns non-zero on success, 0 if the buffer is full.
414 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200415int stats_emit_field_tags(struct buffer *out, const struct field *f,
416 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100417{
418 char origin, nature, scope;
419
420 switch (field_origin(f, 0)) {
421 case FO_METRIC: origin = 'M'; break;
422 case FO_STATUS: origin = 'S'; break;
423 case FO_KEY: origin = 'K'; break;
424 case FO_CONFIG: origin = 'C'; break;
425 case FO_PRODUCT: origin = 'P'; break;
426 default: origin = '?'; break;
427 }
428
429 switch (field_nature(f, 0)) {
430 case FN_GAUGE: nature = 'G'; break;
431 case FN_LIMIT: nature = 'L'; break;
432 case FN_MIN: nature = 'm'; break;
433 case FN_MAX: nature = 'M'; break;
434 case FN_RATE: nature = 'R'; break;
435 case FN_COUNTER: nature = 'C'; break;
436 case FN_DURATION: nature = 'D'; break;
437 case FN_AGE: nature = 'A'; break;
438 case FN_TIME: nature = 'T'; break;
439 case FN_NAME: nature = 'N'; break;
440 case FN_OUTPUT: nature = 'O'; break;
441 case FN_AVG: nature = 'a'; break;
442 default: nature = '?'; break;
443 }
444
445 switch (field_scope(f, 0)) {
446 case FS_PROCESS: scope = 'P'; break;
447 case FS_SERVICE: scope = 'S'; break;
448 case FS_SYSTEM: scope = 's'; break;
449 case FS_CLUSTER: scope = 'C'; break;
450 default: scope = '?'; break;
451 }
452
453 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
454}
455
Simon Horman05ee2132017-01-04 09:37:25 +0100456/* Emits an encoding of the field type as JSON.
457 * Returns non-zero on success, 0 if the buffer is full.
458 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200459int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100460{
461 const char *origin, *nature, *scope;
462 int old_len;
463
464 switch (field_origin(f, 0)) {
465 case FO_METRIC: origin = "Metric"; break;
466 case FO_STATUS: origin = "Status"; break;
467 case FO_KEY: origin = "Key"; break;
468 case FO_CONFIG: origin = "Config"; break;
469 case FO_PRODUCT: origin = "Product"; break;
470 default: origin = "Unknown"; break;
471 }
472
473 switch (field_nature(f, 0)) {
474 case FN_GAUGE: nature = "Gauge"; break;
475 case FN_LIMIT: nature = "Limit"; break;
476 case FN_MIN: nature = "Min"; break;
477 case FN_MAX: nature = "Max"; break;
478 case FN_RATE: nature = "Rate"; break;
479 case FN_COUNTER: nature = "Counter"; break;
480 case FN_DURATION: nature = "Duration"; break;
481 case FN_AGE: nature = "Age"; break;
482 case FN_TIME: nature = "Time"; break;
483 case FN_NAME: nature = "Name"; break;
484 case FN_OUTPUT: nature = "Output"; break;
485 case FN_AVG: nature = "Avg"; break;
486 default: nature = "Unknown"; break;
487 }
488
489 switch (field_scope(f, 0)) {
490 case FS_PROCESS: scope = "Process"; break;
491 case FS_SERVICE: scope = "Service"; break;
492 case FS_SYSTEM: scope = "System"; break;
493 case FS_CLUSTER: scope = "Cluster"; break;
494 default: scope = "Unknown"; break;
495 }
496
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200497 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100498 chunk_appendf(out, "\"tags\":{"
499 "\"origin\":\"%s\","
500 "\"nature\":\"%s\","
501 "\"scope\":\"%s\""
502 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100504}
William Lallemand74c24fb2016-11-21 17:18:36 +0100505
506/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200507static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200508 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100509{
510 int field;
511
512 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
513 if (!stats_emit_raw_data_field(out, &stats[field]))
514 return 0;
515 if (!chunk_strcat(out, ","))
516 return 0;
517 }
518 chunk_strcat(out, "\n");
519 return 1;
520}
521
522/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200523static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200524 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100525{
526 int field;
527
528 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
529 if (!stats[field].type)
530 continue;
531
532 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
533 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
534 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
535 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
536 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
537 '?',
538 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200539 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100540
541 if (!stats_emit_field_tags(out, &stats[field], ':'))
542 return 0;
543 if (!stats_emit_typed_data_field(out, &stats[field]))
544 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200545 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
546 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100547 if (!chunk_strcat(out, "\n"))
548 return 0;
549 }
550 return 1;
551}
552
Simon Horman05ee2132017-01-04 09:37:25 +0100553/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200554static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200555 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100556{
557 int field;
558 int started = 0;
559
560 if (!chunk_strcat(out, "["))
561 return 0;
562
563 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
564 int old_len;
565
566 if (!field_format(info, field))
567 continue;
568
569 if (started && !chunk_strcat(out, ","))
570 goto err;
571 started = 1;
572
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200573 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100574 chunk_appendf(out,
575 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
576 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200577 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100578 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200579 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100580 goto err;
581
582 if (!stats_emit_json_field_tags(out, &info[field]))
583 goto err;
584
585 if (!stats_emit_json_data_field(out, &info[field]))
586 goto err;
587
588 if (!chunk_strcat(out, "}"))
589 goto err;
590 }
591
592 if (!chunk_strcat(out, "]"))
593 goto err;
594 return 1;
595
596err:
597 chunk_reset(out);
598 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
599 return 0;
600}
601
602/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200603static int stats_dump_fields_json(struct buffer *out,
604 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200605 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100606{
607 int field;
608 int started = 0;
609
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200610 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100611 return 0;
612 if (!chunk_strcat(out, "["))
613 return 0;
614
615 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
616 const char *obj_type;
617 int old_len;
618
619 if (!stats[field].type)
620 continue;
621
622 if (started && !chunk_strcat(out, ","))
623 goto err;
624 started = 1;
625
626 switch (stats[ST_F_TYPE].u.u32) {
627 case STATS_TYPE_FE: obj_type = "Frontend"; break;
628 case STATS_TYPE_BE: obj_type = "Backend"; break;
629 case STATS_TYPE_SO: obj_type = "Listener"; break;
630 case STATS_TYPE_SV: obj_type = "Server"; break;
631 default: obj_type = "Unknown"; break;
632 }
633
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200634 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100635 chunk_appendf(out,
636 "{"
637 "\"objType\":\"%s\","
638 "\"proxyId\":%d,"
639 "\"id\":%d,"
640 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
641 "\"processNum\":%u,",
642 obj_type, stats[ST_F_IID].u.u32,
643 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200644 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200645 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100646 goto err;
647
648 if (!stats_emit_json_field_tags(out, &stats[field]))
649 goto err;
650
651 if (!stats_emit_json_data_field(out, &stats[field]))
652 goto err;
653
654 if (!chunk_strcat(out, "}"))
655 goto err;
656 }
657
658 if (!chunk_strcat(out, "]"))
659 goto err;
660
661 return 1;
662
663err:
664 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200665 if (flags & STAT_STARTED)
666 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100667 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
668 return 0;
669}
670
William Lallemand74c24fb2016-11-21 17:18:36 +0100671/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200672 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200673 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100674 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200675static int stats_dump_fields_html(struct buffer *out,
676 const struct field *stats,
677 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100678{
Willy Tarreau83061a82018-07-13 11:56:34 +0200679 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100680
681 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
682 chunk_appendf(out,
683 /* name, queue */
684 "<tr class=\"frontend\">");
685
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200686 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100687 /* Column sub-heading for Enable or Disable server */
688 chunk_appendf(out, "<td></td>");
689 }
690
691 chunk_appendf(out,
692 "<td class=ac>"
693 "<a name=\"%s/Frontend\"></a>"
694 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
695 "<td colspan=3></td>"
696 "",
697 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
698
699 chunk_appendf(out,
700 /* sessions rate : current */
701 "<td><u>%s<div class=tips><table class=det>"
702 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
703 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
704 "",
705 U2H(stats[ST_F_RATE].u.u32),
706 U2H(stats[ST_F_CONN_RATE].u.u32),
707 U2H(stats[ST_F_RATE].u.u32));
708
709 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
710 chunk_appendf(out,
711 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
712 U2H(stats[ST_F_REQ_RATE].u.u32));
713
714 chunk_appendf(out,
715 "</table></div></u></td>"
716 /* sessions rate : max */
717 "<td><u>%s<div class=tips><table class=det>"
718 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
719 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
720 "",
721 U2H(stats[ST_F_RATE_MAX].u.u32),
722 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
723 U2H(stats[ST_F_RATE_MAX].u.u32));
724
725 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
726 chunk_appendf(out,
727 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
728 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
729
730 chunk_appendf(out,
731 "</table></div></u></td>"
732 /* sessions rate : limit */
733 "<td>%s</td>",
734 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
735
736 chunk_appendf(out,
737 /* sessions: current, max, limit, total */
738 "<td>%s</td><td>%s</td><td>%s</td>"
739 "<td><u>%s<div class=tips><table class=det>"
740 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
741 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
742 "",
743 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
744 U2H(stats[ST_F_STOT].u.u64),
745 U2H(stats[ST_F_CONN_TOT].u.u64),
746 U2H(stats[ST_F_STOT].u.u64));
747
748 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
749 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
750 chunk_appendf(out,
751 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
752 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
753 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
754 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
755 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
756 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
757 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
758 "<tr><th>- other responses:</th><td>%s</td></tr>"
759 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100760 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
761 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200762 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100763 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100764 "",
765 U2H(stats[ST_F_REQ_TOT].u.u64),
766 U2H(stats[ST_F_HRSP_1XX].u.u64),
767 U2H(stats[ST_F_HRSP_2XX].u.u64),
768 U2H(stats[ST_F_COMP_RSP].u.u64),
769 stats[ST_F_HRSP_2XX].u.u64 ?
770 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
771 U2H(stats[ST_F_HRSP_3XX].u.u64),
772 U2H(stats[ST_F_HRSP_4XX].u.u64),
773 U2H(stats[ST_F_HRSP_5XX].u.u64),
774 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200775 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100776 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
777 U2H(stats[ST_F_CACHE_HITS].u.u64),
778 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
779 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100780 U2H(stats[ST_F_WREW].u.u64),
781 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100782 }
783
784 chunk_appendf(out,
785 "</table></div></u></td>"
786 /* sessions: lbtot, lastsess */
787 "<td></td><td></td>"
788 /* bytes : in */
789 "<td>%s</td>"
790 "",
791 U2H(stats[ST_F_BIN].u.u64));
792
793 chunk_appendf(out,
794 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
795 "<td>%s%s<div class=tips><table class=det>"
796 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
797 "<tr><th>Compression in:</th><td>%s</td></tr>"
798 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
799 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
800 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
801 "</table></div>%s</td>",
802 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
803 U2H(stats[ST_F_BOUT].u.u64),
804 U2H(stats[ST_F_BOUT].u.u64),
805 U2H(stats[ST_F_COMP_IN].u.u64),
806 U2H(stats[ST_F_COMP_OUT].u.u64),
807 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
808 U2H(stats[ST_F_COMP_BYP].u.u64),
809 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
810 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,
811 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
812
813 chunk_appendf(out,
814 /* denied: req, resp */
815 "<td>%s</td><td>%s</td>"
816 /* errors : request, connect, response */
817 "<td>%s</td><td></td><td></td>"
818 /* warnings: retries, redispatches */
819 "<td></td><td></td>"
820 /* server status : reflect frontend status */
821 "<td class=ac>%s</td>"
822 /* rest of server: nothing */
823 "<td class=ac colspan=8></td></tr>"
824 "",
825 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
826 U2H(stats[ST_F_EREQ].u.u64),
827 field_str(stats, ST_F_STATUS));
828 }
829 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
830 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200831 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100832 /* Column sub-heading for Enable or Disable server */
833 chunk_appendf(out, "<td></td>");
834 }
835
836 chunk_appendf(out,
837 /* frontend name, listener name */
838 "<td class=ac><a name=\"%s/+%s\"></a>%s"
839 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
840 "",
841 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200842 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100843 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
844
Willy Tarreau708c4162019-10-09 10:19:16 +0200845 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100846 chunk_appendf(out, "<div class=tips>");
847
Willy Tarreau90807112020-02-25 08:16:33 +0100848 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100849 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
850 else if (*field_str(stats, ST_F_ADDR) == '[')
851 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
852 else if (*field_str(stats, ST_F_ADDR))
853 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
854
855 /* id */
856 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
857 }
858
859 chunk_appendf(out,
860 /* queue */
861 "%s</td><td colspan=3></td>"
862 /* sessions rate: current, max, limit */
863 "<td colspan=3>&nbsp;</td>"
864 /* sessions: current, max, limit, total, lbtot, lastsess */
865 "<td>%s</td><td>%s</td><td>%s</td>"
866 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
867 /* bytes: in, out */
868 "<td>%s</td><td>%s</td>"
869 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200870 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100871 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
872 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
873
874 chunk_appendf(out,
875 /* denied: req, resp */
876 "<td>%s</td><td>%s</td>"
877 /* errors: request, connect, response */
878 "<td>%s</td><td></td><td></td>"
879 /* warnings: retries, redispatches */
880 "<td></td><td></td>"
881 /* server status: reflect listener status */
882 "<td class=ac>%s</td>"
883 /* rest of server: nothing */
884 "<td class=ac colspan=8></td></tr>"
885 "",
886 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
887 U2H(stats[ST_F_EREQ].u.u64),
888 field_str(stats, ST_F_STATUS));
889 }
890 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
891 const char *style;
892
893 /* determine the style to use depending on the server's state,
894 * its health and weight. There isn't a 1-to-1 mapping between
895 * state and styles for the cases where the server is (still)
896 * up. The reason is that we don't want to report nolb and
897 * drain with the same color.
898 */
899
900 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
901 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
902 style = "down";
903 }
904 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
905 style = "going_up";
906 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400907 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
908 style = "draining";
909 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100910 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
911 style = "going_down";
912 }
913 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
914 style = "nolb";
915 }
916 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
917 style = "no_check";
918 }
919 else if (!stats[ST_F_CHKFAIL].type ||
920 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
921 /* no check or max health = UP */
922 if (stats[ST_F_WEIGHT].u.u32)
923 style = "up";
924 else
925 style = "draining";
926 }
927 else {
928 style = "going_down";
929 }
930
931 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
932 chunk_appendf(out, "<tr class=\"maintain\">");
933 else
934 chunk_appendf(out,
935 "<tr class=\"%s_%s\">",
936 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
937
938
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200939 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100940 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000941 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
942 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100943 field_str(stats, ST_F_SVNAME));
944
945 chunk_appendf(out,
946 "<td class=ac><a name=\"%s/%s\"></a>%s"
947 "<a class=lfsb href=\"#%s/%s\">%s</a>"
948 "",
949 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200950 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100951 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
952
Willy Tarreau708c4162019-10-09 10:19:16 +0200953 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100954 chunk_appendf(out, "<div class=tips>");
955
Willy Tarreau90807112020-02-25 08:16:33 +0100956 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100957 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
958 else if (*field_str(stats, ST_F_ADDR) == '[')
959 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
960 else if (*field_str(stats, ST_F_ADDR))
961 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
962
963 /* id */
964 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
965
966 /* cookie */
967 if (stats[ST_F_COOKIE].type) {
968 chunk_appendf(out, ", cookie: '");
969 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
970 chunk_htmlencode(out, &src);
971 chunk_appendf(out, "'");
972 }
973
974 chunk_appendf(out, "</div>");
975 }
976
977 chunk_appendf(out,
978 /* queue : current, max, limit */
979 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
980 /* sessions rate : current, max, limit */
981 "<td>%s</td><td>%s</td><td></td>"
982 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200983 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100984 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
985 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
986
987 chunk_appendf(out,
988 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200989 "<td><u>%s<div class=tips>"
990 "<table class=det>"
991 "<tr><th>Current active connections:</th><td>%s</td></tr>"
992 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
993 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
994 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
995 "</table></div></u>"
996 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100997 "<td><u>%s<div class=tips><table class=det>"
998 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
999 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001000 U2H(stats[ST_F_SCUR].u.u32),
1001 U2H(stats[ST_F_SCUR].u.u32),
1002 U2H(stats[ST_F_SRV_ICUR].u.u32),
1003 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1004 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1005 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001006 U2H(stats[ST_F_STOT].u.u64),
1007 U2H(stats[ST_F_STOT].u.u64));
1008
1009 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1010 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001011 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001012 "<tr><th>New connections:</th><td>%s</td></tr>"
1013 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001014 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001015 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1016 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1017 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1018 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1019 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1020 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001021 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001022 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001023 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001024 U2H(stats[ST_F_CONNECT].u.u64),
1025 U2H(stats[ST_F_REUSE].u.u64),
1026 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1027 (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 +02001028 U2H(stats[ST_F_REQ_TOT].u.u64),
1029 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1030 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1031 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1032 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1033 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1034 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1035 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1036 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1037 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1038 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1039 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1040 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001041 U2H(stats[ST_F_WREW].u.u64),
1042 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001043 }
1044
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001045 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1046 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1047 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1048 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1049 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001050 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001051 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1052 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1053 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1054 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001055
1056 chunk_appendf(out,
1057 "</table></div></u></td>"
1058 /* sessions: lbtot, last */
1059 "<td>%s</td><td>%s</td>",
1060 U2H(stats[ST_F_LBTOT].u.u64),
1061 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1062
1063 chunk_appendf(out,
1064 /* bytes : in, out */
1065 "<td>%s</td><td>%s</td>"
1066 /* denied: req, resp */
1067 "<td></td><td>%s</td>"
1068 /* errors : request, connect */
1069 "<td></td><td>%s</td>"
1070 /* errors : response */
1071 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1072 /* warnings: retries, redispatches */
1073 "<td>%lld</td><td>%lld</td>"
1074 "",
1075 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1076 U2H(stats[ST_F_DRESP].u.u64),
1077 U2H(stats[ST_F_ECON].u.u64),
1078 U2H(stats[ST_F_ERESP].u.u64),
1079 (long long)stats[ST_F_CLI_ABRT].u.u64,
1080 (long long)stats[ST_F_SRV_ABRT].u.u64,
1081 (long long)stats[ST_F_WRETR].u.u64,
1082 (long long)stats[ST_F_WREDIS].u.u64);
1083
1084 /* status, last change */
1085 chunk_appendf(out, "<td class=ac>");
1086
1087 /* FIXME!!!!
1088 * LASTCHG should contain the last change for *this* server and must be computed
1089 * properly above, as was done below, ie: this server if maint, otherwise ref server
1090 * if tracking. Note that ref is either local or remote depending on tracking.
1091 */
1092
1093
1094 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1095 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1096 }
1097 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1098 chunk_strcat(out, "<i>no check</i>");
1099 }
1100 else {
1101 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1102 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1103 if (stats[ST_F_CHECK_HEALTH].u.u32)
1104 chunk_strcat(out, " &uarr;");
1105 }
1106 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1107 chunk_strcat(out, " &darr;");
1108 }
1109
1110 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1111 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1112 chunk_appendf(out,
1113 "</td><td class=ac><u> %s",
1114 field_str(stats, ST_F_AGENT_STATUS));
1115
1116 if (stats[ST_F_AGENT_CODE].type)
1117 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1118
1119 if (stats[ST_F_AGENT_DURATION].type)
1120 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1121
1122 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1123
1124 if (*field_str(stats, ST_F_LAST_AGT)) {
1125 chunk_appendf(out, ": ");
1126 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1127 chunk_htmlencode(out, &src);
1128 }
1129 chunk_appendf(out, "</div></u>");
1130 }
1131 else if (stats[ST_F_CHECK_STATUS].type) {
1132 chunk_appendf(out,
1133 "</td><td class=ac><u> %s",
1134 field_str(stats, ST_F_CHECK_STATUS));
1135
1136 if (stats[ST_F_CHECK_CODE].type)
1137 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1138
1139 if (stats[ST_F_CHECK_DURATION].type)
1140 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1141
1142 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1143
1144 if (*field_str(stats, ST_F_LAST_CHK)) {
1145 chunk_appendf(out, ": ");
1146 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1147 chunk_htmlencode(out, &src);
1148 }
1149 chunk_appendf(out, "</div></u>");
1150 }
1151 else
1152 chunk_appendf(out, "</td><td>");
1153
1154 chunk_appendf(out,
1155 /* weight */
1156 "</td><td class=ac>%d</td>"
1157 /* act, bck */
1158 "<td class=ac>%s</td><td class=ac>%s</td>"
1159 "",
1160 stats[ST_F_WEIGHT].u.u32,
1161 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1162 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1163
1164 /* check failures: unique, fatal, down time */
1165 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1166 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1167 }
1168 else if (stats[ST_F_CHKFAIL].type) {
1169 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1170
1171 if (stats[ST_F_HANAFAIL].type)
1172 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1173
1174 chunk_appendf(out,
1175 "<div class=tips>Failed Health Checks%s</div></u></td>"
1176 "<td>%lld</td><td>%s</td>"
1177 "",
1178 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1179 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1180 }
1181 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1182 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1183 chunk_appendf(out,
1184 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1185 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1186 }
1187 else
1188 chunk_appendf(out, "<td colspan=3></td>");
1189
1190 /* throttle */
1191 if (stats[ST_F_THROTTLE].type)
1192 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1193 else
1194 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1195 }
1196 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1197 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001198 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001199 /* Column sub-heading for Enable or Disable server */
1200 chunk_appendf(out, "<td></td>");
1201 }
1202 chunk_appendf(out,
1203 "<td class=ac>"
1204 /* name */
1205 "%s<a name=\"%s/Backend\"></a>"
1206 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1207 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001208 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001209 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1210
Willy Tarreau708c4162019-10-09 10:19:16 +02001211 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001212 /* balancing */
1213 chunk_appendf(out, "<div class=tips>balancing: %s",
1214 field_str(stats, ST_F_ALGO));
1215
1216 /* cookie */
1217 if (stats[ST_F_COOKIE].type) {
1218 chunk_appendf(out, ", cookie: '");
1219 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1220 chunk_htmlencode(out, &src);
1221 chunk_appendf(out, "'");
1222 }
1223 chunk_appendf(out, "</div>");
1224 }
1225
1226 chunk_appendf(out,
1227 "%s</td>"
1228 /* queue : current, max */
1229 "<td>%s</td><td>%s</td><td></td>"
1230 /* sessions rate : current, max, limit */
1231 "<td>%s</td><td>%s</td><td></td>"
1232 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001233 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001234 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1235 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1236
1237 chunk_appendf(out,
1238 /* sessions: current, max, limit, total */
1239 "<td>%s</td><td>%s</td><td>%s</td>"
1240 "<td><u>%s<div class=tips><table class=det>"
1241 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1242 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001243 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 +01001244 U2H(stats[ST_F_STOT].u.u64),
1245 U2H(stats[ST_F_STOT].u.u64));
1246
1247 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1248 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1249 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001250 "<tr><th>New connections:</th><td>%s</td></tr>"
1251 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001252 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1253 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1254 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1255 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1256 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1257 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1258 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1259 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001260 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1261 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001262 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001263 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001264 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001265 U2H(stats[ST_F_CONNECT].u.u64),
1266 U2H(stats[ST_F_REUSE].u.u64),
1267 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1268 (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 +01001269 U2H(stats[ST_F_REQ_TOT].u.u64),
1270 U2H(stats[ST_F_HRSP_1XX].u.u64),
1271 U2H(stats[ST_F_HRSP_2XX].u.u64),
1272 U2H(stats[ST_F_COMP_RSP].u.u64),
1273 stats[ST_F_HRSP_2XX].u.u64 ?
1274 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1275 U2H(stats[ST_F_HRSP_3XX].u.u64),
1276 U2H(stats[ST_F_HRSP_4XX].u.u64),
1277 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001278 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001279 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1280 U2H(stats[ST_F_CACHE_HITS].u.u64),
1281 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1282 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001283 U2H(stats[ST_F_WREW].u.u64),
1284 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001285 }
1286
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001287 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1288 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1289 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1290 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1291 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001292 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001293 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1294 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1295 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1296 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001297
1298 chunk_appendf(out,
1299 "</table></div></u></td>"
1300 /* sessions: lbtot, last */
1301 "<td>%s</td><td>%s</td>"
1302 /* bytes: in */
1303 "<td>%s</td>"
1304 "",
1305 U2H(stats[ST_F_LBTOT].u.u64),
1306 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1307 U2H(stats[ST_F_BIN].u.u64));
1308
1309 chunk_appendf(out,
1310 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1311 "<td>%s%s<div class=tips><table class=det>"
1312 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1313 "<tr><th>Compression in:</th><td>%s</td></tr>"
1314 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1315 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1316 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1317 "</table></div>%s</td>",
1318 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1319 U2H(stats[ST_F_BOUT].u.u64),
1320 U2H(stats[ST_F_BOUT].u.u64),
1321 U2H(stats[ST_F_COMP_IN].u.u64),
1322 U2H(stats[ST_F_COMP_OUT].u.u64),
1323 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1324 U2H(stats[ST_F_COMP_BYP].u.u64),
1325 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1326 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,
1327 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1328
1329 chunk_appendf(out,
1330 /* denied: req, resp */
1331 "<td>%s</td><td>%s</td>"
1332 /* errors : request, connect */
1333 "<td></td><td>%s</td>"
1334 /* errors : response */
1335 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1336 /* warnings: retries, redispatches */
1337 "<td>%lld</td><td>%lld</td>"
1338 /* backend status: reflect backend status (up/down): we display UP
1339 * if the backend has known working servers or if it has no server at
1340 * all (eg: for stats). Then we display the total weight, number of
1341 * active and backups. */
1342 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1343 "<td class=ac>%d</td><td class=ac>%d</td>"
1344 "",
1345 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1346 U2H(stats[ST_F_ECON].u.u64),
1347 U2H(stats[ST_F_ERESP].u.u64),
1348 (long long)stats[ST_F_CLI_ABRT].u.u64,
1349 (long long)stats[ST_F_SRV_ABRT].u.u64,
1350 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1351 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1352 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1353 stats[ST_F_WEIGHT].u.u32,
1354 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1355
1356 chunk_appendf(out,
1357 /* rest of backend: nothing, down transitions, total downtime, throttle */
1358 "<td class=ac>&nbsp;</td><td>%d</td>"
1359 "<td>%s</td>"
1360 "<td></td>"
1361 "</tr>",
1362 stats[ST_F_CHKDOWN].u.u32,
1363 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1364 }
1365 return 1;
1366}
1367
Willy Tarreau43241ff2019-10-09 11:27:51 +02001368static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001369{
Simon Horman05ee2132017-01-04 09:37:25 +01001370 int ret;
1371
William Lallemand74c24fb2016-11-21 17:18:36 +01001372 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001373 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001374 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001375 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001376 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001377 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001378 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001379 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001380
1381 if (ret)
1382 appctx->ctx.stats.flags |= STAT_STARTED;
1383
1384 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001385}
1386
1387/* Fill <stats> with the frontend statistics. <stats> is
1388 * preallocated array of length <len>. The length of the array
1389 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1390 * this value, the function returns 0, otherwise, it returns 1.
1391 */
1392int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1393{
1394 if (len < ST_F_TOTAL_FIELDS)
1395 return 0;
1396
1397 memset(stats, 0, sizeof(*stats) * len);
1398
1399 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1400 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1401 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1402 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1403 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1404 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1405 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1406 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1407 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1408 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1409 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1410 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1411 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1412 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1413 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1414 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1415 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1416 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1417 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1418 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1419 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1420 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001421 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001422 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001423
1424 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1425 if (px->mode == PR_MODE_HTTP) {
1426 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1427 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1428 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1429 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1430 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1431 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1432 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001433 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1434 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001435 }
1436
1437 /* requests : req_rate, req_rate_max, req_tot, */
1438 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1439 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1440 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1441
1442 /* compression: in, out, bypassed, responses */
1443 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1444 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1445 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1446 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1447
1448 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1449 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1450 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1451 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1452
1453 return 1;
1454}
1455
1456/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1457 * the state from stream interface <si>. The caller is responsible for clearing
1458 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1459 */
1460static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1461{
1462 struct appctx *appctx = __objt_appctx(si->end);
1463
1464 if (!(px->cap & PR_CAP_FE))
1465 return 0;
1466
1467 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1468 return 0;
1469
1470 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1471 return 0;
1472
Willy Tarreau43241ff2019-10-09 11:27:51 +02001473 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001474}
1475
1476/* Fill <stats> with the listener statistics. <stats> is
1477 * preallocated array of length <len>. The length of the array
1478 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1479 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001480 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001481 */
1482int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1483 struct field *stats, int len)
1484{
Willy Tarreau83061a82018-07-13 11:56:34 +02001485 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001486
1487 if (len < ST_F_TOTAL_FIELDS)
1488 return 0;
1489
1490 if (!l->counters)
1491 return 0;
1492
1493 chunk_reset(out);
1494 memset(stats, 0, sizeof(*stats) * len);
1495
1496 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1497 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1498 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1499 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1500 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1501 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1502 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1503 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1504 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1505 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1506 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1507 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1508 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1509 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001510 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 +01001511 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1512 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1513 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1514 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001515 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001516 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001517
Willy Tarreau708c4162019-10-09 10:19:16 +02001518 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001519 char str[INET6_ADDRSTRLEN];
1520 int port;
1521
1522 port = get_host_port(&l->addr);
1523 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1524 case AF_INET:
1525 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1526 chunk_appendf(out, "%s:%d", str, port);
1527 break;
1528 case AF_INET6:
1529 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1530 chunk_appendf(out, "[%s]:%d", str, port);
1531 break;
1532 case AF_UNIX:
1533 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1534 break;
1535 case -1:
1536 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1537 chunk_strcat(out, strerror(errno));
1538 break;
1539 default: /* address family not supported */
1540 break;
1541 }
1542 }
1543
1544 return 1;
1545}
1546
1547/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001548 * from stream interface <si>. The caller is responsible for clearing the trash
1549 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001550 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001551static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001552{
1553 struct appctx *appctx = __objt_appctx(si->end);
1554
Willy Tarreau43241ff2019-10-09 11:27:51 +02001555 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001556 return 0;
1557
Willy Tarreau43241ff2019-10-09 11:27:51 +02001558 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001559}
1560
1561enum srv_stats_state {
1562 SRV_STATS_STATE_DOWN = 0,
1563 SRV_STATS_STATE_DOWN_AGENT,
1564 SRV_STATS_STATE_GOING_UP,
1565 SRV_STATS_STATE_UP_GOING_DOWN,
1566 SRV_STATS_STATE_UP,
1567 SRV_STATS_STATE_NOLB_GOING_DOWN,
1568 SRV_STATS_STATE_NOLB,
1569 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1570 SRV_STATS_STATE_DRAIN,
1571 SRV_STATS_STATE_DRAIN_AGENT,
1572 SRV_STATS_STATE_NO_CHECK,
1573
1574 SRV_STATS_STATE_COUNT, /* Must be last */
1575};
1576
1577static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1578 [SRV_STATS_STATE_DOWN] = "DOWN",
1579 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1580 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1581 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1582 [SRV_STATS_STATE_UP] = "UP",
1583 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1584 [SRV_STATS_STATE_NOLB] = "NOLB",
1585 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1586 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1587 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1588 [SRV_STATS_STATE_NO_CHECK] = "no check"
1589};
1590
1591/* Fill <stats> with the server statistics. <stats> is
1592 * preallocated array of length <len>. The length of the array
1593 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1594 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001595 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001596 */
1597int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1598 struct field *stats, int len)
1599{
1600 struct server *via, *ref;
1601 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001602 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001603 enum srv_stats_state state;
1604 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001605 long long srv_samples_counter;
1606 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001607
1608 if (len < ST_F_TOTAL_FIELDS)
1609 return 0;
1610
1611 memset(stats, 0, sizeof(*stats) * len);
1612
1613 /* we have "via" which is the tracked server as described in the configuration,
1614 * and "ref" which is the checked server and the end of the chain.
1615 */
1616 via = sv->track ? sv->track : sv;
1617 ref = via;
1618 while (ref->track)
1619 ref = ref->track;
1620
Emeric Brun52a91d32017-08-31 14:41:55 +02001621 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001622 if ((ref->check.state & CHK_ST_ENABLED) &&
1623 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1624 state = SRV_STATS_STATE_UP_GOING_DOWN;
1625 } else {
1626 state = SRV_STATS_STATE_UP;
1627 }
1628
Emeric Brun52a91d32017-08-31 14:41:55 +02001629 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001630 if (ref->agent.state & CHK_ST_ENABLED)
1631 state = SRV_STATS_STATE_DRAIN_AGENT;
1632 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1633 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1634 else
1635 state = SRV_STATS_STATE_DRAIN;
1636 }
1637
1638 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1639 state = SRV_STATS_STATE_NO_CHECK;
1640 }
1641 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001642 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001643 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1644 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1645 state = SRV_STATS_STATE_NOLB;
1646 } else {
1647 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1648 }
1649 }
1650 else { /* stopped */
1651 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1652 state = SRV_STATS_STATE_DOWN_AGENT;
1653 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1654 state = SRV_STATS_STATE_DOWN; /* DOWN */
1655 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1656 state = SRV_STATS_STATE_GOING_UP;
1657 } else {
1658 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1659 }
1660 }
1661
1662 chunk_reset(out);
1663
1664 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1665 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1666 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1667 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1668 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1669 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1670 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1671
1672 if (sv->maxconn)
1673 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1674
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001675 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1676 if (sv->max_idle_conns != -1)
1677 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1678
William Lallemand74c24fb2016-11-21 17:18:36 +01001679 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1680 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1681 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001682 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001683 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1684 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1685 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1686 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001687 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001688 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001689 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1690 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001691
1692 /* status */
1693 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001694 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001695 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001696 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001697 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001698 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001699 chunk_appendf(out, "MAINT");
1700 else
1701 chunk_appendf(out,
1702 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001703 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1704 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001705
1706 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1707 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001708 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 +01001709 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1710 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1711
1712 /* check failures: unique, fatal; last change, total downtime */
1713 if (sv->check.state & CHK_ST_ENABLED) {
1714 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1715 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1716 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1717 }
1718
1719 if (sv->maxqueue)
1720 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1721
1722 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1723 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1724 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1725
Emeric Brun52a91d32017-08-31 14:41:55 +02001726 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001727 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1728
1729 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1730
1731 if (sv->track) {
1732 char *fld_track = chunk_newstr(out);
1733
1734 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1735 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1736 }
1737
1738 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1739 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1740 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1741
1742 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1743 const char *fld_chksts;
1744
1745 fld_chksts = chunk_newstr(out);
1746 chunk_strcat(out, "* "); // for check in progress
1747 chunk_strcat(out, get_check_status_info(sv->check.status));
1748 if (!(sv->check.state & CHK_ST_INPROGRESS))
1749 fld_chksts += 2; // skip "* "
1750 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1751
1752 if (sv->check.status >= HCHK_STATUS_L57DATA)
1753 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1754
1755 if (sv->check.status >= HCHK_STATUS_CHECKED)
1756 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1757
1758 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1759 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1760 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1761 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1762 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1763 }
1764
1765 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1766 const char *fld_chksts;
1767
1768 fld_chksts = chunk_newstr(out);
1769 chunk_strcat(out, "* "); // for check in progress
1770 chunk_strcat(out, get_check_status_info(sv->agent.status));
1771 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1772 fld_chksts += 2; // skip "* "
1773 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1774
1775 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1776 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1777
1778 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1779 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1780
1781 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1782 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1783 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1784 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1785 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1786 }
1787
1788 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1789 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001790 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001791 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1792 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1793 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1794 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1795 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1796 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1797 }
1798
1799 if (ref->observe)
1800 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1801
1802 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1803 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1804 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1805
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001806 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1807 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1808 srv_samples_window = srv_samples_counter;
1809
1810 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1811 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1812 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1813 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001814
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001815 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1816 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1817 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1818 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1819
Willy Tarreau708c4162019-10-09 10:19:16 +02001820 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001821 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1822 case AF_INET:
1823 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001824 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001825 break;
1826 case AF_INET6:
1827 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001828 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001829 break;
1830 case AF_UNIX:
1831 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1832 break;
1833 case -1:
1834 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1835 chunk_strcat(out, strerror(errno));
1836 break;
1837 default: /* address family not supported */
1838 break;
1839 }
1840
1841 if (sv->cookie)
1842 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1843 }
1844
1845 return 1;
1846}
1847
1848/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001849 * from stream interface <si>, and server state <state>. The caller is
1850 * responsible for clearing the trash if needed. Returns non-zero if it emits
1851 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001852 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001853static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001854{
1855 struct appctx *appctx = __objt_appctx(si->end);
1856
Willy Tarreau43241ff2019-10-09 11:27:51 +02001857 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001858 return 0;
1859
Willy Tarreau43241ff2019-10-09 11:27:51 +02001860 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001861}
1862
1863/* Fill <stats> with the backend statistics. <stats> is
1864 * preallocated array of length <len>. The length of the array
1865 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1866 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001867 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001868 */
1869int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1870{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001871 long long be_samples_counter;
1872 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1873
William Lallemand74c24fb2016-11-21 17:18:36 +01001874 if (len < ST_F_TOTAL_FIELDS)
1875 return 0;
1876
1877 memset(stats, 0, sizeof(*stats) * len);
1878
1879 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1880 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1881 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1882 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1883 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001884 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001885 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1886 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1887 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1888 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1889 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1890 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1891 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1892 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1893 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1894 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1895 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001896 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001897 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001898 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1899 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001900 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1901 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1902 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1903 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1904 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1905 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1906 if (px->srv)
1907 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1908
1909 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1910 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1911 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1912 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1913 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1914 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1915 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1916
Willy Tarreau708c4162019-10-09 10:19:16 +02001917 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001918 if (px->cookie_name)
1919 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1920 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1921 }
1922
1923 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1924 if (px->mode == PR_MODE_HTTP) {
1925 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1926 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1927 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1928 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1929 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1930 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1931 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001932 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1933 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001934 }
1935
1936 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1937 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1938
1939 /* compression: in, out, bypassed, responses */
1940 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1941 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1942 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1943 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1944 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1945
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001946 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1947 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1948 be_samples_window = be_samples_counter;
1949
1950 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1951 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1952 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1953 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 +01001954
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001955 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1956 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1957 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1958 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1959
William Lallemand74c24fb2016-11-21 17:18:36 +01001960 return 1;
1961}
1962
1963/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001964 * interface <si>. The caller is responsible for clearing the trash if needed.
1965 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001966 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001967static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001968{
1969 struct appctx *appctx = __objt_appctx(si->end);
1970
1971 if (!(px->cap & PR_CAP_BE))
1972 return 0;
1973
1974 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1975 return 0;
1976
Willy Tarreau43241ff2019-10-09 11:27:51 +02001977 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001978 return 0;
1979
Willy Tarreau43241ff2019-10-09 11:27:51 +02001980 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001981}
1982
1983/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1984 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1985 * for clearing the trash if needed.
1986 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001987static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001988{
1989 struct appctx *appctx = __objt_appctx(si->end);
1990 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1991
1992 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1993 /* A form to enable/disable this proxy servers */
1994
1995 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1996 scope_txt[0] = 0;
1997 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001998 const char *scope_ptr = stats_scope_ptr(appctx, si);
1999
William Lallemand74c24fb2016-11-21 17:18:36 +01002000 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002001 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002002 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2003 }
2004
2005 chunk_appendf(&trash,
2006 "<form method=\"post\">");
2007 }
2008
2009 /* print a new table */
2010 chunk_appendf(&trash,
2011 "<table class=\"tbl\" width=\"100%%\">\n"
2012 "<tr class=\"titre\">"
2013 "<th class=\"pxname\" width=\"10%%\">");
2014
2015 chunk_appendf(&trash,
2016 "<a name=\"%s\"></a>%s"
2017 "<a class=px href=\"#%s\">%s</a>",
2018 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002019 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002020 px->id, px->id);
2021
Willy Tarreau676c29e2019-10-09 10:50:01 +02002022 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002023 /* cap, mode, id */
2024 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2025 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2026 px->uuid);
2027 chunk_appendf(&trash, "</div>");
2028 }
2029
2030 chunk_appendf(&trash,
2031 "%s</th>"
2032 "<th class=\"%s\" width=\"90%%\">%s</th>"
2033 "</tr>\n"
2034 "</table>\n"
2035 "<table class=\"tbl\" width=\"100%%\">\n"
2036 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002037 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002038 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2039
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002040 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002041 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002042 if ((px->cap & PR_CAP_BE) && px->srv)
2043 chunk_appendf(&trash,
2044 "<th rowspan=2 width=1><input type=\"checkbox\" "
2045 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2046 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2047 px->id,
2048 px->id);
2049 else
2050 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002051 }
2052
2053 chunk_appendf(&trash,
2054 "<th rowspan=2></th>"
2055 "<th colspan=3>Queue</th>"
2056 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2057 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2058 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2059 "<th colspan=9>Server</th>"
2060 "</tr>\n"
2061 "<tr class=\"titre\">"
2062 "<th>Cur</th><th>Max</th><th>Limit</th>"
2063 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2064 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2065 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2066 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2067 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2068 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2069 "<th>Thrtle</th>\n"
2070 "</tr>");
2071}
2072
2073/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2074 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2075 */
2076static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2077{
2078 struct appctx *appctx = __objt_appctx(si->end);
2079 chunk_appendf(&trash, "</table>");
2080
2081 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2082 /* close the form used to enable/disable this proxy servers */
2083 chunk_appendf(&trash,
2084 "Choose the action to perform on the checked servers : "
2085 "<select name=action>"
2086 "<option value=\"\"></option>"
2087 "<option value=\"ready\">Set state to READY</option>"
2088 "<option value=\"drain\">Set state to DRAIN</option>"
2089 "<option value=\"maint\">Set state to MAINT</option>"
2090 "<option value=\"dhlth\">Health: disable checks</option>"
2091 "<option value=\"ehlth\">Health: enable checks</option>"
2092 "<option value=\"hrunn\">Health: force UP</option>"
2093 "<option value=\"hnolb\">Health: force NOLB</option>"
2094 "<option value=\"hdown\">Health: force DOWN</option>"
2095 "<option value=\"dagent\">Agent: disable checks</option>"
2096 "<option value=\"eagent\">Agent: enable checks</option>"
2097 "<option value=\"arunn\">Agent: force UP</option>"
2098 "<option value=\"adown\">Agent: force DOWN</option>"
2099 "<option value=\"shutdown\">Kill Sessions</option>"
2100 "</select>"
2101 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2102 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2103 "</form>",
2104 px->uuid);
2105 }
2106
2107 chunk_appendf(&trash, "<p>\n");
2108}
2109
2110/*
2111 * Dumps statistics for a proxy. The output is sent to the stream interface's
2112 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2113 * buffer space, or non-zero if everything completed. This function is used
2114 * both by the CLI and the HTTP entry points, and is able to dump the output
2115 * in HTML or CSV formats. If the later, <uri> must be NULL.
2116 */
Christopher Fauletef779222018-10-31 08:47:01 +01002117int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2118 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002119{
2120 struct appctx *appctx = __objt_appctx(si->end);
2121 struct stream *s = si_strm(si);
2122 struct channel *rep = si_ic(si);
2123 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2124 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002125
2126 chunk_reset(&trash);
2127
2128 switch (appctx->ctx.stats.px_st) {
2129 case STAT_PX_ST_INIT:
2130 /* we are on a new proxy */
2131 if (uri && uri->scope) {
2132 /* we have a limited scope, we have to check the proxy name */
2133 struct stat_scope *scope;
2134 int len;
2135
2136 len = strlen(px->id);
2137 scope = uri->scope;
2138
2139 while (scope) {
2140 /* match exact proxy name */
2141 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2142 break;
2143
2144 /* match '.' which means 'self' proxy */
2145 if (!strcmp(scope->px_id, ".") && px == s->be)
2146 break;
2147 scope = scope->next;
2148 }
2149
2150 /* proxy name not found : don't dump anything */
2151 if (scope == NULL)
2152 return 1;
2153 }
2154
2155 /* if the user has requested a limited output and the proxy
2156 * name does not match, skip it.
2157 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002158 if (appctx->ctx.stats.scope_len) {
2159 const char *scope_ptr = stats_scope_ptr(appctx, si);
2160
2161 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2162 return 1;
2163 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002164
2165 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2166 (appctx->ctx.stats.iid != -1) &&
2167 (px->uuid != appctx->ctx.stats.iid))
2168 return 1;
2169
2170 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2171 /* fall through */
2172
2173 case STAT_PX_ST_TH:
2174 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002175 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002176 if (!stats_putchk(rep, htx, &trash))
2177 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002178 }
2179
2180 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2181 /* fall through */
2182
2183 case STAT_PX_ST_FE:
2184 /* print the frontend */
2185 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002186 if (!stats_putchk(rep, htx, &trash))
2187 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002188 }
2189
2190 appctx->ctx.stats.l = px->conf.listeners.n;
2191 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2192 /* fall through */
2193
2194 case STAT_PX_ST_LI:
2195 /* stats.l has been initialized above */
2196 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002197 if (htx) {
2198 if (htx_almost_full(htx))
2199 goto full;
2200 }
2201 else {
2202 if (buffer_almost_full(&rep->buf))
2203 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002204 }
2205
2206 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2207 if (!l->counters)
2208 continue;
2209
2210 if (appctx->ctx.stats.flags & STAT_BOUND) {
2211 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2212 break;
2213
2214 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2215 continue;
2216 }
2217
2218 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002219 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002220 if (!stats_putchk(rep, htx, &trash))
2221 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002222 }
2223 }
2224
2225 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2226 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2227 /* fall through */
2228
2229 case STAT_PX_ST_SV:
2230 /* stats.sv has been initialized above */
2231 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002232 if (htx) {
2233 if (htx_almost_full(htx))
2234 goto full;
2235 }
2236 else {
2237 if (buffer_almost_full(&rep->buf))
2238 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002239 }
2240
2241 sv = appctx->ctx.stats.sv;
2242
2243 if (appctx->ctx.stats.flags & STAT_BOUND) {
2244 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2245 break;
2246
2247 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2248 continue;
2249 }
2250
2251 svs = sv;
2252 while (svs->track)
2253 svs = svs->track;
2254
2255 /* do not report servers which are DOWN and not changing state */
2256 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002257 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2258 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002259 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2260 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2261 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2262 continue;
2263 }
2264
Willy Tarreau43241ff2019-10-09 11:27:51 +02002265 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002266 if (!stats_putchk(rep, htx, &trash))
2267 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002268 }
2269 } /* for sv */
2270
2271 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2272 /* fall through */
2273
2274 case STAT_PX_ST_BE:
2275 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002276 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002277 if (!stats_putchk(rep, htx, &trash))
2278 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002279 }
2280
2281 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2282 /* fall through */
2283
2284 case STAT_PX_ST_END:
2285 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2286 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002287 if (!stats_putchk(rep, htx, &trash))
2288 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002289 }
2290
2291 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2292 /* fall through */
2293
2294 case STAT_PX_ST_FIN:
2295 return 1;
2296
2297 default:
2298 /* unknown state, we should put an abort() here ! */
2299 return 1;
2300 }
Christopher Fauletef779222018-10-31 08:47:01 +01002301
2302 full:
2303 si_rx_room_blk(si);
2304 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002305}
2306
2307/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2308 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2309 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002310static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002311{
2312 /* WARNING! This must fit in the first buffer !!! */
2313 chunk_appendf(&trash,
2314 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2315 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2316 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2317 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2318 "<style type=\"text/css\"><!--\n"
2319 "body {"
2320 " font-family: arial, helvetica, sans-serif;"
2321 " font-size: 12px;"
2322 " font-weight: normal;"
2323 " color: black;"
2324 " background: white;"
2325 "}\n"
2326 "th,td {"
2327 " font-size: 10px;"
2328 "}\n"
2329 "h1 {"
2330 " font-size: x-large;"
2331 " margin-bottom: 0.5em;"
2332 "}\n"
2333 "h2 {"
2334 " font-family: helvetica, arial;"
2335 " font-size: x-large;"
2336 " font-weight: bold;"
2337 " font-style: italic;"
2338 " color: #6020a0;"
2339 " margin-top: 0em;"
2340 " margin-bottom: 0em;"
2341 "}\n"
2342 "h3 {"
2343 " font-family: helvetica, arial;"
2344 " font-size: 16px;"
2345 " font-weight: bold;"
2346 " color: #b00040;"
2347 " background: #e8e8d0;"
2348 " margin-top: 0em;"
2349 " margin-bottom: 0em;"
2350 "}\n"
2351 "li {"
2352 " margin-top: 0.25em;"
2353 " margin-right: 2em;"
2354 "}\n"
2355 ".hr {margin-top: 0.25em;"
2356 " border-color: black;"
2357 " border-bottom-style: solid;"
2358 "}\n"
2359 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2360 ".total {background: #20D0D0;color: #ffff80;}\n"
2361 ".frontend {background: #e8e8d0;}\n"
2362 ".socket {background: #d0d0d0;}\n"
2363 ".backend {background: #e8e8d0;}\n"
2364 ".active_down {background: #ff9090;}\n"
2365 ".active_going_up {background: #ffd020;}\n"
2366 ".active_going_down {background: #ffffa0;}\n"
2367 ".active_up {background: #c0ffc0;}\n"
2368 ".active_nolb {background: #20a0ff;}\n"
2369 ".active_draining {background: #20a0FF;}\n"
2370 ".active_no_check {background: #e0e0e0;}\n"
2371 ".backup_down {background: #ff9090;}\n"
2372 ".backup_going_up {background: #ff80ff;}\n"
2373 ".backup_going_down {background: #c060ff;}\n"
2374 ".backup_up {background: #b0d0ff;}\n"
2375 ".backup_nolb {background: #90b0e0;}\n"
2376 ".backup_draining {background: #cc9900;}\n"
2377 ".backup_no_check {background: #e0e0e0;}\n"
2378 ".maintain {background: #c07820;}\n"
2379 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2380 "\n"
2381 "a.px:link {color: #ffff40; text-decoration: none;}"
2382 "a.px:visited {color: #ffff40; text-decoration: none;}"
2383 "a.px:hover {color: #ffffff; text-decoration: none;}"
2384 "a.lfsb:link {color: #000000; text-decoration: none;}"
2385 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2386 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2387 "\n"
2388 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2389 "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"
2390 "table.tbl td.ac { text-align: center;}\n"
2391 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2392 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2393 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2394 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2395 "\n"
2396 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2397 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2398 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2399 "table.det { border-collapse: collapse; border-style: none; }\n"
2400 "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"
2401 "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"
2402 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2403 "div.tips {\n"
2404 " display:block;\n"
2405 " visibility:hidden;\n"
2406 " z-index:2147483647;\n"
2407 " position:absolute;\n"
2408 " padding:2px 4px 3px;\n"
2409 " background:#f0f060; color:#000000;\n"
2410 " border:1px solid #7040c0;\n"
2411 " white-space:nowrap;\n"
2412 " font-style:normal;font-size:11px;font-weight:normal;\n"
2413 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2414 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2415 "}\n"
2416 "u:hover div.tips {visibility:visible;}\n"
2417 "-->\n"
2418 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002419 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002420 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002421 );
2422}
2423
2424/* Dumps the HTML stats information block to the trash for and uses the state from
2425 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2426 * for clearing the trash if needed.
2427 */
2428static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2429{
2430 struct appctx *appctx = __objt_appctx(si->end);
2431 unsigned int up = (now.tv_sec - start_date.tv_sec);
2432 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002433 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002434 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2435
2436 /* Turn the bytes per second to bits per second and take care of the
2437 * usual ethernet overhead in order to help figure how far we are from
2438 * interface saturation since it's the only case which usually matters.
2439 * For this we count the total size of an Ethernet frame on the wire
2440 * including preamble and IFG (1538) for the largest TCP segment it
2441 * transports (1448 with TCP timestamps). This is not valid for smaller
2442 * packets (under-estimated), but it gives a reasonably accurate
2443 * estimation of how far we are from uplink saturation.
2444 */
2445 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002446
2447 /* WARNING! this has to fit the first packet too.
2448 * We are around 3.5 kB, add adding entries will
2449 * become tricky if we want to support 4kB buffers !
2450 */
2451 chunk_appendf(&trash,
2452 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2453 PRODUCT_NAME "%s</a></h1>\n"
2454 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2455 "<hr width=\"100%%\" class=\"hr\">\n"
2456 "<h3>&gt; General process information</h3>\n"
2457 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002458 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002459 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2460 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2461 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002462 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002463 "Running tasks: %d/%d; idle = %d %%<br>\n"
2464 "</td><td align=\"center\" nowrap>\n"
2465 "<table class=\"lgd\"><tr>\n"
2466 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2467 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2468 "</tr><tr>\n"
2469 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2470 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2471 "</tr><tr>\n"
2472 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2473 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2474 "</tr><tr>\n"
2475 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2476 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2477 "</tr><tr>\n"
2478 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2479 "</tr><tr>\n"
2480 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2481 "</tr></table>\n"
2482 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2483 "</td>"
2484 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2485 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2486 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002487 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2488 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2489 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2490 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2491 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002492 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002493 up / 86400, (up % 86400) / 3600,
2494 (up % 3600) / 60, (up % 60),
2495 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2496 global.rlimit_memmax ? " MB" : "",
2497 global.rlimit_nofile,
2498 global.maxsock, global.maxconn, global.maxpipes,
2499 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002500 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2501 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002502 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002503 );
2504
2505 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002506 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002507 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2508
2509 chunk_appendf(&trash,
2510 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2511 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2512 STAT_SCOPE_TXT_MAXLEN);
2513
2514 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2515 scope_txt[0] = 0;
2516 if (appctx->ctx.stats.scope_len) {
2517 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002518 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002519 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2520 }
2521
2522 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2523 chunk_appendf(&trash,
2524 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2525 uri->uri_prefix,
2526 "",
2527 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2528 scope_txt);
2529 else
2530 chunk_appendf(&trash,
2531 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2532 uri->uri_prefix,
2533 ";up",
2534 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2535 scope_txt);
2536
2537 if (uri->refresh > 0) {
2538 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2539 chunk_appendf(&trash,
2540 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2541 uri->uri_prefix,
2542 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2543 "",
2544 scope_txt);
2545 else
2546 chunk_appendf(&trash,
2547 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2548 uri->uri_prefix,
2549 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2550 ";norefresh",
2551 scope_txt);
2552 }
2553
2554 chunk_appendf(&trash,
2555 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2556 uri->uri_prefix,
2557 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2558 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2559 scope_txt);
2560
2561 chunk_appendf(&trash,
2562 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2563 uri->uri_prefix,
2564 (uri->refresh > 0) ? ";norefresh" : "",
2565 scope_txt);
2566
2567 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002568 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2569 uri->uri_prefix,
2570 (uri->refresh > 0) ? ";norefresh" : "",
2571 scope_txt, uri->uri_prefix);
2572
2573 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002574 "</ul></td>"
2575 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2576 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2577 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2578 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2579 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2580 "</ul>"
2581 "</td>"
2582 "</tr></table>\n"
2583 ""
2584 );
2585
2586 if (appctx->ctx.stats.st_code) {
2587 switch (appctx->ctx.stats.st_code) {
2588 case STAT_STATUS_DONE:
2589 chunk_appendf(&trash,
2590 "<p><div class=active_up>"
2591 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2592 "Action processed successfully."
2593 "</div>\n", uri->uri_prefix,
2594 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2595 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2596 scope_txt);
2597 break;
2598 case STAT_STATUS_NONE:
2599 chunk_appendf(&trash,
2600 "<p><div class=active_going_down>"
2601 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2602 "Nothing has changed."
2603 "</div>\n", uri->uri_prefix,
2604 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2605 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2606 scope_txt);
2607 break;
2608 case STAT_STATUS_PART:
2609 chunk_appendf(&trash,
2610 "<p><div class=active_going_down>"
2611 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2612 "Action partially processed.<br>"
2613 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2614 "</div>\n", uri->uri_prefix,
2615 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2616 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2617 scope_txt);
2618 break;
2619 case STAT_STATUS_ERRP:
2620 chunk_appendf(&trash,
2621 "<p><div class=active_down>"
2622 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2623 "Action not processed because of invalid parameters."
2624 "<ul>"
2625 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002626 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002627 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2628 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2629 "</ul>"
2630 "</div>\n", uri->uri_prefix,
2631 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2632 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2633 scope_txt);
2634 break;
2635 case STAT_STATUS_EXCD:
2636 chunk_appendf(&trash,
2637 "<p><div class=active_down>"
2638 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2639 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2640 "You should retry with less servers at a time.</b>"
2641 "</div>\n", uri->uri_prefix,
2642 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2643 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2644 scope_txt);
2645 break;
2646 case STAT_STATUS_DENY:
2647 chunk_appendf(&trash,
2648 "<p><div class=active_down>"
2649 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2650 "<b>Action denied.</b>"
2651 "</div>\n", uri->uri_prefix,
2652 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2653 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2654 scope_txt);
2655 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002656 case STAT_STATUS_IVAL:
2657 chunk_appendf(&trash,
2658 "<p><div class=active_down>"
2659 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2660 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2661 "</div>\n", uri->uri_prefix,
2662 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2663 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2664 scope_txt);
2665 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002666 default:
2667 chunk_appendf(&trash,
2668 "<p><div class=active_no_check>"
2669 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2670 "Unexpected result."
2671 "</div>\n", uri->uri_prefix,
2672 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2673 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2674 scope_txt);
2675 }
2676 chunk_appendf(&trash, "<p>\n");
2677 }
2678}
2679
2680/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2681 * for clearing the trash if needed.
2682 */
2683static void stats_dump_html_end()
2684{
2685 chunk_appendf(&trash, "</body></html>\n");
2686}
2687
Simon Horman05ee2132017-01-04 09:37:25 +01002688/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2689 * for clearing it if needed.
2690 */
2691static void stats_dump_json_header()
2692{
2693 chunk_strcat(&trash, "[");
2694}
2695
2696
2697/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2698 * for clearing the trash if needed.
2699 */
2700static void stats_dump_json_end()
2701{
2702 chunk_strcat(&trash, "]");
2703}
2704
William Lallemand74c24fb2016-11-21 17:18:36 +01002705/* This function dumps statistics onto the stream interface's read buffer in
2706 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2707 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2708 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2709 * and the stream must be closed, or -1 in case of any error. This function is
2710 * used by both the CLI and the HTTP handlers.
2711 */
Christopher Fauletef779222018-10-31 08:47:01 +01002712static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2713 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002714{
2715 struct appctx *appctx = __objt_appctx(si->end);
2716 struct channel *rep = si_ic(si);
2717 struct proxy *px;
2718
2719 chunk_reset(&trash);
2720
2721 switch (appctx->st2) {
2722 case STAT_ST_INIT:
2723 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2724 /* fall through */
2725
2726 case STAT_ST_HEAD:
2727 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002728 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002729 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2730 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002731 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002732 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002733 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2734 stats_dump_csv_header();
2735
Christopher Fauletef779222018-10-31 08:47:01 +01002736 if (!stats_putchk(rep, htx, &trash))
2737 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002738
Christopher Faulet6338a082019-09-09 15:50:54 +02002739 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2740 appctx->st2 = STAT_ST_FIN;
2741 return 1;
2742 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002743 appctx->st2 = STAT_ST_INFO;
2744 /* fall through */
2745
2746 case STAT_ST_INFO:
2747 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2748 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002749 if (!stats_putchk(rep, htx, &trash))
2750 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002751 }
2752
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002753 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002754 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2755 appctx->st2 = STAT_ST_LIST;
2756 /* fall through */
2757
2758 case STAT_ST_LIST:
2759 /* dump proxies */
2760 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002761 if (htx) {
2762 if (htx_almost_full(htx))
2763 goto full;
2764 }
2765 else {
2766 if (buffer_almost_full(&rep->buf))
2767 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002768 }
2769
2770 px = appctx->ctx.stats.px;
2771 /* skip the disabled proxies, global frontend and non-networked ones */
2772 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002773 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002774 return 0;
2775
2776 appctx->ctx.stats.px = px->next;
2777 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2778 }
2779 /* here, we just have reached the last proxy */
2780
2781 appctx->st2 = STAT_ST_END;
2782 /* fall through */
2783
2784 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002785 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2786 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2787 stats_dump_html_end();
2788 else
2789 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002790 if (!stats_putchk(rep, htx, &trash))
2791 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002792 }
2793
2794 appctx->st2 = STAT_ST_FIN;
2795 /* fall through */
2796
2797 case STAT_ST_FIN:
2798 return 1;
2799
2800 default:
2801 /* unknown state ! */
2802 appctx->st2 = STAT_ST_FIN;
2803 return -1;
2804 }
Christopher Fauletef779222018-10-31 08:47:01 +01002805
2806 full:
2807 si_rx_room_blk(si);
2808 return 0;
2809
William Lallemand74c24fb2016-11-21 17:18:36 +01002810}
2811
2812/* We reached the stats page through a POST request. The appctx is
2813 * expected to have already been allocated by the caller.
2814 * Parse the posted data and enable/disable servers if necessary.
2815 * Returns 1 if request was parsed or zero if it needs more data.
2816 */
2817static int stats_process_http_post(struct stream_interface *si)
2818{
2819 struct stream *s = si_strm(si);
2820 struct appctx *appctx = objt_appctx(si->end);
2821
2822 struct proxy *px = NULL;
2823 struct server *sv = NULL;
2824
2825 char key[LINESIZE];
2826 int action = ST_ADM_ACTION_NONE;
2827 int reprocess = 0;
2828
2829 int total_servers = 0;
2830 int altered_servers = 0;
2831
2832 char *first_param, *cur_param, *next_param, *end_params;
2833 char *st_cur_param = NULL;
2834 char *st_next_param = NULL;
2835
Christopher Fauleta3618372018-12-13 21:59:56 +01002836 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002837
Christopher Fauletb7f88902019-07-15 21:56:43 +02002838 struct htx *htx = htxbuf(&s->req.buf);
2839 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002840
Christopher Fauletb7f88902019-07-15 21:56:43 +02002841 /* we need more data */
2842 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2843 /* check if we can receive more */
2844 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2845 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2846 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002847 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002848 goto wait;
2849 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002850
Christopher Fauletb7f88902019-07-15 21:56:43 +02002851 /* The request was fully received. Copy data */
2852 blk = htx_get_head_blk(htx);
2853 while (blk) {
2854 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002855
Christopher Fauletb7f88902019-07-15 21:56:43 +02002856 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2857 break;
2858 if (type == HTX_BLK_DATA) {
2859 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002860
Christopher Fauletb7f88902019-07-15 21:56:43 +02002861 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002862 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2863 goto out;
2864 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002865 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002866 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002867 }
2868
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002869 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002870 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002871 cur_param = next_param = end_params;
2872 *end_params = '\0';
2873
2874 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2875
2876 /*
2877 * Parse the parameters in reverse order to only store the last value.
2878 * From the html form, the backend and the action are at the end.
2879 */
2880 while (cur_param > first_param) {
2881 char *value;
2882 int poffset, plen;
2883
2884 cur_param--;
2885
2886 if ((*cur_param == '&') || (cur_param == first_param)) {
2887 reprocess_servers:
2888 /* Parse the key */
2889 poffset = (cur_param != first_param ? 1 : 0);
2890 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2891 if ((plen > 0) && (plen <= sizeof(key))) {
2892 strncpy(key, cur_param + poffset, plen);
2893 key[plen - 1] = '\0';
2894 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002895 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002896 goto out;
2897 }
2898
2899 /* Parse the value */
2900 value = key;
2901 while (*value != '\0' && *value != '=') {
2902 value++;
2903 }
2904 if (*value == '=') {
2905 /* Ok, a value is found, we can mark the end of the key */
2906 *value++ = '\0';
2907 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002908 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002909 break;
2910
2911 /* Now we can check the key to see what to do */
2912 if (!px && (strcmp(key, "b") == 0)) {
2913 if ((px = proxy_be_by_name(value)) == NULL) {
2914 /* the backend name is unknown or ambiguous (duplicate names) */
2915 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2916 goto out;
2917 }
2918 }
2919 else if (!action && (strcmp(key, "action") == 0)) {
2920 if (strcmp(value, "ready") == 0) {
2921 action = ST_ADM_ACTION_READY;
2922 }
2923 else if (strcmp(value, "drain") == 0) {
2924 action = ST_ADM_ACTION_DRAIN;
2925 }
2926 else if (strcmp(value, "maint") == 0) {
2927 action = ST_ADM_ACTION_MAINT;
2928 }
2929 else if (strcmp(value, "shutdown") == 0) {
2930 action = ST_ADM_ACTION_SHUTDOWN;
2931 }
2932 else if (strcmp(value, "dhlth") == 0) {
2933 action = ST_ADM_ACTION_DHLTH;
2934 }
2935 else if (strcmp(value, "ehlth") == 0) {
2936 action = ST_ADM_ACTION_EHLTH;
2937 }
2938 else if (strcmp(value, "hrunn") == 0) {
2939 action = ST_ADM_ACTION_HRUNN;
2940 }
2941 else if (strcmp(value, "hnolb") == 0) {
2942 action = ST_ADM_ACTION_HNOLB;
2943 }
2944 else if (strcmp(value, "hdown") == 0) {
2945 action = ST_ADM_ACTION_HDOWN;
2946 }
2947 else if (strcmp(value, "dagent") == 0) {
2948 action = ST_ADM_ACTION_DAGENT;
2949 }
2950 else if (strcmp(value, "eagent") == 0) {
2951 action = ST_ADM_ACTION_EAGENT;
2952 }
2953 else if (strcmp(value, "arunn") == 0) {
2954 action = ST_ADM_ACTION_ARUNN;
2955 }
2956 else if (strcmp(value, "adown") == 0) {
2957 action = ST_ADM_ACTION_ADOWN;
2958 }
2959 /* else these are the old supported methods */
2960 else if (strcmp(value, "disable") == 0) {
2961 action = ST_ADM_ACTION_DISABLE;
2962 }
2963 else if (strcmp(value, "enable") == 0) {
2964 action = ST_ADM_ACTION_ENABLE;
2965 }
2966 else if (strcmp(value, "stop") == 0) {
2967 action = ST_ADM_ACTION_STOP;
2968 }
2969 else if (strcmp(value, "start") == 0) {
2970 action = ST_ADM_ACTION_START;
2971 }
2972 else {
2973 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2974 goto out;
2975 }
2976 }
2977 else if (strcmp(key, "s") == 0) {
2978 if (!(px && action)) {
2979 /*
2980 * Indicates that we'll need to reprocess the parameters
2981 * as soon as backend and action are known
2982 */
2983 if (!reprocess) {
2984 st_cur_param = cur_param;
2985 st_next_param = next_param;
2986 }
2987 reprocess = 1;
2988 }
2989 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002990 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002991 switch (action) {
2992 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002993 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002994 altered_servers++;
2995 total_servers++;
2996 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2997 }
2998 break;
2999 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003000 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003001 altered_servers++;
3002 total_servers++;
3003 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3004 }
3005 break;
3006 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003007 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003008 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3009 altered_servers++;
3010 total_servers++;
3011 }
3012 break;
3013 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003014 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003015 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3016 altered_servers++;
3017 total_servers++;
3018 }
3019 break;
3020 case ST_ADM_ACTION_DHLTH:
3021 if (sv->check.state & CHK_ST_CONFIGURED) {
3022 sv->check.state &= ~CHK_ST_ENABLED;
3023 altered_servers++;
3024 total_servers++;
3025 }
3026 break;
3027 case ST_ADM_ACTION_EHLTH:
3028 if (sv->check.state & CHK_ST_CONFIGURED) {
3029 sv->check.state |= CHK_ST_ENABLED;
3030 altered_servers++;
3031 total_servers++;
3032 }
3033 break;
3034 case ST_ADM_ACTION_HRUNN:
3035 if (!(sv->track)) {
3036 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003037 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003038 altered_servers++;
3039 total_servers++;
3040 }
3041 break;
3042 case ST_ADM_ACTION_HNOLB:
3043 if (!(sv->track)) {
3044 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003045 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003046 altered_servers++;
3047 total_servers++;
3048 }
3049 break;
3050 case ST_ADM_ACTION_HDOWN:
3051 if (!(sv->track)) {
3052 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003053 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003054 altered_servers++;
3055 total_servers++;
3056 }
3057 break;
3058 case ST_ADM_ACTION_DAGENT:
3059 if (sv->agent.state & CHK_ST_CONFIGURED) {
3060 sv->agent.state &= ~CHK_ST_ENABLED;
3061 altered_servers++;
3062 total_servers++;
3063 }
3064 break;
3065 case ST_ADM_ACTION_EAGENT:
3066 if (sv->agent.state & CHK_ST_CONFIGURED) {
3067 sv->agent.state |= CHK_ST_ENABLED;
3068 altered_servers++;
3069 total_servers++;
3070 }
3071 break;
3072 case ST_ADM_ACTION_ARUNN:
3073 if (sv->agent.state & CHK_ST_ENABLED) {
3074 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003075 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003076 altered_servers++;
3077 total_servers++;
3078 }
3079 break;
3080 case ST_ADM_ACTION_ADOWN:
3081 if (sv->agent.state & CHK_ST_ENABLED) {
3082 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003083 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003084 altered_servers++;
3085 total_servers++;
3086 }
3087 break;
3088 case ST_ADM_ACTION_READY:
3089 srv_adm_set_ready(sv);
3090 altered_servers++;
3091 total_servers++;
3092 break;
3093 case ST_ADM_ACTION_DRAIN:
3094 srv_adm_set_drain(sv);
3095 altered_servers++;
3096 total_servers++;
3097 break;
3098 case ST_ADM_ACTION_MAINT:
3099 srv_adm_set_maint(sv);
3100 altered_servers++;
3101 total_servers++;
3102 break;
3103 case ST_ADM_ACTION_SHUTDOWN:
3104 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003105 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003106 altered_servers++;
3107 total_servers++;
3108 }
3109 break;
3110 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003111 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003112 } else {
3113 /* the server name is unknown or ambiguous (duplicate names) */
3114 total_servers++;
3115 }
3116 }
3117 if (reprocess && px && action) {
3118 /* Now, we know the backend and the action chosen by the user.
3119 * We can safely restart from the first server parameter
3120 * to reprocess them
3121 */
3122 cur_param = st_cur_param;
3123 next_param = st_next_param;
3124 reprocess = 0;
3125 goto reprocess_servers;
3126 }
3127
3128 next_param = cur_param;
3129 }
3130 }
3131
3132 if (total_servers == 0) {
3133 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3134 }
3135 else if (altered_servers == 0) {
3136 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3137 }
3138 else if (altered_servers == total_servers) {
3139 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3140 }
3141 else {
3142 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3143 }
3144 out:
3145 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003146 wait:
3147 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3148 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003149}
3150
3151
Christopher Fauletb7f88902019-07-15 21:56:43 +02003152static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003153{
3154 struct stream *s = si_strm(si);
3155 struct uri_auth *uri = s->be->uri_auth;
3156 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003157 struct htx_sl *sl;
3158 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003159
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003160 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);
3161 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3162 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003163 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003164 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003165
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003166 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003167 goto full;
3168 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3169 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3170 goto full;
3171 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003172 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3173 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3174 goto full;
3175 }
Christopher Fauletef779222018-10-31 08:47:01 +01003176 else {
3177 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3178 goto full;
3179 }
3180
3181 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3182 const char *refresh = U2A(uri->refresh);
3183 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3184 goto full;
3185 }
3186
3187 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3188 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3189 goto full;
3190 }
3191
3192 if (!htx_add_endof(htx, HTX_BLK_EOH))
3193 goto full;
3194
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003195 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003196 return 1;
3197
3198 full:
3199 htx_reset(htx);
3200 si_rx_room_blk(si);
3201 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003202}
3203
Christopher Fauletef779222018-10-31 08:47:01 +01003204
Christopher Fauletb7f88902019-07-15 21:56:43 +02003205static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003206{
3207 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3208 struct stream *s = si_strm(si);
3209 struct uri_auth *uri = s->be->uri_auth;
3210 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003211 struct htx_sl *sl;
3212 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003213
3214 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3215 scope_txt[0] = 0;
3216 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003217 const char *scope_ptr = stats_scope_ptr(appctx, si);
3218
Christopher Fauletef779222018-10-31 08:47:01 +01003219 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003220 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003221 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3222 }
3223
3224 /* We don't want to land on the posted stats page because a refresh will
3225 * repost the data. We don't want this to happen on accident so we redirect
3226 * the browse to the stats page with a GET.
3227 */
3228 chunk_printf(&trash, "%s;st=%s%s%s%s",
3229 uri->uri_prefix,
3230 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3231 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3232 stat_status_codes[appctx->ctx.stats.st_code]) ?
3233 stat_status_codes[appctx->ctx.stats.st_code] :
3234 stat_status_codes[STAT_STATUS_UNKN],
3235 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3236 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3237 scope_txt);
3238
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003239 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3240 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3241 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003242 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003243 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003244
3245 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003246 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3247 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3248 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3249 goto full;
3250
3251 if (!htx_add_endof(htx, HTX_BLK_EOH))
3252 goto full;
3253
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003254 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003255 return 1;
3256
3257full:
3258 htx_reset(htx);
3259 si_rx_room_blk(si);
3260 return 0;
3261}
William Lallemand74c24fb2016-11-21 17:18:36 +01003262
Simon Horman05ee2132017-01-04 09:37:25 +01003263
William Lallemand74c24fb2016-11-21 17:18:36 +01003264/* This I/O handler runs as an applet embedded in a stream interface. It is
3265 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3266 * appctx->st0 contains the operation in progress (dump, done). The handler
3267 * automatically unregisters itself once transfer is complete.
3268 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003269static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003270{
3271 struct stream_interface *si = appctx->owner;
3272 struct stream *s = si_strm(si);
3273 struct channel *req = si_oc(si);
3274 struct channel *res = si_ic(si);
3275 struct htx *req_htx, *res_htx;
3276
3277 res_htx = htx_from_buf(&res->buf);
3278
3279 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3280 goto out;
3281
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003282 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003283 if (!b_size(&res->buf)) {
3284 si_rx_room_blk(si);
3285 goto out;
3286 }
3287
3288 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003289 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3290 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003291
3292 /* all states are processed in sequence */
3293 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003294 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003295 if (s->txn->meth == HTTP_METH_HEAD)
3296 appctx->st0 = STAT_HTTP_DONE;
3297 else
3298 appctx->st0 = STAT_HTTP_DUMP;
3299 }
3300 }
3301
3302 if (appctx->st0 == STAT_HTTP_DUMP) {
3303 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3304 appctx->st0 = STAT_HTTP_DONE;
3305 }
3306
3307 if (appctx->st0 == STAT_HTTP_POST) {
3308 if (stats_process_http_post(si))
3309 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003310 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003311 appctx->st0 = STAT_HTTP_DONE;
3312 }
3313
3314 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003315 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003316 appctx->st0 = STAT_HTTP_DONE;
3317 }
3318
3319 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003320 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003321 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3322 si_rx_room_blk(si);
3323 goto out;
3324 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003325 channel_add_input(&s->res, 1);
3326 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003327 }
3328
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003329 if (appctx->st0 == STAT_HTTP_END) {
3330 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003331 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003332 si_shutr(si);
3333 }
3334
3335 /* eat the whole request */
3336 if (co_data(req)) {
3337 req_htx = htx_from_buf(&req->buf);
3338 co_htx_skip(req, req_htx, co_data(req));
3339 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003340 }
3341 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003342
Christopher Fauletef779222018-10-31 08:47:01 +01003343 out:
3344 /* we have left the request in the buffer for the case where we
3345 * process a POST, and this automatically re-enables activity on
3346 * read. It's better to indicate that we want to stop reading when
3347 * we're sending, so that we know there's at most one direction
3348 * deciding to wake the applet up. It saves it from looping when
3349 * emitting large blocks into small TCP windows.
3350 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003351 htx_to_buf(res_htx, &res->buf);
3352 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003353 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003354}
3355
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003356/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003357static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003358 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003359{
3360 int field;
3361
3362 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3363 if (!field_format(info, field))
3364 continue;
3365
Willy Tarreaueaa55372019-10-09 07:39:11 +02003366 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003367 return 0;
3368 if (!stats_emit_raw_data_field(out, &info[field]))
3369 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003370 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3371 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003372 if (!chunk_strcat(out, "\n"))
3373 return 0;
3374 }
3375 return 1;
3376}
3377
3378/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003379static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003380 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003381{
3382 int field;
3383
3384 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3385 if (!field_format(info, field))
3386 continue;
3387
Willy Tarreaueaa55372019-10-09 07:39:11 +02003388 if (!chunk_appendf(out, "%d.%s.%u:", field, info_fields[field].name, info[INF_PROCESS_NUM].u.u32))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003389 return 0;
3390 if (!stats_emit_field_tags(out, &info[field], ':'))
3391 return 0;
3392 if (!stats_emit_typed_data_field(out, &info[field]))
3393 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003394 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3395 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003396 if (!chunk_strcat(out, "\n"))
3397 return 0;
3398 }
3399 return 1;
3400}
3401
3402/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003403 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003404 * INF_TOTAL_FIELDS. If this length is less then this value, the
3405 * function returns 0, otherwise, it returns 1.
3406 */
3407int stats_fill_info(struct field *info, int len)
3408{
3409 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003410 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003411
3412#ifdef USE_OPENSSL
3413 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3414 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3415 int ssl_reuse = 0;
3416
3417 if (ssl_key_rate < ssl_sess_rate) {
3418 /* count the ssl reuse ratio and avoid overflows in both directions */
3419 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3420 }
3421#endif
3422
3423 if (len < INF_TOTAL_FIELDS)
3424 return 0;
3425
3426 chunk_reset(out);
3427 memset(info, 0, sizeof(*info) * len);
3428
3429 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003430 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3431 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003432
Yves Lafon95317282018-02-26 11:10:37 +01003433 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003434 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3435 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3436 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3437
3438 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3439 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3440
3441 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3442 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3443 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3444 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3445 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3446 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3447 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3448 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3449 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3450 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3451 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3452 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3453#ifdef USE_OPENSSL
3454 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3455 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3456 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3457#endif
3458 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3459 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3460 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3461 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3462 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3463 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3464 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3465 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3466 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3467
3468#ifdef USE_OPENSSL
3469 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3470 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3471 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3472 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3473 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3474 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3475 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3476 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3477 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3478 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3479#endif
3480 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3481 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3482 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3483#ifdef USE_ZLIB
3484 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3485 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3486#endif
3487 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003488 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003489 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003490 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3491 if (global.desc)
3492 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003493 info[INF_STOPPING] = mkf_u32(0, stopping);
3494 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003495 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003496 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003497 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003498 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003499 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003500 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003501 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003502 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3503 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 +02003504 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003505
3506 return 1;
3507}
3508
3509/* This function dumps information onto the stream interface's read buffer.
3510 * It returns 0 as long as it does not complete, non-zero upon completion.
3511 * No state is used.
3512 */
3513static int stats_dump_info_to_buffer(struct stream_interface *si)
3514{
3515 struct appctx *appctx = __objt_appctx(si->end);
3516
3517 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3518 return 0;
3519
3520 chunk_reset(&trash);
3521
3522 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003523 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003524 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003525 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003526 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003527 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003528
Willy Tarreau06d80a92017-10-19 14:32:15 +02003529 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003530 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003531 return 0;
3532 }
3533
3534 return 1;
3535}
3536
Simon Horman6f6bb382017-01-04 09:37:26 +01003537/* This function dumps the schema onto the stream interface's read buffer.
3538 * It returns 0 as long as it does not complete, non-zero upon completion.
3539 * No state is used.
3540 *
3541 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3542 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3543 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003544static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003545{
3546
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003547 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003548
3549 chunk_strcat(out,
3550 "{"
3551 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3552 "\"oneOf\":["
3553 "{"
3554 "\"title\":\"Info\","
3555 "\"type\":\"array\","
3556 "\"items\":{"
3557 "\"properties\":{"
3558 "\"title\":\"InfoItem\","
3559 "\"type\":\"object\","
3560 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3561 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3562 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3563 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3564 "},"
3565 "\"required\":[\"field\",\"processNum\",\"tags\","
3566 "\"value\"]"
3567 "}"
3568 "},"
3569 "{"
3570 "\"title\":\"Stat\","
3571 "\"type\":\"array\","
3572 "\"items\":{"
3573 "\"title\":\"InfoItem\","
3574 "\"type\":\"object\","
3575 "\"properties\":{"
3576 "\"objType\":{"
3577 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3578 "\"Server\",\"Unknown\"]"
3579 "},"
3580 "\"proxyId\":{"
3581 "\"type\":\"integer\","
3582 "\"minimum\":0"
3583 "},"
3584 "\"id\":{"
3585 "\"type\":\"integer\","
3586 "\"minimum\":0"
3587 "},"
3588 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3589 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3590 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3591 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3592 "},"
3593 "\"required\":[\"objType\",\"proxyId\",\"id\","
3594 "\"field\",\"processNum\",\"tags\","
3595 "\"value\"]"
3596 "}"
3597 "},"
3598 "{"
3599 "\"title\":\"Error\","
3600 "\"type\":\"object\","
3601 "\"properties\":{"
3602 "\"errorStr\":{"
3603 "\"type\":\"string\""
3604 "},"
3605 "\"required\":[\"errorStr\"]"
3606 "}"
3607 "}"
3608 "],"
3609 "\"definitions\":{"
3610 "\"field\":{"
3611 "\"type\":\"object\","
3612 "\"pos\":{"
3613 "\"type\":\"integer\","
3614 "\"minimum\":0"
3615 "},"
3616 "\"name\":{"
3617 "\"type\":\"string\""
3618 "},"
3619 "\"required\":[\"pos\",\"name\"]"
3620 "},"
3621 "\"processNum\":{"
3622 "\"type\":\"integer\","
3623 "\"minimum\":1"
3624 "},"
3625 "\"tags\":{"
3626 "\"type\":\"object\","
3627 "\"origin\":{"
3628 "\"type\":\"string\","
3629 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3630 "\"Config\",\"Product\",\"Unknown\"]"
3631 "},"
3632 "\"nature\":{"
3633 "\"type\":\"string\","
3634 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3635 "\"Rate\",\"Counter\",\"Duration\","
3636 "\"Age\",\"Time\",\"Name\",\"Output\","
3637 "\"Avg\", \"Unknown\"]"
3638 "},"
3639 "\"scope\":{"
3640 "\"type\":\"string\","
3641 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3642 "\"System\",\"Unknown\"]"
3643 "},"
3644 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3645 "},"
3646 "\"typedValue\":{"
3647 "\"type\":\"object\","
3648 "\"oneOf\":["
3649 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3650 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3651 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3652 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3653 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3654 "],"
3655 "\"definitions\":{"
3656 "\"s32Value\":{"
3657 "\"properties\":{"
3658 "\"type\":{"
3659 "\"type\":\"string\","
3660 "\"enum\":[\"s32\"]"
3661 "},"
3662 "\"value\":{"
3663 "\"type\":\"integer\","
3664 "\"minimum\":-2147483648,"
3665 "\"maximum\":2147483647"
3666 "}"
3667 "},"
3668 "\"required\":[\"type\",\"value\"]"
3669 "},"
3670 "\"s64Value\":{"
3671 "\"properties\":{"
3672 "\"type\":{"
3673 "\"type\":\"string\","
3674 "\"enum\":[\"s64\"]"
3675 "},"
3676 "\"value\":{"
3677 "\"type\":\"integer\","
3678 "\"minimum\":-9007199254740991,"
3679 "\"maximum\":9007199254740991"
3680 "}"
3681 "},"
3682 "\"required\":[\"type\",\"value\"]"
3683 "},"
3684 "\"u32Value\":{"
3685 "\"properties\":{"
3686 "\"type\":{"
3687 "\"type\":\"string\","
3688 "\"enum\":[\"u32\"]"
3689 "},"
3690 "\"value\":{"
3691 "\"type\":\"integer\","
3692 "\"minimum\":0,"
3693 "\"maximum\":4294967295"
3694 "}"
3695 "},"
3696 "\"required\":[\"type\",\"value\"]"
3697 "},"
3698 "\"u64Value\":{"
3699 "\"properties\":{"
3700 "\"type\":{"
3701 "\"type\":\"string\","
3702 "\"enum\":[\"u64\"]"
3703 "},"
3704 "\"value\":{"
3705 "\"type\":\"integer\","
3706 "\"minimum\":0,"
3707 "\"maximum\":9007199254740991"
3708 "}"
3709 "},"
3710 "\"required\":[\"type\",\"value\"]"
3711 "},"
3712 "\"strValue\":{"
3713 "\"properties\":{"
3714 "\"type\":{"
3715 "\"type\":\"string\","
3716 "\"enum\":[\"str\"]"
3717 "},"
3718 "\"value\":{\"type\":\"string\"}"
3719 "},"
3720 "\"required\":[\"type\",\"value\"]"
3721 "},"
3722 "\"unknownValue\":{"
3723 "\"properties\":{"
3724 "\"type\":{"
3725 "\"type\":\"integer\","
3726 "\"minimum\":0"
3727 "},"
3728 "\"value\":{"
3729 "\"type\":\"string\","
3730 "\"enum\":[\"unknown\"]"
3731 "}"
3732 "},"
3733 "\"required\":[\"type\",\"value\"]"
3734 "}"
3735 "}"
3736 "}"
3737 "}"
3738 "}");
3739
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003740 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003741 chunk_reset(out);
3742 chunk_appendf(out,
3743 "{\"errorStr\":\"output buffer too short\"}");
3744 }
3745}
3746
3747/* This function dumps the schema onto the stream interface's read buffer.
3748 * It returns 0 as long as it does not complete, non-zero upon completion.
3749 * No state is used.
3750 */
3751static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3752{
3753 chunk_reset(&trash);
3754
3755 stats_dump_json_schema(&trash);
3756
Willy Tarreau06d80a92017-10-19 14:32:15 +02003757 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003758 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003759 return 0;
3760 }
3761
3762 return 1;
3763}
3764
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003765static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003766{
3767 struct proxy *px;
3768 struct server *sv;
3769 struct listener *li;
3770 int clrall = 0;
3771
3772 if (strcmp(args[2], "all") == 0)
3773 clrall = 1;
3774
3775 /* check permissions */
3776 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3777 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3778 return 1;
3779
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003780 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003781 if (clrall) {
3782 memset(&px->be_counters, 0, sizeof(px->be_counters));
3783 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3784 }
3785 else {
3786 px->be_counters.conn_max = 0;
3787 px->be_counters.p.http.rps_max = 0;
3788 px->be_counters.sps_max = 0;
3789 px->be_counters.cps_max = 0;
3790 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003791 px->be_counters.qtime_max = 0;
3792 px->be_counters.ctime_max = 0;
3793 px->be_counters.dtime_max = 0;
3794 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003795
3796 px->fe_counters.conn_max = 0;
3797 px->fe_counters.p.http.rps_max = 0;
3798 px->fe_counters.sps_max = 0;
3799 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003800 }
3801
3802 for (sv = px->srv; sv; sv = sv->next)
3803 if (clrall)
3804 memset(&sv->counters, 0, sizeof(sv->counters));
3805 else {
3806 sv->counters.cur_sess_max = 0;
3807 sv->counters.nbpend_max = 0;
3808 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003809 sv->counters.qtime_max = 0;
3810 sv->counters.ctime_max = 0;
3811 sv->counters.dtime_max = 0;
3812 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003813 }
3814
3815 list_for_each_entry(li, &px->conf.listeners, by_fe)
3816 if (li->counters) {
3817 if (clrall)
3818 memset(li->counters, 0, sizeof(*li->counters));
3819 else
3820 li->counters->conn_max = 0;
3821 }
3822 }
3823
3824 global.cps_max = 0;
3825 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003826 global.ssl_max = 0;
3827 global.ssl_fe_keys_max = 0;
3828 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003829
3830 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003831 return 1;
3832}
3833
3834
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003835static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003836{
Willy Tarreau2f397382019-10-09 11:43:59 +02003837 int arg = 2;
3838
Willy Tarreaud25fc792016-12-16 12:33:47 +01003839 appctx->ctx.stats.scope_str = 0;
3840 appctx->ctx.stats.scope_len = 0;
3841 appctx->ctx.stats.flags = 0;
3842
Willy Tarreau2f397382019-10-09 11:43:59 +02003843 while (*args[arg]) {
3844 if (strcmp(args[arg], "typed") == 0)
3845 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3846 else if (strcmp(args[arg], "json") == 0)
3847 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3848 else if (strcmp(args[arg], "desc") == 0)
3849 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3850 arg++;
3851 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003852 return 0;
3853}
3854
3855
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003856static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003857{
Willy Tarreau2f397382019-10-09 11:43:59 +02003858 int arg = 2;
3859
Willy Tarreaud25fc792016-12-16 12:33:47 +01003860 appctx->ctx.stats.scope_str = 0;
3861 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003862 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3863
3864 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3865 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003866
Willy Tarreau2f397382019-10-09 11:43:59 +02003867 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003868 struct proxy *px;
3869
Willy Tarreau2f397382019-10-09 11:43:59 +02003870 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003871 if (px)
3872 appctx->ctx.stats.iid = px->uuid;
3873 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003874 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003875
Willy Tarreau9d008692019-08-09 11:21:01 +02003876 if (!appctx->ctx.stats.iid)
3877 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003878
Willy Tarreau2b812e22016-11-22 16:18:05 +01003879 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003880 appctx->ctx.stats.type = atoi(args[arg+1]);
3881 appctx->ctx.stats.sid = atoi(args[arg+2]);
3882 arg += 3;
3883 }
3884
3885 while (*args[arg]) {
3886 if (strcmp(args[arg], "typed") == 0)
3887 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3888 else if (strcmp(args[arg], "json") == 0)
3889 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3890 else if (strcmp(args[arg], "desc") == 0)
3891 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3892 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003893 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003894
Willy Tarreau2b812e22016-11-22 16:18:05 +01003895 return 0;
3896}
3897
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003898static int cli_io_handler_dump_info(struct appctx *appctx)
3899{
3900 return stats_dump_info_to_buffer(appctx->owner);
3901}
3902
Willy Tarreau2b812e22016-11-22 16:18:05 +01003903/* This I/O handler runs as an applet embedded in a stream interface. It is
3904 * used to send raw stats over a socket.
3905 */
3906static int cli_io_handler_dump_stat(struct appctx *appctx)
3907{
Christopher Fauletef779222018-10-31 08:47:01 +01003908 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003909}
3910
Simon Horman6f6bb382017-01-04 09:37:26 +01003911static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3912{
3913 return stats_dump_json_schema_to_buffer(appctx->owner);
3914}
3915
Willy Tarreau2b812e22016-11-22 16:18:05 +01003916/* register cli keywords */
3917static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003918 { { "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 +02003919 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3920 { { "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 +01003921 { { "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 +01003922 {{},}
3923}};
3924
Willy Tarreau0108d902018-11-25 19:14:37 +01003925INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3926
William Lallemand74c24fb2016-11-21 17:18:36 +01003927struct applet http_stats_applet = {
3928 .obj_type = OBJ_TYPE_APPLET,
3929 .name = "<STATS>", /* used for logging */
3930 .fct = http_stats_io_handler,
3931 .release = NULL,
3932};
3933
William Lallemand74c24fb2016-11-21 17:18:36 +01003934/*
3935 * Local variables:
3936 * c-indent-level: 8
3937 * c-basic-offset: 8
3938 * End:
3939 */