blob: 82e2b1f6d9fab16eda36755ba34e1acfdd9abcae [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020028#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020035#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020036#include <haproxy/debug.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020037#include <haproxy/dns.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020044#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020045#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020046#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020047#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020048#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020049#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020050#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020051#include <haproxy/pipe.h>
52#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020053#include <haproxy/proxy.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020054#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020055#include <haproxy/session.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020056#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020057#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020058#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020059#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020060#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020061#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020062#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020063#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020064#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020065#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066
William Lallemand74c24fb2016-11-21 17:18:36 +010067
Christopher Faulet2b9b6782019-02-27 16:42:58 +010068/* status codes available for the stats admin page (strictly 4 chars length) */
69const char *stat_status_codes[STAT_STATUS_SIZE] = {
70 [STAT_STATUS_DENY] = "DENY",
71 [STAT_STATUS_DONE] = "DONE",
72 [STAT_STATUS_ERRP] = "ERRP",
73 [STAT_STATUS_EXCD] = "EXCD",
74 [STAT_STATUS_NONE] = "NONE",
75 [STAT_STATUS_PART] = "PART",
76 [STAT_STATUS_UNKN] = "UNKN",
77 [STAT_STATUS_IVAL] = "IVAL",
78};
79
Willy Tarreau0baac8c2016-11-22 16:36:53 +010080/* These are the field names for each INF_* field position. Please pay attention
81 * to always use the exact same name except that the strings for new names must
82 * be lower case or CamelCase while the enum entries must be upper case.
83 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020084const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020085 [INF_NAME] = { .name = "Name", .desc = "Product name" },
86 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
87 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
88 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
89 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
90 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
91 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
92 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
93 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
94 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
95 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
96 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
97 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
98 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
99 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
100 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
101 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
102 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
103 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
104 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
105 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
106 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
107 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
108 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
109 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
110 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
111 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
112 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
113 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
114 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
115 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
116 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
117 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
118 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
119 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
120 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
121 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
122 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
123 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
124 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
125 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
126 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
127 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
128 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
129 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
130 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
131 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
132 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
133 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
134 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
135 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
136 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
137 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
138 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
139 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
140 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
141 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
142 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
143 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
144 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
145 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
146 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Christopher Fauletaaa70852020-07-10 13:56:30 +0200147 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicdedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200148 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200149 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200150 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100151};
152
Willy Tarreaueaa55372019-10-09 07:39:11 +0200153const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
155 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
156 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
157 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
158 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
159 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
160 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
161 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
162 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
163 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
164 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
165 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
166 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
167 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
168 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
169 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
170 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
171 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
Willy Tarreaubd715102020-10-23 22:44:30 +0200172 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server's effective weight, or sum of active servers' effective weights for a backend" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200173 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
174 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
175 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
176 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
177 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
178 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
179 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
180 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
181 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
182 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
183 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
184 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
185 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
186 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
187 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
188 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
189 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
190 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
191 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
192 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
193 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
194 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
195 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
196 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
197 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
198 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
199 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
200 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
201 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
202 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
203 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
204 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
205 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
206 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
207 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
208 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
209 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
210 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
211 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
212 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
213 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
214 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
215 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
216 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
217 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
218 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
219 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
220 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
221 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
222 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
223 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
224 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
225 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
226 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
227 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
228 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
229 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
230 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
231 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
232 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
233 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
234 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
235 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
236 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
237 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
238 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
239 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
240 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
241 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
242 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
243 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100244 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
245 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
246 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
247 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100248 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200249 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
250 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
251 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200252 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200253 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100254};
255
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100256/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100257static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200258
259/* description of statistics (static and dynamic) */
260static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
261static size_t stat_count[STATS_DOMAIN_COUNT];
262
263/* one line for stats */
264static struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100265
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200266/* list of all registered stats module */
267static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
268 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200269 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_DNS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200270};
271
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200272static inline uint8_t stats_get_domain(uint32_t domain)
273{
274 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
275}
276
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200277static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
278{
279 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
280}
281
Christopher Faulet6338a082019-09-09 15:50:54 +0200282static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100283
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200284int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100285{
286 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100287 if (chk->data >= channel_htx_recv_max(chn, htx))
288 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200289 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100290 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100291 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100292 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100293 }
294 else {
295 if (ci_putchk(chn, chk) == -1)
296 return 0;
297 }
298 return 1;
299}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100300
Christopher Fauleted7a0662019-01-14 11:07:34 +0100301static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
302{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200303 struct channel *req = si_oc(si);
304 struct htx *htx = htxbuf(&req->buf);
305 struct htx_blk *blk;
306 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100307
Christopher Fauletb7f88902019-07-15 21:56:43 +0200308 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100309 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200310 ALREADY_CHECKED(blk);
311 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
312 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100313}
314
William Lallemand74c24fb2016-11-21 17:18:36 +0100315/*
316 * http_stats_io_handler()
317 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
318 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100319 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100320 * -> stats_dump_html_head() // emits the HTML headers
321 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
322 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
323 * -> stats_dump_html_px_hdr()
324 * -> stats_dump_fe_stats()
325 * -> stats_dump_li_stats()
326 * -> stats_dump_sv_stats()
327 * -> stats_dump_be_stats()
328 * -> stats_dump_html_px_end()
329 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100330 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100331 */
332
333
William Lallemand74c24fb2016-11-21 17:18:36 +0100334/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
335 * for clearing it if needed.
336 * NOTE: Some tools happen to rely on the field position instead of its name,
337 * so please only append new fields at the end, never in the middle.
338 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200339static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100340{
341 int field;
342
343 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200344 if (stat_f[domain]) {
345 for (field = 0; field < stat_count[domain]; ++field) {
346 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100347
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200348 /* print special delimiter on proxy stats to mark end of
349 static fields */
350 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
351 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200352 }
353 }
354
William Lallemand74c24fb2016-11-21 17:18:36 +0100355 chunk_appendf(&trash, "\n");
356}
357
William Lallemand74c24fb2016-11-21 17:18:36 +0100358/* Emits a stats field without any surrounding element and properly encoded to
359 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
360 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200361int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100362{
363 switch (field_format(f, 0)) {
364 case FF_EMPTY: return 1;
365 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
366 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
367 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
368 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200369 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100370 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
371 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
372 }
373}
374
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200375const char *field_to_html_str(const struct field *f)
376{
377 switch (field_format(f, 0)) {
378 case FF_S32: return U2H(f->u.s32);
379 case FF_S64: return U2H(f->u.s64);
380 case FF_U64: return U2H(f->u.u64);
381 case FF_U32: return U2H(f->u.u32);
382 case FF_STR: return field_str(f, 0);
383 case FF_EMPTY:
384 default:
385 return "";
386 }
387}
388
William Lallemand74c24fb2016-11-21 17:18:36 +0100389/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
390 * output is supposed to be used on its own line. Returns non-zero on success, 0
391 * if the buffer is full.
392 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200393int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100394{
395 switch (field_format(f, 0)) {
396 case FF_EMPTY: return 1;
397 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
398 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
399 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
400 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200401 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100402 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
403 default: return chunk_appendf(out, "%08x:?", f->type);
404 }
405}
406
Simon Horman05ee2132017-01-04 09:37:25 +0100407/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
408 * the recommendation for interoperable integers in section 6 of RFC 7159.
409 */
410#define JSON_INT_MAX ((1ULL << 53) - 1)
411#define JSON_INT_MIN (0 - JSON_INT_MAX)
412
413/* Emits a stats field value and its type in JSON.
414 * Returns non-zero on success, 0 on error.
415 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200416int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100417{
418 int old_len;
419 char buf[20];
420 const char *type, *value = buf, *quote = "";
421
422 switch (field_format(f, 0)) {
423 case FF_EMPTY: return 1;
424 case FF_S32: type = "\"s32\"";
425 snprintf(buf, sizeof(buf), "%d", f->u.s32);
426 break;
427 case FF_U32: type = "\"u32\"";
428 snprintf(buf, sizeof(buf), "%u", f->u.u32);
429 break;
430 case FF_S64: type = "\"s64\"";
431 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
432 return 0;
433 type = "\"s64\"";
434 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
435 break;
436 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
437 return 0;
438 type = "\"u64\"";
439 snprintf(buf, sizeof(buf), "%llu",
440 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200441 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200442 case FF_FLT: type = "\"flt\"";
443 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100444 break;
445 case FF_STR: type = "\"str\"";
446 value = field_str(f, 0);
447 quote = "\"";
448 break;
449 default: snprintf(buf, sizeof(buf), "%u", f->type);
450 type = buf;
451 value = "unknown";
452 quote = "\"";
453 break;
454 }
455
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200456 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100457 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
458 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200459 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100460}
461
William Lallemand74c24fb2016-11-21 17:18:36 +0100462/* Emits an encoding of the field type on 3 characters followed by a delimiter.
463 * Returns non-zero on success, 0 if the buffer is full.
464 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200465int stats_emit_field_tags(struct buffer *out, const struct field *f,
466 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100467{
468 char origin, nature, scope;
469
470 switch (field_origin(f, 0)) {
471 case FO_METRIC: origin = 'M'; break;
472 case FO_STATUS: origin = 'S'; break;
473 case FO_KEY: origin = 'K'; break;
474 case FO_CONFIG: origin = 'C'; break;
475 case FO_PRODUCT: origin = 'P'; break;
476 default: origin = '?'; break;
477 }
478
479 switch (field_nature(f, 0)) {
480 case FN_GAUGE: nature = 'G'; break;
481 case FN_LIMIT: nature = 'L'; break;
482 case FN_MIN: nature = 'm'; break;
483 case FN_MAX: nature = 'M'; break;
484 case FN_RATE: nature = 'R'; break;
485 case FN_COUNTER: nature = 'C'; break;
486 case FN_DURATION: nature = 'D'; break;
487 case FN_AGE: nature = 'A'; break;
488 case FN_TIME: nature = 'T'; break;
489 case FN_NAME: nature = 'N'; break;
490 case FN_OUTPUT: nature = 'O'; break;
491 case FN_AVG: nature = 'a'; break;
492 default: nature = '?'; break;
493 }
494
495 switch (field_scope(f, 0)) {
496 case FS_PROCESS: scope = 'P'; break;
497 case FS_SERVICE: scope = 'S'; break;
498 case FS_SYSTEM: scope = 's'; break;
499 case FS_CLUSTER: scope = 'C'; break;
500 default: scope = '?'; break;
501 }
502
503 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
504}
505
Simon Horman05ee2132017-01-04 09:37:25 +0100506/* Emits an encoding of the field type as JSON.
507 * Returns non-zero on success, 0 if the buffer is full.
508 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200509int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100510{
511 const char *origin, *nature, *scope;
512 int old_len;
513
514 switch (field_origin(f, 0)) {
515 case FO_METRIC: origin = "Metric"; break;
516 case FO_STATUS: origin = "Status"; break;
517 case FO_KEY: origin = "Key"; break;
518 case FO_CONFIG: origin = "Config"; break;
519 case FO_PRODUCT: origin = "Product"; break;
520 default: origin = "Unknown"; break;
521 }
522
523 switch (field_nature(f, 0)) {
524 case FN_GAUGE: nature = "Gauge"; break;
525 case FN_LIMIT: nature = "Limit"; break;
526 case FN_MIN: nature = "Min"; break;
527 case FN_MAX: nature = "Max"; break;
528 case FN_RATE: nature = "Rate"; break;
529 case FN_COUNTER: nature = "Counter"; break;
530 case FN_DURATION: nature = "Duration"; break;
531 case FN_AGE: nature = "Age"; break;
532 case FN_TIME: nature = "Time"; break;
533 case FN_NAME: nature = "Name"; break;
534 case FN_OUTPUT: nature = "Output"; break;
535 case FN_AVG: nature = "Avg"; break;
536 default: nature = "Unknown"; break;
537 }
538
539 switch (field_scope(f, 0)) {
540 case FS_PROCESS: scope = "Process"; break;
541 case FS_SERVICE: scope = "Service"; break;
542 case FS_SYSTEM: scope = "System"; break;
543 case FS_CLUSTER: scope = "Cluster"; break;
544 default: scope = "Unknown"; break;
545 }
546
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200547 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100548 chunk_appendf(out, "\"tags\":{"
549 "\"origin\":\"%s\","
550 "\"nature\":\"%s\","
551 "\"scope\":\"%s\""
552 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200553 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100554}
William Lallemand74c24fb2016-11-21 17:18:36 +0100555
556/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200557static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200558 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200559 unsigned int flags,
560 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100561{
562 int field;
563
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200564 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100565 if (!stats_emit_raw_data_field(out, &stats[field]))
566 return 0;
567 if (!chunk_strcat(out, ","))
568 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200569
570 /* print special delimiter on proxy stats to mark end of
571 static fields */
572 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
573 if (!chunk_strcat(out, "-,"))
574 return 0;
575 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100576 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200577
William Lallemand74c24fb2016-11-21 17:18:36 +0100578 chunk_strcat(out, "\n");
579 return 1;
580}
581
582/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200583static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200584 const struct field *stats,
585 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200586 unsigned int flags,
587 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100588{
589 int field;
590
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200591 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100592 if (!stats[field].type)
593 continue;
594
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200595 switch (domain) {
596 case STATS_DOMAIN_PROXY:
597 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
598 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
599 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
600 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
601 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
602 '?',
603 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
604 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200605 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200606 stats[ST_F_PID].u.u32);
607 break;
608
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200609 case STATS_DOMAIN_DNS:
610 chunk_appendf(out, "D.%d.%s:", field,
611 stat_f[domain][field].name);
612 break;
613
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200614 default:
615 break;
616 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100617
618 if (!stats_emit_field_tags(out, &stats[field], ':'))
619 return 0;
620 if (!stats_emit_typed_data_field(out, &stats[field]))
621 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200622
623 if (flags & STAT_SHOW_FDESC
624 && !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].name)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200625 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200626 }
627
William Lallemand74c24fb2016-11-21 17:18:36 +0100628 if (!chunk_strcat(out, "\n"))
629 return 0;
630 }
631 return 1;
632}
633
Simon Horman05ee2132017-01-04 09:37:25 +0100634/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200635static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200636 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100637{
638 int field;
639 int started = 0;
640
641 if (!chunk_strcat(out, "["))
642 return 0;
643
644 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
645 int old_len;
646
647 if (!field_format(info, field))
648 continue;
649
650 if (started && !chunk_strcat(out, ","))
651 goto err;
652 started = 1;
653
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200654 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100655 chunk_appendf(out,
656 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
657 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200658 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100659 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200660 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100661 goto err;
662
663 if (!stats_emit_json_field_tags(out, &info[field]))
664 goto err;
665
666 if (!stats_emit_json_data_field(out, &info[field]))
667 goto err;
668
669 if (!chunk_strcat(out, "}"))
670 goto err;
671 }
672
673 if (!chunk_strcat(out, "]"))
674 goto err;
675 return 1;
676
677err:
678 chunk_reset(out);
679 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
680 return 0;
681}
682
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200683static void stats_print_proxy_field_json(struct buffer *out,
684 const struct field *stat,
685 const char *name,
686 int pos,
687 uint32_t field_type,
688 uint32_t iid,
689 uint32_t sid,
690 uint32_t pid)
691{
692 const char *obj_type;
693 switch (field_type) {
694 case STATS_TYPE_FE: obj_type = "Frontend"; break;
695 case STATS_TYPE_BE: obj_type = "Backend"; break;
696 case STATS_TYPE_SO: obj_type = "Listener"; break;
697 case STATS_TYPE_SV: obj_type = "Server"; break;
698 default: obj_type = "Unknown"; break;
699 }
700
701 chunk_appendf(out,
702 "{"
703 "\"objType\":\"%s\","
704 "\"proxyId\":%u,"
705 "\"id\":%u,"
706 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
707 "\"processNum\":%u,",
708 obj_type, iid, sid, pos, name, pid);
709}
710
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200711static void stats_print_dns_field_json(struct buffer *out,
712 const struct field *stat,
713 const char *name,
714 int pos)
715{
716 chunk_appendf(out,
717 "{"
718 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
719 pos, name);
720}
721
722
Simon Horman05ee2132017-01-04 09:37:25 +0100723/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200724static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200725 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200726 unsigned int flags,
727 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100728{
729 int field;
730 int started = 0;
731
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200732 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100733 return 0;
734 if (!chunk_strcat(out, "["))
735 return 0;
736
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200737 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100738 int old_len;
739
740 if (!stats[field].type)
741 continue;
742
743 if (started && !chunk_strcat(out, ","))
744 goto err;
745 started = 1;
746
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200748 if (domain == STATS_DOMAIN_PROXY) {
749 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200750 stat_f[domain][field].name,
751 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200752 stats[ST_F_TYPE].u.u32,
753 stats[ST_F_IID].u.u32,
754 stats[ST_F_SID].u.u32,
755 stats[ST_F_PID].u.u32);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200756 } else if (domain == STATS_DOMAIN_DNS) {
757 stats_print_dns_field_json(out, &stats[field],
758 stat_f[domain][field].name,
759 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200760 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200761
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200762 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100763 goto err;
764
765 if (!stats_emit_json_field_tags(out, &stats[field]))
766 goto err;
767
768 if (!stats_emit_json_data_field(out, &stats[field]))
769 goto err;
770
771 if (!chunk_strcat(out, "}"))
772 goto err;
773 }
774
775 if (!chunk_strcat(out, "]"))
776 goto err;
777
778 return 1;
779
780err:
781 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200782 if (flags & STAT_STARTED)
783 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100784 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
785 return 0;
786}
787
William Lallemand74c24fb2016-11-21 17:18:36 +0100788/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200789 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200790 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
791 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
792 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100793 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200794static int stats_dump_fields_html(struct buffer *out,
795 const struct field *stats,
796 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100797{
Willy Tarreau83061a82018-07-13 11:56:34 +0200798 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200799 struct stats_module *mod;
800 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100801
802 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
803 chunk_appendf(out,
804 /* name, queue */
805 "<tr class=\"frontend\">");
806
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200807 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100808 /* Column sub-heading for Enable or Disable server */
809 chunk_appendf(out, "<td></td>");
810 }
811
812 chunk_appendf(out,
813 "<td class=ac>"
814 "<a name=\"%s/Frontend\"></a>"
815 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
816 "<td colspan=3></td>"
817 "",
818 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
819
820 chunk_appendf(out,
821 /* sessions rate : current */
822 "<td><u>%s<div class=tips><table class=det>"
823 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
824 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
825 "",
826 U2H(stats[ST_F_RATE].u.u32),
827 U2H(stats[ST_F_CONN_RATE].u.u32),
828 U2H(stats[ST_F_RATE].u.u32));
829
830 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
831 chunk_appendf(out,
832 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
833 U2H(stats[ST_F_REQ_RATE].u.u32));
834
835 chunk_appendf(out,
836 "</table></div></u></td>"
837 /* sessions rate : max */
838 "<td><u>%s<div class=tips><table class=det>"
839 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
840 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
841 "",
842 U2H(stats[ST_F_RATE_MAX].u.u32),
843 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
844 U2H(stats[ST_F_RATE_MAX].u.u32));
845
846 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
847 chunk_appendf(out,
848 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
849 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
850
851 chunk_appendf(out,
852 "</table></div></u></td>"
853 /* sessions rate : limit */
854 "<td>%s</td>",
855 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
856
857 chunk_appendf(out,
858 /* sessions: current, max, limit, total */
859 "<td>%s</td><td>%s</td><td>%s</td>"
860 "<td><u>%s<div class=tips><table class=det>"
861 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
862 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
863 "",
864 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
865 U2H(stats[ST_F_STOT].u.u64),
866 U2H(stats[ST_F_CONN_TOT].u.u64),
867 U2H(stats[ST_F_STOT].u.u64));
868
869 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
870 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
871 chunk_appendf(out,
872 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
873 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
874 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
875 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
876 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
877 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
878 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
879 "<tr><th>- other responses:</th><td>%s</td></tr>"
880 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100881 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
882 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200883 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100884 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100885 "",
886 U2H(stats[ST_F_REQ_TOT].u.u64),
887 U2H(stats[ST_F_HRSP_1XX].u.u64),
888 U2H(stats[ST_F_HRSP_2XX].u.u64),
889 U2H(stats[ST_F_COMP_RSP].u.u64),
890 stats[ST_F_HRSP_2XX].u.u64 ?
891 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
892 U2H(stats[ST_F_HRSP_3XX].u.u64),
893 U2H(stats[ST_F_HRSP_4XX].u.u64),
894 U2H(stats[ST_F_HRSP_5XX].u.u64),
895 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200896 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100897 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
898 U2H(stats[ST_F_CACHE_HITS].u.u64),
899 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
900 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100901 U2H(stats[ST_F_WREW].u.u64),
902 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100903 }
904
905 chunk_appendf(out,
906 "</table></div></u></td>"
907 /* sessions: lbtot, lastsess */
908 "<td></td><td></td>"
909 /* bytes : in */
910 "<td>%s</td>"
911 "",
912 U2H(stats[ST_F_BIN].u.u64));
913
914 chunk_appendf(out,
915 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
916 "<td>%s%s<div class=tips><table class=det>"
917 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
918 "<tr><th>Compression in:</th><td>%s</td></tr>"
919 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
920 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
921 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
922 "</table></div>%s</td>",
923 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
924 U2H(stats[ST_F_BOUT].u.u64),
925 U2H(stats[ST_F_BOUT].u.u64),
926 U2H(stats[ST_F_COMP_IN].u.u64),
927 U2H(stats[ST_F_COMP_OUT].u.u64),
928 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
929 U2H(stats[ST_F_COMP_BYP].u.u64),
930 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
931 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,
932 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
933
934 chunk_appendf(out,
935 /* denied: req, resp */
936 "<td>%s</td><td>%s</td>"
937 /* errors : request, connect, response */
938 "<td>%s</td><td></td><td></td>"
939 /* warnings: retries, redispatches */
940 "<td></td><td></td>"
941 /* server status : reflect frontend status */
942 "<td class=ac>%s</td>"
943 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200944 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100945 "",
946 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
947 U2H(stats[ST_F_EREQ].u.u64),
948 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200949
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200950 if (flags & STAT_SHMODULES) {
951 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
952 chunk_appendf(out,
953 "<td><u>%s<div class=tips><table class=det>",
954 mod->name);
955 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
956 for (j = 0; j < mod->stats_count; ++j) {
957 chunk_appendf(out,
958 "<tr><th>%s</th><td>%s</td></tr>",
959 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
960 ++i;
961 }
962 } else {
963 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200964 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200965
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200966 chunk_appendf(out,
967 "</table></div></u></td>");
968 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200969 }
970
971 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100972 }
973 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
974 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200975 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100976 /* Column sub-heading for Enable or Disable server */
977 chunk_appendf(out, "<td></td>");
978 }
979
980 chunk_appendf(out,
981 /* frontend name, listener name */
982 "<td class=ac><a name=\"%s/+%s\"></a>%s"
983 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
984 "",
985 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200986 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100987 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
988
Willy Tarreau708c4162019-10-09 10:19:16 +0200989 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100990 chunk_appendf(out, "<div class=tips>");
991
Willy Tarreau90807112020-02-25 08:16:33 +0100992 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100993 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
994 else if (*field_str(stats, ST_F_ADDR) == '[')
995 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
996 else if (*field_str(stats, ST_F_ADDR))
997 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
998
999 /* id */
1000 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1001 }
1002
1003 chunk_appendf(out,
1004 /* queue */
1005 "%s</td><td colspan=3></td>"
1006 /* sessions rate: current, max, limit */
1007 "<td colspan=3>&nbsp;</td>"
1008 /* sessions: current, max, limit, total, lbtot, lastsess */
1009 "<td>%s</td><td>%s</td><td>%s</td>"
1010 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1011 /* bytes: in, out */
1012 "<td>%s</td><td>%s</td>"
1013 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001014 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001015 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1016 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1017
1018 chunk_appendf(out,
1019 /* denied: req, resp */
1020 "<td>%s</td><td>%s</td>"
1021 /* errors: request, connect, response */
1022 "<td>%s</td><td></td><td></td>"
1023 /* warnings: retries, redispatches */
1024 "<td></td><td></td>"
1025 /* server status: reflect listener status */
1026 "<td class=ac>%s</td>"
1027 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001028 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001029 "",
1030 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1031 U2H(stats[ST_F_EREQ].u.u64),
1032 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001033
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001034 if (flags & STAT_SHMODULES) {
1035 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1036 chunk_appendf(out,
1037 "<td><u>%s<div class=tips><table class=det>",
1038 mod->name);
1039 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
1040 for (j = 0; j < mod->stats_count; ++j) {
1041 chunk_appendf(out,
1042 "<tr><th>%s</th><td>%s</td></tr>",
1043 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1044 ++i;
1045 }
1046 } else {
1047 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001048 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001049
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001050 chunk_appendf(out,
1051 "</table></div></u></td>");
1052 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001053 }
1054
1055 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001056 }
1057 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1058 const char *style;
1059
1060 /* determine the style to use depending on the server's state,
1061 * its health and weight. There isn't a 1-to-1 mapping between
1062 * state and styles for the cases where the server is (still)
1063 * up. The reason is that we don't want to report nolb and
1064 * drain with the same color.
1065 */
1066
1067 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1068 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1069 style = "down";
1070 }
1071 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
1072 style = "going_up";
1073 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001074 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1075 style = "draining";
1076 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001077 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
1078 style = "going_down";
1079 }
1080 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1081 style = "nolb";
1082 }
1083 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1084 style = "no_check";
1085 }
1086 else if (!stats[ST_F_CHKFAIL].type ||
1087 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1088 /* no check or max health = UP */
1089 if (stats[ST_F_WEIGHT].u.u32)
1090 style = "up";
1091 else
1092 style = "draining";
1093 }
1094 else {
1095 style = "going_down";
1096 }
1097
Willy Tarreau7b524852020-08-11 10:26:36 +02001098 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001099 chunk_appendf(out, "<tr class=\"maintain\">");
1100 else
1101 chunk_appendf(out,
1102 "<tr class=\"%s_%s\">",
1103 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1104
1105
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001106 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001107 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001108 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1109 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001110 field_str(stats, ST_F_SVNAME));
1111
1112 chunk_appendf(out,
1113 "<td class=ac><a name=\"%s/%s\"></a>%s"
1114 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1115 "",
1116 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001117 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001118 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1119
Willy Tarreau708c4162019-10-09 10:19:16 +02001120 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001121 chunk_appendf(out, "<div class=tips>");
1122
Willy Tarreau90807112020-02-25 08:16:33 +01001123 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001124 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1125 else if (*field_str(stats, ST_F_ADDR) == '[')
1126 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1127 else if (*field_str(stats, ST_F_ADDR))
1128 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1129
1130 /* id */
1131 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1132
1133 /* cookie */
1134 if (stats[ST_F_COOKIE].type) {
1135 chunk_appendf(out, ", cookie: '");
1136 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1137 chunk_htmlencode(out, &src);
1138 chunk_appendf(out, "'");
1139 }
1140
1141 chunk_appendf(out, "</div>");
1142 }
1143
1144 chunk_appendf(out,
1145 /* queue : current, max, limit */
1146 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1147 /* sessions rate : current, max, limit */
1148 "<td>%s</td><td>%s</td><td></td>"
1149 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001150 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001151 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1152 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1153
1154 chunk_appendf(out,
1155 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001156 "<td><u>%s<div class=tips>"
1157 "<table class=det>"
1158 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001159 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001160 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001161 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1162 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001163 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001164 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1165 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1166 "</table></div></u>"
1167 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001168 "<td><u>%s<div class=tips><table class=det>"
1169 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1170 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001171 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001172 U2H(stats[ST_F_SCUR].u.u32),
1173 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1174 U2H(stats[ST_F_SRV_ICUR].u.u32),
1175 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1176 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001177 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001178
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001179 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1180 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1181 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001182 U2H(stats[ST_F_STOT].u.u64),
1183 U2H(stats[ST_F_STOT].u.u64));
1184
1185 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1186 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001187 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001188 "<tr><th>New connections:</th><td>%s</td></tr>"
1189 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001190 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001191 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1192 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1193 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1194 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1195 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1196 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001197 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001198 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001199 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001200 U2H(stats[ST_F_CONNECT].u.u64),
1201 U2H(stats[ST_F_REUSE].u.u64),
1202 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1203 (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 +02001204 U2H(stats[ST_F_REQ_TOT].u.u64),
1205 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1206 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1207 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1208 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1209 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1210 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1211 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1212 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1213 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1214 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1215 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1216 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001217 U2H(stats[ST_F_WREW].u.u64),
1218 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001219 }
1220
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001221 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1222 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1223 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1224 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1225 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001226 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001227 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1228 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1229 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1230 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001231
1232 chunk_appendf(out,
1233 "</table></div></u></td>"
1234 /* sessions: lbtot, last */
1235 "<td>%s</td><td>%s</td>",
1236 U2H(stats[ST_F_LBTOT].u.u64),
1237 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1238
1239 chunk_appendf(out,
1240 /* bytes : in, out */
1241 "<td>%s</td><td>%s</td>"
1242 /* denied: req, resp */
1243 "<td></td><td>%s</td>"
1244 /* errors : request, connect */
1245 "<td></td><td>%s</td>"
1246 /* errors : response */
1247 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1248 /* warnings: retries, redispatches */
1249 "<td>%lld</td><td>%lld</td>"
1250 "",
1251 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1252 U2H(stats[ST_F_DRESP].u.u64),
1253 U2H(stats[ST_F_ECON].u.u64),
1254 U2H(stats[ST_F_ERESP].u.u64),
1255 (long long)stats[ST_F_CLI_ABRT].u.u64,
1256 (long long)stats[ST_F_SRV_ABRT].u.u64,
1257 (long long)stats[ST_F_WRETR].u.u64,
1258 (long long)stats[ST_F_WREDIS].u.u64);
1259
1260 /* status, last change */
1261 chunk_appendf(out, "<td class=ac>");
1262
1263 /* FIXME!!!!
1264 * LASTCHG should contain the last change for *this* server and must be computed
1265 * properly above, as was done below, ie: this server if maint, otherwise ref server
1266 * if tracking. Note that ref is either local or remote depending on tracking.
1267 */
1268
1269
Willy Tarreau7b524852020-08-11 10:26:36 +02001270 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001271 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1272 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001273 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001274 chunk_strcat(out, "<i>no check</i>");
1275 }
1276 else {
1277 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreau7b524852020-08-11 10:26:36 +02001278 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001279 if (stats[ST_F_CHECK_HEALTH].u.u32)
1280 chunk_strcat(out, " &uarr;");
1281 }
1282 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1283 chunk_strcat(out, " &darr;");
1284 }
1285
Willy Tarreau7b524852020-08-11 10:26:36 +02001286 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001287 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1288 chunk_appendf(out,
1289 "</td><td class=ac><u> %s",
1290 field_str(stats, ST_F_AGENT_STATUS));
1291
1292 if (stats[ST_F_AGENT_CODE].type)
1293 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1294
1295 if (stats[ST_F_AGENT_DURATION].type)
1296 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1297
1298 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1299
1300 if (*field_str(stats, ST_F_LAST_AGT)) {
1301 chunk_appendf(out, ": ");
1302 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1303 chunk_htmlencode(out, &src);
1304 }
1305 chunk_appendf(out, "</div></u>");
1306 }
1307 else if (stats[ST_F_CHECK_STATUS].type) {
1308 chunk_appendf(out,
1309 "</td><td class=ac><u> %s",
1310 field_str(stats, ST_F_CHECK_STATUS));
1311
1312 if (stats[ST_F_CHECK_CODE].type)
1313 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1314
1315 if (stats[ST_F_CHECK_DURATION].type)
1316 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1317
1318 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1319
1320 if (*field_str(stats, ST_F_LAST_CHK)) {
1321 chunk_appendf(out, ": ");
1322 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1323 chunk_htmlencode(out, &src);
1324 }
1325 chunk_appendf(out, "</div></u>");
1326 }
1327 else
1328 chunk_appendf(out, "</td><td>");
1329
1330 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001331 /* weight / uweight */
1332 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001333 /* act, bck */
1334 "<td class=ac>%s</td><td class=ac>%s</td>"
1335 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001336 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001337 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1338 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1339
1340 /* check failures: unique, fatal, down time */
1341 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1342 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1343 }
1344 else if (stats[ST_F_CHKFAIL].type) {
1345 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1346
1347 if (stats[ST_F_HANAFAIL].type)
1348 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1349
1350 chunk_appendf(out,
1351 "<div class=tips>Failed Health Checks%s</div></u></td>"
1352 "<td>%lld</td><td>%s</td>"
1353 "",
1354 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1355 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1356 }
1357 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1358 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1359 chunk_appendf(out,
1360 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1361 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1362 }
1363 else
1364 chunk_appendf(out, "<td colspan=3></td>");
1365
1366 /* throttle */
1367 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001368 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001369 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001370 chunk_appendf(out, "<td class=ac>-</td>");
1371
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001372 if (flags & STAT_SHMODULES) {
1373 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1374 chunk_appendf(out,
1375 "<td><u>%s<div class=tips><table class=det>",
1376 mod->name);
1377 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
1378 for (j = 0; j < mod->stats_count; ++j) {
1379 chunk_appendf(out,
1380 "<tr><th>%s</th><td>%s</td></tr>",
1381 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1382 ++i;
1383 }
1384 } else {
1385 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001386 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001387
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001388 chunk_appendf(out,
1389 "</table></div></u></td>");
1390 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001391 }
1392
1393 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001394 }
1395 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1396 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001397 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001398 /* Column sub-heading for Enable or Disable server */
1399 chunk_appendf(out, "<td></td>");
1400 }
1401 chunk_appendf(out,
1402 "<td class=ac>"
1403 /* name */
1404 "%s<a name=\"%s/Backend\"></a>"
1405 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1406 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001407 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001408 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1409
Willy Tarreau708c4162019-10-09 10:19:16 +02001410 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001411 /* balancing */
1412 chunk_appendf(out, "<div class=tips>balancing: %s",
1413 field_str(stats, ST_F_ALGO));
1414
1415 /* cookie */
1416 if (stats[ST_F_COOKIE].type) {
1417 chunk_appendf(out, ", cookie: '");
1418 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1419 chunk_htmlencode(out, &src);
1420 chunk_appendf(out, "'");
1421 }
1422 chunk_appendf(out, "</div>");
1423 }
1424
1425 chunk_appendf(out,
1426 "%s</td>"
1427 /* queue : current, max */
1428 "<td>%s</td><td>%s</td><td></td>"
1429 /* sessions rate : current, max, limit */
1430 "<td>%s</td><td>%s</td><td></td>"
1431 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001432 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001433 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1434 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1435
1436 chunk_appendf(out,
1437 /* sessions: current, max, limit, total */
1438 "<td>%s</td><td>%s</td><td>%s</td>"
1439 "<td><u>%s<div class=tips><table class=det>"
1440 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1441 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001442 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 +01001443 U2H(stats[ST_F_STOT].u.u64),
1444 U2H(stats[ST_F_STOT].u.u64));
1445
1446 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1447 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1448 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001449 "<tr><th>New connections:</th><td>%s</td></tr>"
1450 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001451 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1452 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1453 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1454 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1455 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1456 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1457 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1458 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001459 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1460 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001461 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001462 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001463 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001464 U2H(stats[ST_F_CONNECT].u.u64),
1465 U2H(stats[ST_F_REUSE].u.u64),
1466 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1467 (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 +01001468 U2H(stats[ST_F_REQ_TOT].u.u64),
1469 U2H(stats[ST_F_HRSP_1XX].u.u64),
1470 U2H(stats[ST_F_HRSP_2XX].u.u64),
1471 U2H(stats[ST_F_COMP_RSP].u.u64),
1472 stats[ST_F_HRSP_2XX].u.u64 ?
1473 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1474 U2H(stats[ST_F_HRSP_3XX].u.u64),
1475 U2H(stats[ST_F_HRSP_4XX].u.u64),
1476 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001477 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001478 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1479 U2H(stats[ST_F_CACHE_HITS].u.u64),
1480 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1481 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001482 U2H(stats[ST_F_WREW].u.u64),
1483 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001484 }
1485
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001486 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1487 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1488 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1489 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1490 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001491 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001492 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1493 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1494 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1495 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001496
1497 chunk_appendf(out,
1498 "</table></div></u></td>"
1499 /* sessions: lbtot, last */
1500 "<td>%s</td><td>%s</td>"
1501 /* bytes: in */
1502 "<td>%s</td>"
1503 "",
1504 U2H(stats[ST_F_LBTOT].u.u64),
1505 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1506 U2H(stats[ST_F_BIN].u.u64));
1507
1508 chunk_appendf(out,
1509 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1510 "<td>%s%s<div class=tips><table class=det>"
1511 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1512 "<tr><th>Compression in:</th><td>%s</td></tr>"
1513 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1514 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1515 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1516 "</table></div>%s</td>",
1517 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1518 U2H(stats[ST_F_BOUT].u.u64),
1519 U2H(stats[ST_F_BOUT].u.u64),
1520 U2H(stats[ST_F_COMP_IN].u.u64),
1521 U2H(stats[ST_F_COMP_OUT].u.u64),
1522 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1523 U2H(stats[ST_F_COMP_BYP].u.u64),
1524 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1525 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,
1526 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1527
1528 chunk_appendf(out,
1529 /* denied: req, resp */
1530 "<td>%s</td><td>%s</td>"
1531 /* errors : request, connect */
1532 "<td></td><td>%s</td>"
1533 /* errors : response */
1534 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1535 /* warnings: retries, redispatches */
1536 "<td>%lld</td><td>%lld</td>"
1537 /* backend status: reflect backend status (up/down): we display UP
1538 * if the backend has known working servers or if it has no server at
1539 * all (eg: for stats). Then we display the total weight, number of
1540 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001541 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001542 "<td class=ac>%d</td><td class=ac>%d</td>"
1543 "",
1544 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1545 U2H(stats[ST_F_ECON].u.u64),
1546 U2H(stats[ST_F_ERESP].u.u64),
1547 (long long)stats[ST_F_CLI_ABRT].u.u64,
1548 (long long)stats[ST_F_SRV_ABRT].u.u64,
1549 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1550 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1551 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreaubd715102020-10-23 22:44:30 +02001552 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001553 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1554
1555 chunk_appendf(out,
1556 /* rest of backend: nothing, down transitions, total downtime, throttle */
1557 "<td class=ac>&nbsp;</td><td>%d</td>"
1558 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001559 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001560 stats[ST_F_CHKDOWN].u.u32,
1561 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001562
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001563 if (flags & STAT_SHMODULES) {
1564 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1565 chunk_appendf(out,
1566 "<td><u>%s<div class=tips><table class=det>",
1567 mod->name);
1568 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
1569 for (j = 0; j < mod->stats_count; ++j) {
1570 chunk_appendf(out,
1571 "<tr><th>%s</th><td>%s</td></tr>",
1572 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1573 ++i;
1574 }
1575 } else {
1576 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001577 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001578 chunk_appendf(out,
1579 "</table></div></u></td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001580 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001581 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001582
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001583 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001584 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001585
William Lallemand74c24fb2016-11-21 17:18:36 +01001586 return 1;
1587}
1588
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001589int stats_dump_one_line(const struct field *stats, size_t stats_count,
1590 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001591{
Simon Horman05ee2132017-01-04 09:37:25 +01001592 int ret;
1593
William Lallemand74c24fb2016-11-21 17:18:36 +01001594 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001595 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001596 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001597 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001598 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001599 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001600 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001601 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001602
1603 if (ret)
1604 appctx->ctx.stats.flags |= STAT_STARTED;
1605
1606 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001607}
1608
1609/* Fill <stats> with the frontend statistics. <stats> is
1610 * preallocated array of length <len>. The length of the array
1611 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1612 * this value, the function returns 0, otherwise, it returns 1.
1613 */
1614int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1615{
1616 if (len < ST_F_TOTAL_FIELDS)
1617 return 0;
1618
William Lallemand74c24fb2016-11-21 17:18:36 +01001619 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1620 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1621 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1622 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1623 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1624 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1625 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1626 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1627 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1628 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1629 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1630 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1631 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1632 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
Willy Tarreauc3914d42020-09-24 08:39:22 +02001633 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->disabled ? "STOP" : "OPEN");
William Lallemand74c24fb2016-11-21 17:18:36 +01001634 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1635 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1636 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1637 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1638 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1639 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1640 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001641 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001642 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001643
1644 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1645 if (px->mode == PR_MODE_HTTP) {
1646 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1647 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1648 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1649 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1650 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1651 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1652 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001653 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1654 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001655 }
1656
1657 /* requests : req_rate, req_rate_max, req_tot, */
1658 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1659 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1660 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1661
1662 /* compression: in, out, bypassed, responses */
1663 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1664 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1665 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1666 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1667
1668 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1669 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1670 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1671 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1672
1673 return 1;
1674}
1675
1676/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1677 * the state from stream interface <si>. The caller is responsible for clearing
1678 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1679 */
1680static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1681{
1682 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001683 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1684 struct stats_module *mod;
1685 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001686
1687 if (!(px->cap & PR_CAP_FE))
1688 return 0;
1689
1690 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1691 return 0;
1692
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001693 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1694
William Lallemand74c24fb2016-11-21 17:18:36 +01001695 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1696 return 0;
1697
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001698 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1699 void *counters;
1700
1701 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1702 stats_count += mod->stats_count;
1703 continue;
1704 }
1705
1706 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1707 mod->fill_stats(counters, stats + stats_count);
1708 stats_count += mod->stats_count;
1709 }
1710
1711 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001712}
1713
1714/* Fill <stats> with the listener statistics. <stats> is
1715 * preallocated array of length <len>. The length of the array
1716 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1717 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001718 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001719 */
1720int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1721 struct field *stats, int len)
1722{
Willy Tarreau83061a82018-07-13 11:56:34 +02001723 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001724
1725 if (len < ST_F_TOTAL_FIELDS)
1726 return 0;
1727
1728 if (!l->counters)
1729 return 0;
1730
1731 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001732
1733 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1734 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1735 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1736 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1737 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1738 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1739 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1740 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1741 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1742 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1743 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1744 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1745 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1746 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001747 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 +01001748 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1749 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1750 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1751 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001752 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001753 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001754
Willy Tarreau708c4162019-10-09 10:19:16 +02001755 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001756 char str[INET6_ADDRSTRLEN];
1757 int port;
1758
Willy Tarreau37159062020-08-27 07:48:42 +02001759 port = get_host_port(&l->rx.addr);
1760 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001761 case AF_INET:
1762 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1763 chunk_appendf(out, "%s:%d", str, port);
1764 break;
1765 case AF_INET6:
1766 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1767 chunk_appendf(out, "[%s]:%d", str, port);
1768 break;
1769 case AF_UNIX:
1770 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1771 break;
1772 case -1:
1773 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1774 chunk_strcat(out, strerror(errno));
1775 break;
1776 default: /* address family not supported */
1777 break;
1778 }
1779 }
1780
1781 return 1;
1782}
1783
1784/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001785 * from stream interface <si>. The caller is responsible for clearing the trash
1786 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001787 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001788static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001789{
1790 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001791 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1792 struct stats_module *mod;
1793 size_t stats_count = ST_F_TOTAL_FIELDS;
1794
1795 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001796
Willy Tarreau43241ff2019-10-09 11:27:51 +02001797 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001798 return 0;
1799
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001800 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1801 void *counters;
1802
1803 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1804 stats_count += mod->stats_count;
1805 continue;
1806 }
1807
1808 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
1809 mod->fill_stats(counters, stats + stats_count);
1810 stats_count += mod->stats_count;
1811 }
1812
1813 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001814}
1815
1816enum srv_stats_state {
1817 SRV_STATS_STATE_DOWN = 0,
1818 SRV_STATS_STATE_DOWN_AGENT,
1819 SRV_STATS_STATE_GOING_UP,
1820 SRV_STATS_STATE_UP_GOING_DOWN,
1821 SRV_STATS_STATE_UP,
1822 SRV_STATS_STATE_NOLB_GOING_DOWN,
1823 SRV_STATS_STATE_NOLB,
1824 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1825 SRV_STATS_STATE_DRAIN,
1826 SRV_STATS_STATE_DRAIN_AGENT,
1827 SRV_STATS_STATE_NO_CHECK,
1828
1829 SRV_STATS_STATE_COUNT, /* Must be last */
1830};
1831
1832static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1833 [SRV_STATS_STATE_DOWN] = "DOWN",
1834 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1835 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1836 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1837 [SRV_STATS_STATE_UP] = "UP",
1838 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1839 [SRV_STATS_STATE_NOLB] = "NOLB",
1840 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1841 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1842 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1843 [SRV_STATS_STATE_NO_CHECK] = "no check"
1844};
1845
1846/* Fill <stats> with the server statistics. <stats> is
1847 * preallocated array of length <len>. The length of the array
1848 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1849 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001850 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001851 */
1852int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1853 struct field *stats, int len)
1854{
1855 struct server *via, *ref;
1856 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001857 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001858 enum srv_stats_state state;
1859 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001860 long long srv_samples_counter;
1861 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001862
1863 if (len < ST_F_TOTAL_FIELDS)
1864 return 0;
1865
William Lallemand74c24fb2016-11-21 17:18:36 +01001866 /* we have "via" which is the tracked server as described in the configuration,
1867 * and "ref" which is the checked server and the end of the chain.
1868 */
1869 via = sv->track ? sv->track : sv;
1870 ref = via;
1871 while (ref->track)
1872 ref = ref->track;
1873
Emeric Brun52a91d32017-08-31 14:41:55 +02001874 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001875 if ((ref->check.state & CHK_ST_ENABLED) &&
1876 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1877 state = SRV_STATS_STATE_UP_GOING_DOWN;
1878 } else {
1879 state = SRV_STATS_STATE_UP;
1880 }
1881
Emeric Brun52a91d32017-08-31 14:41:55 +02001882 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001883 if (ref->agent.state & CHK_ST_ENABLED)
1884 state = SRV_STATS_STATE_DRAIN_AGENT;
1885 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1886 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1887 else
1888 state = SRV_STATS_STATE_DRAIN;
1889 }
1890
1891 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1892 state = SRV_STATS_STATE_NO_CHECK;
1893 }
1894 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001895 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001896 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1897 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1898 state = SRV_STATS_STATE_NOLB;
1899 } else {
1900 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1901 }
1902 }
1903 else { /* stopped */
1904 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1905 state = SRV_STATS_STATE_DOWN_AGENT;
1906 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1907 state = SRV_STATS_STATE_DOWN; /* DOWN */
1908 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1909 state = SRV_STATS_STATE_GOING_UP;
1910 } else {
1911 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1912 }
1913 }
1914
1915 chunk_reset(out);
1916
1917 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1918 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1919 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1920 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1921 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1922 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1923 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1924
1925 if (sv->maxconn)
1926 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1927
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001928 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1929 if (sv->max_idle_conns != -1)
1930 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1931
William Lallemand74c24fb2016-11-21 17:18:36 +01001932 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1933 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1934 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001935 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001936 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1937 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1938 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1939 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001940 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001941 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001942 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1943 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001944
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001945 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1946 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1947 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001948 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001949
William Lallemand74c24fb2016-11-21 17:18:36 +01001950 /* status */
1951 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001952 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001953 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001954 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001955 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001956 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001957 chunk_appendf(out, "MAINT");
1958 else
1959 chunk_appendf(out,
1960 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001961 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1962 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001963
1964 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1965 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001966 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
Willy Tarreaubd715102020-10-23 22:44:30 +02001967 stats[ST_F_UWEIGHT] = mkf_u32(FN_AVG, sv->uweight);
William Lallemand74c24fb2016-11-21 17:18:36 +01001968 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1969 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1970
1971 /* check failures: unique, fatal; last change, total downtime */
1972 if (sv->check.state & CHK_ST_ENABLED) {
1973 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1974 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1975 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1976 }
1977
1978 if (sv->maxqueue)
1979 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1980
1981 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1982 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1983 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1984
Emeric Brun52a91d32017-08-31 14:41:55 +02001985 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001986 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1987
1988 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1989
1990 if (sv->track) {
1991 char *fld_track = chunk_newstr(out);
1992
1993 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1994 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1995 }
1996
1997 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1998 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1999 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
2000
2001 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2002 const char *fld_chksts;
2003
2004 fld_chksts = chunk_newstr(out);
2005 chunk_strcat(out, "* "); // for check in progress
2006 chunk_strcat(out, get_check_status_info(sv->check.status));
2007 if (!(sv->check.state & CHK_ST_INPROGRESS))
2008 fld_chksts += 2; // skip "* "
2009 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
2010
2011 if (sv->check.status >= HCHK_STATUS_L57DATA)
2012 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
2013
2014 if (sv->check.status >= HCHK_STATUS_CHECKED)
2015 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
2016
2017 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2018 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
2019 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2020 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2021 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2022 }
2023
2024 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2025 const char *fld_chksts;
2026
2027 fld_chksts = chunk_newstr(out);
2028 chunk_strcat(out, "* "); // for check in progress
2029 chunk_strcat(out, get_check_status_info(sv->agent.status));
2030 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2031 fld_chksts += 2; // skip "* "
2032 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
2033
2034 if (sv->agent.status >= HCHK_STATUS_L57DATA)
2035 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
2036
2037 if (sv->agent.status >= HCHK_STATUS_CHECKED)
2038 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
2039
2040 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2041 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
2042 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2043 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2044 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2045 }
2046
2047 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2048 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02002049 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01002050 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2051 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2052 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2053 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2054 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2055 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2056 }
2057
2058 if (ref->observe)
2059 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2060
2061 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2062 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2063 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
2064
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002065 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2066 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2067 srv_samples_window = srv_samples_counter;
2068
2069 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2070 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2071 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2072 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01002073
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002074 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
2075 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
2076 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
2077 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
2078
Willy Tarreau708c4162019-10-09 10:19:16 +02002079 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002080 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2081 case AF_INET:
2082 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01002083 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01002084 break;
2085 case AF_INET6:
2086 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01002087 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01002088 break;
2089 case AF_UNIX:
2090 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2091 break;
2092 case -1:
2093 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2094 chunk_strcat(out, strerror(errno));
2095 break;
2096 default: /* address family not supported */
2097 break;
2098 }
2099
2100 if (sv->cookie)
2101 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2102 }
2103
2104 return 1;
2105}
2106
2107/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02002108 * from stream interface <si>, and server state <state>. The caller is
2109 * responsible for clearing the trash if needed. Returns non-zero if it emits
2110 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002111 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002112static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002113{
2114 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002115 struct stats_module *mod;
2116 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2117 size_t stats_count = ST_F_TOTAL_FIELDS;
2118
2119 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002120
Willy Tarreau43241ff2019-10-09 11:27:51 +02002121 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002122 return 0;
2123
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002124 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2125 void *counters;
2126
2127 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2128 continue;
2129
2130 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2131 stats_count += mod->stats_count;
2132 continue;
2133 }
2134
2135 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2136 mod->fill_stats(counters, stats + stats_count);
2137 stats_count += mod->stats_count;
2138 }
2139
2140 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002141}
2142
2143/* Fill <stats> with the backend statistics. <stats> is
2144 * preallocated array of length <len>. The length of the array
2145 * must be at least ST_F_TOTAL_FIELDS. If this length is less
2146 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02002147 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01002148 */
2149int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
2150{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002151 long long be_samples_counter;
2152 unsigned int be_samples_window = TIME_STATS_SAMPLES;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002153 struct buffer *out = get_trash_chunk();
2154 const struct server *srv;
2155 int nbup, nbsrv;
Willy Tarreaubd715102020-10-23 22:44:30 +02002156 int totuw;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002157 char *fld;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002158
William Lallemand74c24fb2016-11-21 17:18:36 +01002159 if (len < ST_F_TOTAL_FIELDS)
2160 return 0;
2161
Willy Tarreaubd715102020-10-23 22:44:30 +02002162 totuw = 0;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002163 nbup = nbsrv = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002164 for (srv = px->srv; srv; srv = srv->next) {
2165 if (srv->cur_state != SRV_ST_STOPPED) {
2166 nbup++;
2167 if (srv_currently_usable(srv) &&
2168 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
2169 totuw += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002170 }
Willy Tarreaubd715102020-10-23 22:44:30 +02002171 nbsrv++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002172 }
2173
Willy Tarreaubd715102020-10-23 22:44:30 +02002174 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2175 if (!px->srv_act && px->lbprm.fbck)
2176 totuw = px->lbprm.fbck->uweight;
2177 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2178
William Lallemand74c24fb2016-11-21 17:18:36 +01002179 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2180 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2181 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2182 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
2183 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01002184 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01002185 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
2186 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2187 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2188 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2189 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2190 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2191 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2192 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2193 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2194 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
2195 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002196 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01002197 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01002198 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
2199 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002200
2201 fld = chunk_newstr(out);
2202 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2203 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2204 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2205
2206 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld);
William Lallemand74c24fb2016-11-21 17:18:36 +01002207 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
Willy Tarreaubd715102020-10-23 22:44:30 +02002208 stats[ST_F_UWEIGHT] = mkf_u32(FN_AVG, totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002209 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
2210 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
2211 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
2212 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2213 if (px->srv)
2214 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
2215
2216 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
2217 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2218 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
2219 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2220 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2221 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2222 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
2223
Willy Tarreau708c4162019-10-09 10:19:16 +02002224 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002225 if (px->cookie_name)
2226 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2227 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2228 }
2229
2230 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2231 if (px->mode == PR_MODE_HTTP) {
2232 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2233 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2234 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2235 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2236 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2237 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2238 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01002239 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2240 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01002241 }
2242
2243 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2244 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2245
2246 /* compression: in, out, bypassed, responses */
2247 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2248 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2249 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2250 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2251 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
2252
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002253 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2254 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2255 be_samples_window = be_samples_counter;
2256
2257 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2258 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2259 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2260 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 +01002261
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002262 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2263 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2264 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2265 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2266
William Lallemand74c24fb2016-11-21 17:18:36 +01002267 return 1;
2268}
2269
2270/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002271 * interface <si>. The caller is responsible for clearing the trash if needed.
2272 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002273 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002274static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002275{
2276 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002277 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2278 struct stats_module *mod;
2279 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002280
2281 if (!(px->cap & PR_CAP_BE))
2282 return 0;
2283
2284 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2285 return 0;
2286
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002287 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2288
Willy Tarreau43241ff2019-10-09 11:27:51 +02002289 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002290 return 0;
2291
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002292 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2293 struct extra_counters *counters;
2294
2295 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2296 continue;
2297
2298 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2299 stats_count += mod->stats_count;
2300 continue;
2301 }
2302
2303 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2304 mod->fill_stats(counters, stats + stats_count);
2305 stats_count += mod->stats_count;
2306 }
2307
2308 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002309}
2310
2311/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2312 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2313 * for clearing the trash if needed.
2314 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002315static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002316{
2317 struct appctx *appctx = __objt_appctx(si->end);
2318 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002319 struct stats_module *mod;
2320 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002321
2322 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2323 /* A form to enable/disable this proxy servers */
2324
2325 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2326 scope_txt[0] = 0;
2327 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002328 const char *scope_ptr = stats_scope_ptr(appctx, si);
2329
William Lallemand74c24fb2016-11-21 17:18:36 +01002330 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002331 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002332 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2333 }
2334
2335 chunk_appendf(&trash,
2336 "<form method=\"post\">");
2337 }
2338
2339 /* print a new table */
2340 chunk_appendf(&trash,
2341 "<table class=\"tbl\" width=\"100%%\">\n"
2342 "<tr class=\"titre\">"
2343 "<th class=\"pxname\" width=\"10%%\">");
2344
2345 chunk_appendf(&trash,
2346 "<a name=\"%s\"></a>%s"
2347 "<a class=px href=\"#%s\">%s</a>",
2348 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002349 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002350 px->id, px->id);
2351
Willy Tarreau676c29e2019-10-09 10:50:01 +02002352 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002353 /* cap, mode, id */
2354 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2355 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2356 px->uuid);
2357 chunk_appendf(&trash, "</div>");
2358 }
2359
2360 chunk_appendf(&trash,
2361 "%s</th>"
2362 "<th class=\"%s\" width=\"90%%\">%s</th>"
2363 "</tr>\n"
2364 "</table>\n"
2365 "<table class=\"tbl\" width=\"100%%\">\n"
2366 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002367 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002368 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2369
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002370 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002371 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002372 if ((px->cap & PR_CAP_BE) && px->srv)
2373 chunk_appendf(&trash,
2374 "<th rowspan=2 width=1><input type=\"checkbox\" "
2375 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2376 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2377 px->id,
2378 px->id);
2379 else
2380 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002381 }
2382
2383 chunk_appendf(&trash,
2384 "<th rowspan=2></th>"
2385 "<th colspan=3>Queue</th>"
2386 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2387 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2388 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002389 "<th colspan=9>Server</th>");
2390
2391 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2392 // calculate the count of module for colspan attribute
2393 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2394 ++stats_module_len;
2395 }
2396 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2397 stats_module_len);
2398 }
2399
2400 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002401 "</tr>\n"
2402 "<tr class=\"titre\">"
2403 "<th>Cur</th><th>Max</th><th>Limit</th>"
2404 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2405 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2406 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2407 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2408 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2409 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002410 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002411
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002412 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2413 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2414 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2415 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002416 }
2417
2418 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002419}
2420
2421/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2422 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2423 */
2424static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2425{
2426 struct appctx *appctx = __objt_appctx(si->end);
2427 chunk_appendf(&trash, "</table>");
2428
2429 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2430 /* close the form used to enable/disable this proxy servers */
2431 chunk_appendf(&trash,
2432 "Choose the action to perform on the checked servers : "
2433 "<select name=action>"
2434 "<option value=\"\"></option>"
2435 "<option value=\"ready\">Set state to READY</option>"
2436 "<option value=\"drain\">Set state to DRAIN</option>"
2437 "<option value=\"maint\">Set state to MAINT</option>"
2438 "<option value=\"dhlth\">Health: disable checks</option>"
2439 "<option value=\"ehlth\">Health: enable checks</option>"
2440 "<option value=\"hrunn\">Health: force UP</option>"
2441 "<option value=\"hnolb\">Health: force NOLB</option>"
2442 "<option value=\"hdown\">Health: force DOWN</option>"
2443 "<option value=\"dagent\">Agent: disable checks</option>"
2444 "<option value=\"eagent\">Agent: enable checks</option>"
2445 "<option value=\"arunn\">Agent: force UP</option>"
2446 "<option value=\"adown\">Agent: force DOWN</option>"
2447 "<option value=\"shutdown\">Kill Sessions</option>"
2448 "</select>"
2449 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2450 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2451 "</form>",
2452 px->uuid);
2453 }
2454
2455 chunk_appendf(&trash, "<p>\n");
2456}
2457
2458/*
2459 * Dumps statistics for a proxy. The output is sent to the stream interface's
2460 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2461 * buffer space, or non-zero if everything completed. This function is used
2462 * both by the CLI and the HTTP entry points, and is able to dump the output
2463 * in HTML or CSV formats. If the later, <uri> must be NULL.
2464 */
Christopher Fauletef779222018-10-31 08:47:01 +01002465int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2466 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002467{
2468 struct appctx *appctx = __objt_appctx(si->end);
2469 struct stream *s = si_strm(si);
2470 struct channel *rep = si_ic(si);
2471 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2472 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002473
2474 chunk_reset(&trash);
2475
2476 switch (appctx->ctx.stats.px_st) {
2477 case STAT_PX_ST_INIT:
2478 /* we are on a new proxy */
2479 if (uri && uri->scope) {
2480 /* we have a limited scope, we have to check the proxy name */
2481 struct stat_scope *scope;
2482 int len;
2483
2484 len = strlen(px->id);
2485 scope = uri->scope;
2486
2487 while (scope) {
2488 /* match exact proxy name */
2489 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2490 break;
2491
2492 /* match '.' which means 'self' proxy */
2493 if (!strcmp(scope->px_id, ".") && px == s->be)
2494 break;
2495 scope = scope->next;
2496 }
2497
2498 /* proxy name not found : don't dump anything */
2499 if (scope == NULL)
2500 return 1;
2501 }
2502
2503 /* if the user has requested a limited output and the proxy
2504 * name does not match, skip it.
2505 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002506 if (appctx->ctx.stats.scope_len) {
2507 const char *scope_ptr = stats_scope_ptr(appctx, si);
2508
2509 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2510 return 1;
2511 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002512
2513 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2514 (appctx->ctx.stats.iid != -1) &&
2515 (px->uuid != appctx->ctx.stats.iid))
2516 return 1;
2517
2518 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2519 /* fall through */
2520
2521 case STAT_PX_ST_TH:
2522 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002523 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002524 if (!stats_putchk(rep, htx, &trash))
2525 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002526 }
2527
2528 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2529 /* fall through */
2530
2531 case STAT_PX_ST_FE:
2532 /* print the frontend */
2533 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002534 if (!stats_putchk(rep, htx, &trash))
2535 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002536 }
2537
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002538 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01002539 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2540 /* fall through */
2541
2542 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002543 /* obj2 points to listeners list as initialized above */
2544 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002545 if (htx) {
2546 if (htx_almost_full(htx))
2547 goto full;
2548 }
2549 else {
2550 if (buffer_almost_full(&rep->buf))
2551 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002552 }
2553
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002554 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01002555 if (!l->counters)
2556 continue;
2557
2558 if (appctx->ctx.stats.flags & STAT_BOUND) {
2559 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2560 break;
2561
2562 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2563 continue;
2564 }
2565
2566 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002567 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002568 if (!stats_putchk(rep, htx, &trash))
2569 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002570 }
2571 }
2572
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002573 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01002574 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2575 /* fall through */
2576
2577 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002578 /* obj2 points to servers list as initialized above */
2579 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002580 if (htx) {
2581 if (htx_almost_full(htx))
2582 goto full;
2583 }
2584 else {
2585 if (buffer_almost_full(&rep->buf))
2586 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002587 }
2588
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002589 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01002590
2591 if (appctx->ctx.stats.flags & STAT_BOUND) {
2592 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2593 break;
2594
2595 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2596 continue;
2597 }
2598
Willy Tarreau3e320362020-10-23 17:28:57 +02002599 /* do not report disabled servers */
2600 if (appctx->ctx.stats.flags & STAT_HIDE_MAINT &&
2601 sv->cur_admin & SRV_ADMF_MAINT) {
2602 continue;
2603 }
2604
William Lallemand74c24fb2016-11-21 17:18:36 +01002605 svs = sv;
2606 while (svs->track)
2607 svs = svs->track;
2608
2609 /* do not report servers which are DOWN and not changing state */
2610 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002611 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2612 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002613 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2614 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2615 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2616 continue;
2617 }
2618
Willy Tarreau43241ff2019-10-09 11:27:51 +02002619 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002620 if (!stats_putchk(rep, htx, &trash))
2621 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002622 }
2623 } /* for sv */
2624
2625 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2626 /* fall through */
2627
2628 case STAT_PX_ST_BE:
2629 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002630 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002631 if (!stats_putchk(rep, htx, &trash))
2632 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002633 }
2634
2635 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2636 /* fall through */
2637
2638 case STAT_PX_ST_END:
2639 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2640 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002641 if (!stats_putchk(rep, htx, &trash))
2642 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002643 }
2644
2645 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2646 /* fall through */
2647
2648 case STAT_PX_ST_FIN:
2649 return 1;
2650
2651 default:
2652 /* unknown state, we should put an abort() here ! */
2653 return 1;
2654 }
Christopher Fauletef779222018-10-31 08:47:01 +01002655
2656 full:
2657 si_rx_room_blk(si);
2658 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002659}
2660
2661/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2662 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2663 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002664static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002665{
2666 /* WARNING! This must fit in the first buffer !!! */
2667 chunk_appendf(&trash,
2668 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2669 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2670 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03002671 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002672 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2673 "<style type=\"text/css\"><!--\n"
2674 "body {"
2675 " font-family: arial, helvetica, sans-serif;"
2676 " font-size: 12px;"
2677 " font-weight: normal;"
2678 " color: black;"
2679 " background: white;"
2680 "}\n"
2681 "th,td {"
2682 " font-size: 10px;"
2683 "}\n"
2684 "h1 {"
2685 " font-size: x-large;"
2686 " margin-bottom: 0.5em;"
2687 "}\n"
2688 "h2 {"
2689 " font-family: helvetica, arial;"
2690 " font-size: x-large;"
2691 " font-weight: bold;"
2692 " font-style: italic;"
2693 " color: #6020a0;"
2694 " margin-top: 0em;"
2695 " margin-bottom: 0em;"
2696 "}\n"
2697 "h3 {"
2698 " font-family: helvetica, arial;"
2699 " font-size: 16px;"
2700 " font-weight: bold;"
2701 " color: #b00040;"
2702 " background: #e8e8d0;"
2703 " margin-top: 0em;"
2704 " margin-bottom: 0em;"
2705 "}\n"
2706 "li {"
2707 " margin-top: 0.25em;"
2708 " margin-right: 2em;"
2709 "}\n"
2710 ".hr {margin-top: 0.25em;"
2711 " border-color: black;"
2712 " border-bottom-style: solid;"
2713 "}\n"
2714 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2715 ".total {background: #20D0D0;color: #ffff80;}\n"
2716 ".frontend {background: #e8e8d0;}\n"
2717 ".socket {background: #d0d0d0;}\n"
2718 ".backend {background: #e8e8d0;}\n"
2719 ".active_down {background: #ff9090;}\n"
2720 ".active_going_up {background: #ffd020;}\n"
2721 ".active_going_down {background: #ffffa0;}\n"
2722 ".active_up {background: #c0ffc0;}\n"
2723 ".active_nolb {background: #20a0ff;}\n"
2724 ".active_draining {background: #20a0FF;}\n"
2725 ".active_no_check {background: #e0e0e0;}\n"
2726 ".backup_down {background: #ff9090;}\n"
2727 ".backup_going_up {background: #ff80ff;}\n"
2728 ".backup_going_down {background: #c060ff;}\n"
2729 ".backup_up {background: #b0d0ff;}\n"
2730 ".backup_nolb {background: #90b0e0;}\n"
2731 ".backup_draining {background: #cc9900;}\n"
2732 ".backup_no_check {background: #e0e0e0;}\n"
2733 ".maintain {background: #c07820;}\n"
2734 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2735 "\n"
2736 "a.px:link {color: #ffff40; text-decoration: none;}"
2737 "a.px:visited {color: #ffff40; text-decoration: none;}"
2738 "a.px:hover {color: #ffffff; text-decoration: none;}"
2739 "a.lfsb:link {color: #000000; text-decoration: none;}"
2740 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2741 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2742 "\n"
2743 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2744 "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"
2745 "table.tbl td.ac { text-align: center;}\n"
2746 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2747 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2748 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2749 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2750 "\n"
2751 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2752 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2753 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2754 "table.det { border-collapse: collapse; border-style: none; }\n"
2755 "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"
2756 "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"
2757 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2758 "div.tips {\n"
2759 " display:block;\n"
2760 " visibility:hidden;\n"
2761 " z-index:2147483647;\n"
2762 " position:absolute;\n"
2763 " padding:2px 4px 3px;\n"
2764 " background:#f0f060; color:#000000;\n"
2765 " border:1px solid #7040c0;\n"
2766 " white-space:nowrap;\n"
2767 " font-style:normal;font-size:11px;font-weight:normal;\n"
2768 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2769 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2770 "}\n"
2771 "u:hover div.tips {visibility:visible;}\n"
2772 "-->\n"
2773 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002774 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002775 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002776 );
2777}
2778
2779/* Dumps the HTML stats information block to the trash for and uses the state from
2780 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2781 * for clearing the trash if needed.
2782 */
2783static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2784{
2785 struct appctx *appctx = __objt_appctx(si->end);
2786 unsigned int up = (now.tv_sec - start_date.tv_sec);
2787 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002788 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002789 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2790
2791 /* Turn the bytes per second to bits per second and take care of the
2792 * usual ethernet overhead in order to help figure how far we are from
2793 * interface saturation since it's the only case which usually matters.
2794 * For this we count the total size of an Ethernet frame on the wire
2795 * including preamble and IFG (1538) for the largest TCP segment it
2796 * transports (1448 with TCP timestamps). This is not valid for smaller
2797 * packets (under-estimated), but it gives a reasonably accurate
2798 * estimation of how far we are from uplink saturation.
2799 */
2800 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002801
2802 /* WARNING! this has to fit the first packet too.
2803 * We are around 3.5 kB, add adding entries will
2804 * become tricky if we want to support 4kB buffers !
2805 */
2806 chunk_appendf(&trash,
2807 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2808 PRODUCT_NAME "%s</a></h1>\n"
2809 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2810 "<hr width=\"100%%\" class=\"hr\">\n"
2811 "<h3>&gt; General process information</h3>\n"
2812 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002813 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002814 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2815 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2816 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002817 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002818 "Running tasks: %d/%d; idle = %d %%<br>\n"
2819 "</td><td align=\"center\" nowrap>\n"
2820 "<table class=\"lgd\"><tr>\n"
2821 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2822 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2823 "</tr><tr>\n"
2824 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2825 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2826 "</tr><tr>\n"
2827 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2828 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2829 "</tr><tr>\n"
2830 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2831 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2832 "</tr><tr>\n"
2833 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2834 "</tr><tr>\n"
2835 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2836 "</tr></table>\n"
2837 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2838 "</td>"
2839 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2840 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2841 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002842 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2843 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2844 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2845 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2846 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002847 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002848 up / 86400, (up % 86400) / 3600,
2849 (up % 3600) / 60, (up % 60),
2850 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2851 global.rlimit_memmax ? " MB" : "",
2852 global.rlimit_nofile,
2853 global.maxsock, global.maxconn, global.maxpipes,
2854 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002855 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2856 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002857 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002858 );
2859
2860 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002861 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002862 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2863
2864 chunk_appendf(&trash,
2865 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2866 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2867 STAT_SCOPE_TXT_MAXLEN);
2868
2869 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2870 scope_txt[0] = 0;
2871 if (appctx->ctx.stats.scope_len) {
2872 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002873 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002874 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2875 }
2876
2877 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2878 chunk_appendf(&trash,
2879 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2880 uri->uri_prefix,
2881 "",
2882 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2883 scope_txt);
2884 else
2885 chunk_appendf(&trash,
2886 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2887 uri->uri_prefix,
2888 ";up",
2889 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2890 scope_txt);
2891
2892 if (uri->refresh > 0) {
2893 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2894 chunk_appendf(&trash,
2895 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2896 uri->uri_prefix,
2897 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2898 "",
2899 scope_txt);
2900 else
2901 chunk_appendf(&trash,
2902 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2903 uri->uri_prefix,
2904 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2905 ";norefresh",
2906 scope_txt);
2907 }
2908
2909 chunk_appendf(&trash,
2910 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2911 uri->uri_prefix,
2912 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2913 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2914 scope_txt);
2915
2916 chunk_appendf(&trash,
2917 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2918 uri->uri_prefix,
2919 (uri->refresh > 0) ? ";norefresh" : "",
2920 scope_txt);
2921
2922 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002923 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2924 uri->uri_prefix,
2925 (uri->refresh > 0) ? ";norefresh" : "",
2926 scope_txt, uri->uri_prefix);
2927
2928 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002929 "</ul></td>"
2930 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2931 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2932 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2933 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2934 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2935 "</ul>"
2936 "</td>"
2937 "</tr></table>\n"
2938 ""
2939 );
2940
2941 if (appctx->ctx.stats.st_code) {
2942 switch (appctx->ctx.stats.st_code) {
2943 case STAT_STATUS_DONE:
2944 chunk_appendf(&trash,
2945 "<p><div class=active_up>"
2946 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2947 "Action processed successfully."
2948 "</div>\n", uri->uri_prefix,
2949 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2950 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2951 scope_txt);
2952 break;
2953 case STAT_STATUS_NONE:
2954 chunk_appendf(&trash,
2955 "<p><div class=active_going_down>"
2956 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2957 "Nothing has changed."
2958 "</div>\n", uri->uri_prefix,
2959 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2960 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2961 scope_txt);
2962 break;
2963 case STAT_STATUS_PART:
2964 chunk_appendf(&trash,
2965 "<p><div class=active_going_down>"
2966 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2967 "Action partially processed.<br>"
2968 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2969 "</div>\n", uri->uri_prefix,
2970 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2971 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2972 scope_txt);
2973 break;
2974 case STAT_STATUS_ERRP:
2975 chunk_appendf(&trash,
2976 "<p><div class=active_down>"
2977 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2978 "Action not processed because of invalid parameters."
2979 "<ul>"
2980 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002981 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002982 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2983 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2984 "</ul>"
2985 "</div>\n", uri->uri_prefix,
2986 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2987 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2988 scope_txt);
2989 break;
2990 case STAT_STATUS_EXCD:
2991 chunk_appendf(&trash,
2992 "<p><div class=active_down>"
2993 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2994 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2995 "You should retry with less servers at a time.</b>"
2996 "</div>\n", uri->uri_prefix,
2997 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2998 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2999 scope_txt);
3000 break;
3001 case STAT_STATUS_DENY:
3002 chunk_appendf(&trash,
3003 "<p><div class=active_down>"
3004 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3005 "<b>Action denied.</b>"
3006 "</div>\n", uri->uri_prefix,
3007 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3008 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3009 scope_txt);
3010 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003011 case STAT_STATUS_IVAL:
3012 chunk_appendf(&trash,
3013 "<p><div class=active_down>"
3014 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3015 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3016 "</div>\n", uri->uri_prefix,
3017 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3018 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3019 scope_txt);
3020 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003021 default:
3022 chunk_appendf(&trash,
3023 "<p><div class=active_no_check>"
3024 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3025 "Unexpected result."
3026 "</div>\n", uri->uri_prefix,
3027 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3028 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3029 scope_txt);
3030 }
3031 chunk_appendf(&trash, "<p>\n");
3032 }
3033}
3034
3035/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3036 * for clearing the trash if needed.
3037 */
3038static void stats_dump_html_end()
3039{
3040 chunk_appendf(&trash, "</body></html>\n");
3041}
3042
Simon Horman05ee2132017-01-04 09:37:25 +01003043/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3044 * for clearing it if needed.
3045 */
3046static void stats_dump_json_header()
3047{
3048 chunk_strcat(&trash, "[");
3049}
3050
3051
3052/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3053 * for clearing the trash if needed.
3054 */
3055static void stats_dump_json_end()
3056{
3057 chunk_strcat(&trash, "]");
3058}
3059
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003060/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3061 * a pointer to the current server/listener.
3062 */
3063static int stats_dump_proxies(struct stream_interface *si,
3064 struct htx *htx,
3065 struct uri_auth *uri)
3066{
3067 struct appctx *appctx = __objt_appctx(si->end);
3068 struct channel *rep = si_ic(si);
3069 struct proxy *px;
3070
3071 /* dump proxies */
3072 while (appctx->ctx.stats.obj1) {
3073 if (htx) {
3074 if (htx_almost_full(htx))
3075 goto full;
3076 }
3077 else {
3078 if (buffer_almost_full(&rep->buf))
3079 goto full;
3080 }
3081
3082 px = appctx->ctx.stats.obj1;
3083 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02003084 if (!px->disabled && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003085 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
3086 return 0;
3087 }
3088
3089 appctx->ctx.stats.obj1 = px->next;
3090 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3091 }
3092
3093 return 1;
3094
3095 full:
3096 si_rx_room_blk(si);
3097 return 0;
3098}
3099
William Lallemand74c24fb2016-11-21 17:18:36 +01003100/* This function dumps statistics onto the stream interface's read buffer in
3101 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3102 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3103 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3104 * and the stream must be closed, or -1 in case of any error. This function is
3105 * used by both the CLI and the HTTP handlers.
3106 */
Christopher Fauletef779222018-10-31 08:47:01 +01003107static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
3108 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003109{
3110 struct appctx *appctx = __objt_appctx(si->end);
3111 struct channel *rep = si_ic(si);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003112 enum stats_domain domain = appctx->ctx.stats.domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003113
3114 chunk_reset(&trash);
3115
3116 switch (appctx->st2) {
3117 case STAT_ST_INIT:
3118 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3119 /* fall through */
3120
3121 case STAT_ST_HEAD:
3122 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003123 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003124 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3125 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003126 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003127 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003128 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003129 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003130
Christopher Fauletef779222018-10-31 08:47:01 +01003131 if (!stats_putchk(rep, htx, &trash))
3132 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003133
Christopher Faulet6338a082019-09-09 15:50:54 +02003134 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3135 appctx->st2 = STAT_ST_FIN;
3136 return 1;
3137 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003138 appctx->st2 = STAT_ST_INFO;
3139 /* fall through */
3140
3141 case STAT_ST_INFO:
3142 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3143 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003144 if (!stats_putchk(rep, htx, &trash))
3145 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003146 }
3147
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003148 if (domain == STATS_DOMAIN_PROXY)
3149 appctx->ctx.stats.obj1 = proxies_list;
3150
William Lallemand74c24fb2016-11-21 17:18:36 +01003151 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3152 appctx->st2 = STAT_ST_LIST;
3153 /* fall through */
3154
3155 case STAT_ST_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003156 switch (domain) {
3157 case STATS_DOMAIN_DNS:
3158 if (!stats_dump_dns(si, stat_l[domain],
3159 stat_count[domain],
3160 &stats_module_list[domain])) {
3161 return 0;
3162 }
3163 break;
3164
3165 case STATS_DOMAIN_PROXY:
3166 default:
3167 /* dump proxies */
3168 if (!stats_dump_proxies(si, htx, uri))
3169 return 0;
3170 break;
3171 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003172
3173 appctx->st2 = STAT_ST_END;
3174 /* fall through */
3175
3176 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003177 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3178 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3179 stats_dump_html_end();
3180 else
3181 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003182 if (!stats_putchk(rep, htx, &trash))
3183 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003184 }
3185
3186 appctx->st2 = STAT_ST_FIN;
3187 /* fall through */
3188
3189 case STAT_ST_FIN:
3190 return 1;
3191
3192 default:
3193 /* unknown state ! */
3194 appctx->st2 = STAT_ST_FIN;
3195 return -1;
3196 }
Christopher Fauletef779222018-10-31 08:47:01 +01003197
3198 full:
3199 si_rx_room_blk(si);
3200 return 0;
3201
William Lallemand74c24fb2016-11-21 17:18:36 +01003202}
3203
3204/* We reached the stats page through a POST request. The appctx is
3205 * expected to have already been allocated by the caller.
3206 * Parse the posted data and enable/disable servers if necessary.
3207 * Returns 1 if request was parsed or zero if it needs more data.
3208 */
3209static int stats_process_http_post(struct stream_interface *si)
3210{
3211 struct stream *s = si_strm(si);
3212 struct appctx *appctx = objt_appctx(si->end);
3213
3214 struct proxy *px = NULL;
3215 struct server *sv = NULL;
3216
3217 char key[LINESIZE];
3218 int action = ST_ADM_ACTION_NONE;
3219 int reprocess = 0;
3220
3221 int total_servers = 0;
3222 int altered_servers = 0;
3223
3224 char *first_param, *cur_param, *next_param, *end_params;
3225 char *st_cur_param = NULL;
3226 char *st_next_param = NULL;
3227
Christopher Fauleta3618372018-12-13 21:59:56 +01003228 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003229
Christopher Fauletb7f88902019-07-15 21:56:43 +02003230 struct htx *htx = htxbuf(&s->req.buf);
3231 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003232
Christopher Fauletb7f88902019-07-15 21:56:43 +02003233 /* we need more data */
3234 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3235 /* check if we can receive more */
3236 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3237 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3238 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003239 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003240 goto wait;
3241 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003242
Christopher Fauletb7f88902019-07-15 21:56:43 +02003243 /* The request was fully received. Copy data */
3244 blk = htx_get_head_blk(htx);
3245 while (blk) {
3246 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003247
Christopher Fauletb7f88902019-07-15 21:56:43 +02003248 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3249 break;
3250 if (type == HTX_BLK_DATA) {
3251 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003252
Christopher Fauletb7f88902019-07-15 21:56:43 +02003253 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003254 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3255 goto out;
3256 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003257 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003258 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003259 }
3260
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003261 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003262 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003263 cur_param = next_param = end_params;
3264 *end_params = '\0';
3265
3266 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3267
3268 /*
3269 * Parse the parameters in reverse order to only store the last value.
3270 * From the html form, the backend and the action are at the end.
3271 */
3272 while (cur_param > first_param) {
3273 char *value;
3274 int poffset, plen;
3275
3276 cur_param--;
3277
3278 if ((*cur_param == '&') || (cur_param == first_param)) {
3279 reprocess_servers:
3280 /* Parse the key */
3281 poffset = (cur_param != first_param ? 1 : 0);
3282 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3283 if ((plen > 0) && (plen <= sizeof(key))) {
3284 strncpy(key, cur_param + poffset, plen);
3285 key[plen - 1] = '\0';
3286 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003287 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003288 goto out;
3289 }
3290
3291 /* Parse the value */
3292 value = key;
3293 while (*value != '\0' && *value != '=') {
3294 value++;
3295 }
3296 if (*value == '=') {
3297 /* Ok, a value is found, we can mark the end of the key */
3298 *value++ = '\0';
3299 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003300 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003301 break;
3302
3303 /* Now we can check the key to see what to do */
3304 if (!px && (strcmp(key, "b") == 0)) {
3305 if ((px = proxy_be_by_name(value)) == NULL) {
3306 /* the backend name is unknown or ambiguous (duplicate names) */
3307 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3308 goto out;
3309 }
3310 }
3311 else if (!action && (strcmp(key, "action") == 0)) {
3312 if (strcmp(value, "ready") == 0) {
3313 action = ST_ADM_ACTION_READY;
3314 }
3315 else if (strcmp(value, "drain") == 0) {
3316 action = ST_ADM_ACTION_DRAIN;
3317 }
3318 else if (strcmp(value, "maint") == 0) {
3319 action = ST_ADM_ACTION_MAINT;
3320 }
3321 else if (strcmp(value, "shutdown") == 0) {
3322 action = ST_ADM_ACTION_SHUTDOWN;
3323 }
3324 else if (strcmp(value, "dhlth") == 0) {
3325 action = ST_ADM_ACTION_DHLTH;
3326 }
3327 else if (strcmp(value, "ehlth") == 0) {
3328 action = ST_ADM_ACTION_EHLTH;
3329 }
3330 else if (strcmp(value, "hrunn") == 0) {
3331 action = ST_ADM_ACTION_HRUNN;
3332 }
3333 else if (strcmp(value, "hnolb") == 0) {
3334 action = ST_ADM_ACTION_HNOLB;
3335 }
3336 else if (strcmp(value, "hdown") == 0) {
3337 action = ST_ADM_ACTION_HDOWN;
3338 }
3339 else if (strcmp(value, "dagent") == 0) {
3340 action = ST_ADM_ACTION_DAGENT;
3341 }
3342 else if (strcmp(value, "eagent") == 0) {
3343 action = ST_ADM_ACTION_EAGENT;
3344 }
3345 else if (strcmp(value, "arunn") == 0) {
3346 action = ST_ADM_ACTION_ARUNN;
3347 }
3348 else if (strcmp(value, "adown") == 0) {
3349 action = ST_ADM_ACTION_ADOWN;
3350 }
3351 /* else these are the old supported methods */
3352 else if (strcmp(value, "disable") == 0) {
3353 action = ST_ADM_ACTION_DISABLE;
3354 }
3355 else if (strcmp(value, "enable") == 0) {
3356 action = ST_ADM_ACTION_ENABLE;
3357 }
3358 else if (strcmp(value, "stop") == 0) {
3359 action = ST_ADM_ACTION_STOP;
3360 }
3361 else if (strcmp(value, "start") == 0) {
3362 action = ST_ADM_ACTION_START;
3363 }
3364 else {
3365 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3366 goto out;
3367 }
3368 }
3369 else if (strcmp(key, "s") == 0) {
3370 if (!(px && action)) {
3371 /*
3372 * Indicates that we'll need to reprocess the parameters
3373 * as soon as backend and action are known
3374 */
3375 if (!reprocess) {
3376 st_cur_param = cur_param;
3377 st_next_param = next_param;
3378 }
3379 reprocess = 1;
3380 }
3381 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003382 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003383 switch (action) {
3384 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003385 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003386 altered_servers++;
3387 total_servers++;
3388 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3389 }
3390 break;
3391 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003392 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003393 altered_servers++;
3394 total_servers++;
3395 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3396 }
3397 break;
3398 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003399 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003400 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3401 altered_servers++;
3402 total_servers++;
3403 }
3404 break;
3405 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003406 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003407 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3408 altered_servers++;
3409 total_servers++;
3410 }
3411 break;
3412 case ST_ADM_ACTION_DHLTH:
3413 if (sv->check.state & CHK_ST_CONFIGURED) {
3414 sv->check.state &= ~CHK_ST_ENABLED;
3415 altered_servers++;
3416 total_servers++;
3417 }
3418 break;
3419 case ST_ADM_ACTION_EHLTH:
3420 if (sv->check.state & CHK_ST_CONFIGURED) {
3421 sv->check.state |= CHK_ST_ENABLED;
3422 altered_servers++;
3423 total_servers++;
3424 }
3425 break;
3426 case ST_ADM_ACTION_HRUNN:
3427 if (!(sv->track)) {
3428 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003429 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003430 altered_servers++;
3431 total_servers++;
3432 }
3433 break;
3434 case ST_ADM_ACTION_HNOLB:
3435 if (!(sv->track)) {
3436 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003437 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003438 altered_servers++;
3439 total_servers++;
3440 }
3441 break;
3442 case ST_ADM_ACTION_HDOWN:
3443 if (!(sv->track)) {
3444 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003445 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003446 altered_servers++;
3447 total_servers++;
3448 }
3449 break;
3450 case ST_ADM_ACTION_DAGENT:
3451 if (sv->agent.state & CHK_ST_CONFIGURED) {
3452 sv->agent.state &= ~CHK_ST_ENABLED;
3453 altered_servers++;
3454 total_servers++;
3455 }
3456 break;
3457 case ST_ADM_ACTION_EAGENT:
3458 if (sv->agent.state & CHK_ST_CONFIGURED) {
3459 sv->agent.state |= CHK_ST_ENABLED;
3460 altered_servers++;
3461 total_servers++;
3462 }
3463 break;
3464 case ST_ADM_ACTION_ARUNN:
3465 if (sv->agent.state & CHK_ST_ENABLED) {
3466 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003467 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003468 altered_servers++;
3469 total_servers++;
3470 }
3471 break;
3472 case ST_ADM_ACTION_ADOWN:
3473 if (sv->agent.state & CHK_ST_ENABLED) {
3474 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003475 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003476 altered_servers++;
3477 total_servers++;
3478 }
3479 break;
3480 case ST_ADM_ACTION_READY:
3481 srv_adm_set_ready(sv);
3482 altered_servers++;
3483 total_servers++;
3484 break;
3485 case ST_ADM_ACTION_DRAIN:
3486 srv_adm_set_drain(sv);
3487 altered_servers++;
3488 total_servers++;
3489 break;
3490 case ST_ADM_ACTION_MAINT:
3491 srv_adm_set_maint(sv);
3492 altered_servers++;
3493 total_servers++;
3494 break;
3495 case ST_ADM_ACTION_SHUTDOWN:
Willy Tarreauc3914d42020-09-24 08:39:22 +02003496 if (!px->disabled) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003497 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003498 altered_servers++;
3499 total_servers++;
3500 }
3501 break;
3502 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003503 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003504 } else {
3505 /* the server name is unknown or ambiguous (duplicate names) */
3506 total_servers++;
3507 }
3508 }
3509 if (reprocess && px && action) {
3510 /* Now, we know the backend and the action chosen by the user.
3511 * We can safely restart from the first server parameter
3512 * to reprocess them
3513 */
3514 cur_param = st_cur_param;
3515 next_param = st_next_param;
3516 reprocess = 0;
3517 goto reprocess_servers;
3518 }
3519
3520 next_param = cur_param;
3521 }
3522 }
3523
3524 if (total_servers == 0) {
3525 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3526 }
3527 else if (altered_servers == 0) {
3528 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3529 }
3530 else if (altered_servers == total_servers) {
3531 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3532 }
3533 else {
3534 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3535 }
3536 out:
3537 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003538 wait:
3539 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3540 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003541}
3542
3543
Christopher Fauletb7f88902019-07-15 21:56:43 +02003544static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003545{
3546 struct stream *s = si_strm(si);
3547 struct uri_auth *uri = s->be->uri_auth;
3548 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003549 struct htx_sl *sl;
3550 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003551
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003552 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);
3553 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3554 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003555 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003556 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003557
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003558 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003559 goto full;
3560 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3561 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3562 goto full;
3563 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003564 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3565 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3566 goto full;
3567 }
Christopher Fauletef779222018-10-31 08:47:01 +01003568 else {
3569 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3570 goto full;
3571 }
3572
3573 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3574 const char *refresh = U2A(uri->refresh);
3575 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3576 goto full;
3577 }
3578
3579 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3580 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3581 goto full;
3582 }
3583
3584 if (!htx_add_endof(htx, HTX_BLK_EOH))
3585 goto full;
3586
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003587 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003588 return 1;
3589
3590 full:
3591 htx_reset(htx);
3592 si_rx_room_blk(si);
3593 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003594}
3595
Christopher Fauletef779222018-10-31 08:47:01 +01003596
Christopher Fauletb7f88902019-07-15 21:56:43 +02003597static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003598{
3599 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3600 struct stream *s = si_strm(si);
3601 struct uri_auth *uri = s->be->uri_auth;
3602 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003603 struct htx_sl *sl;
3604 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003605
3606 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3607 scope_txt[0] = 0;
3608 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003609 const char *scope_ptr = stats_scope_ptr(appctx, si);
3610
Christopher Fauletef779222018-10-31 08:47:01 +01003611 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003612 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003613 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3614 }
3615
3616 /* We don't want to land on the posted stats page because a refresh will
3617 * repost the data. We don't want this to happen on accident so we redirect
3618 * the browse to the stats page with a GET.
3619 */
3620 chunk_printf(&trash, "%s;st=%s%s%s%s",
3621 uri->uri_prefix,
3622 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3623 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3624 stat_status_codes[appctx->ctx.stats.st_code]) ?
3625 stat_status_codes[appctx->ctx.stats.st_code] :
3626 stat_status_codes[STAT_STATUS_UNKN],
3627 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3628 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3629 scope_txt);
3630
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003631 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3632 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3633 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003634 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003635 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003636
3637 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003638 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3639 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3640 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3641 goto full;
3642
3643 if (!htx_add_endof(htx, HTX_BLK_EOH))
3644 goto full;
3645
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003646 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003647 return 1;
3648
3649full:
3650 htx_reset(htx);
3651 si_rx_room_blk(si);
3652 return 0;
3653}
William Lallemand74c24fb2016-11-21 17:18:36 +01003654
Simon Horman05ee2132017-01-04 09:37:25 +01003655
William Lallemand74c24fb2016-11-21 17:18:36 +01003656/* This I/O handler runs as an applet embedded in a stream interface. It is
3657 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3658 * appctx->st0 contains the operation in progress (dump, done). The handler
3659 * automatically unregisters itself once transfer is complete.
3660 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003661static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003662{
3663 struct stream_interface *si = appctx->owner;
3664 struct stream *s = si_strm(si);
3665 struct channel *req = si_oc(si);
3666 struct channel *res = si_ic(si);
3667 struct htx *req_htx, *res_htx;
3668
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003669 /* only proxy stats are available via http */
3670 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3671
Christopher Fauletef779222018-10-31 08:47:01 +01003672 res_htx = htx_from_buf(&res->buf);
3673
3674 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3675 goto out;
3676
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003677 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003678 if (!b_size(&res->buf)) {
3679 si_rx_room_blk(si);
3680 goto out;
3681 }
3682
3683 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003684 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3685 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003686
3687 /* all states are processed in sequence */
3688 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003689 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003690 if (s->txn->meth == HTTP_METH_HEAD)
3691 appctx->st0 = STAT_HTTP_DONE;
3692 else
3693 appctx->st0 = STAT_HTTP_DUMP;
3694 }
3695 }
3696
3697 if (appctx->st0 == STAT_HTTP_DUMP) {
3698 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3699 appctx->st0 = STAT_HTTP_DONE;
3700 }
3701
3702 if (appctx->st0 == STAT_HTTP_POST) {
3703 if (stats_process_http_post(si))
3704 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003705 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003706 appctx->st0 = STAT_HTTP_DONE;
3707 }
3708
3709 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003710 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003711 appctx->st0 = STAT_HTTP_DONE;
3712 }
3713
3714 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003715 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003716 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003717 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3718 si_rx_room_blk(si);
3719 goto out;
3720 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003721 channel_add_input(&s->res, 1);
3722 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003723 }
3724
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003725 if (appctx->st0 == STAT_HTTP_END) {
3726 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003727 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003728 si_shutr(si);
3729 }
3730
3731 /* eat the whole request */
3732 if (co_data(req)) {
3733 req_htx = htx_from_buf(&req->buf);
3734 co_htx_skip(req, req_htx, co_data(req));
3735 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003736 }
3737 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003738
Christopher Fauletef779222018-10-31 08:47:01 +01003739 out:
3740 /* we have left the request in the buffer for the case where we
3741 * process a POST, and this automatically re-enables activity on
3742 * read. It's better to indicate that we want to stop reading when
3743 * we're sending, so that we know there's at most one direction
3744 * deciding to wake the applet up. It saves it from looping when
3745 * emitting large blocks into small TCP windows.
3746 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003747 htx_to_buf(res_htx, &res->buf);
3748 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003749 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003750}
3751
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003752/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003753static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003754 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003755{
3756 int field;
3757
3758 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3759 if (!field_format(info, field))
3760 continue;
3761
Willy Tarreaueaa55372019-10-09 07:39:11 +02003762 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003763 return 0;
3764 if (!stats_emit_raw_data_field(out, &info[field]))
3765 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003766 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3767 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003768 if (!chunk_strcat(out, "\n"))
3769 return 0;
3770 }
3771 return 1;
3772}
3773
3774/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003775static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003776 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003777{
3778 int field;
3779
3780 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3781 if (!field_format(info, field))
3782 continue;
3783
Willy Tarreaueaa55372019-10-09 07:39:11 +02003784 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 +01003785 return 0;
3786 if (!stats_emit_field_tags(out, &info[field], ':'))
3787 return 0;
3788 if (!stats_emit_typed_data_field(out, &info[field]))
3789 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003790 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3791 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003792 if (!chunk_strcat(out, "\n"))
3793 return 0;
3794 }
3795 return 1;
3796}
3797
3798/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003799 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003800 * INF_TOTAL_FIELDS. If this length is less then this value, the
3801 * function returns 0, otherwise, it returns 1.
3802 */
3803int stats_fill_info(struct field *info, int len)
3804{
3805 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003806 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003807
3808#ifdef USE_OPENSSL
3809 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3810 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3811 int ssl_reuse = 0;
3812
3813 if (ssl_key_rate < ssl_sess_rate) {
3814 /* count the ssl reuse ratio and avoid overflows in both directions */
3815 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3816 }
3817#endif
3818
3819 if (len < INF_TOTAL_FIELDS)
3820 return 0;
3821
3822 chunk_reset(out);
3823 memset(info, 0, sizeof(*info) * len);
3824
3825 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003826 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3827 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003828
Yves Lafon95317282018-02-26 11:10:37 +01003829 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003830 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3831 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3832 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3833
3834 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3835 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3836
3837 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3838 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3839 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3840 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3841 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3842 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3843 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3844 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3845 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3846 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3847 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3848 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3849#ifdef USE_OPENSSL
3850 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3851 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3852 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3853#endif
3854 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3855 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3856 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3857 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3858 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3859 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3860 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3861 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3862 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3863
3864#ifdef USE_OPENSSL
3865 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3866 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3867 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3868 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3869 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3870 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3871 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3872 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3873 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3874 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3875#endif
3876 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3877 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3878 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3879#ifdef USE_ZLIB
3880 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3881 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3882#endif
3883 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003884 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003885 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003886 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3887 if (global.desc)
3888 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003889 info[INF_STOPPING] = mkf_u32(0, stopping);
3890 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003891 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003892 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003893 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003894 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003895 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003896 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003897 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003898 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003899 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003900 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 +02003901 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003902 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003903
3904 return 1;
3905}
3906
3907/* This function dumps information onto the stream interface's read buffer.
3908 * It returns 0 as long as it does not complete, non-zero upon completion.
3909 * No state is used.
3910 */
3911static int stats_dump_info_to_buffer(struct stream_interface *si)
3912{
3913 struct appctx *appctx = __objt_appctx(si->end);
3914
3915 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3916 return 0;
3917
3918 chunk_reset(&trash);
3919
3920 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003921 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003922 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003923 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003924 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003925 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003926
Willy Tarreau06d80a92017-10-19 14:32:15 +02003927 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003928 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003929 return 0;
3930 }
3931
3932 return 1;
3933}
3934
Simon Horman6f6bb382017-01-04 09:37:26 +01003935/* This function dumps the schema onto the stream interface's read buffer.
3936 * It returns 0 as long as it does not complete, non-zero upon completion.
3937 * No state is used.
3938 *
3939 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3940 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3941 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003942static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003943{
3944
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003945 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003946
3947 chunk_strcat(out,
3948 "{"
3949 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3950 "\"oneOf\":["
3951 "{"
3952 "\"title\":\"Info\","
3953 "\"type\":\"array\","
3954 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003955 "\"title\":\"InfoItem\","
3956 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003957 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003958 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3959 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3960 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3961 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3962 "},"
3963 "\"required\":[\"field\",\"processNum\",\"tags\","
3964 "\"value\"]"
3965 "}"
3966 "},"
3967 "{"
3968 "\"title\":\"Stat\","
3969 "\"type\":\"array\","
3970 "\"items\":{"
3971 "\"title\":\"InfoItem\","
3972 "\"type\":\"object\","
3973 "\"properties\":{"
3974 "\"objType\":{"
3975 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3976 "\"Server\",\"Unknown\"]"
3977 "},"
3978 "\"proxyId\":{"
3979 "\"type\":\"integer\","
3980 "\"minimum\":0"
3981 "},"
3982 "\"id\":{"
3983 "\"type\":\"integer\","
3984 "\"minimum\":0"
3985 "},"
3986 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3987 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3988 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3989 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3990 "},"
3991 "\"required\":[\"objType\",\"proxyId\",\"id\","
3992 "\"field\",\"processNum\",\"tags\","
3993 "\"value\"]"
3994 "}"
3995 "},"
3996 "{"
3997 "\"title\":\"Error\","
3998 "\"type\":\"object\","
3999 "\"properties\":{"
4000 "\"errorStr\":{"
4001 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004002 "}"
4003 "},"
4004 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004005 "}"
4006 "],"
4007 "\"definitions\":{"
4008 "\"field\":{"
4009 "\"type\":\"object\","
4010 "\"pos\":{"
4011 "\"type\":\"integer\","
4012 "\"minimum\":0"
4013 "},"
4014 "\"name\":{"
4015 "\"type\":\"string\""
4016 "},"
4017 "\"required\":[\"pos\",\"name\"]"
4018 "},"
4019 "\"processNum\":{"
4020 "\"type\":\"integer\","
4021 "\"minimum\":1"
4022 "},"
4023 "\"tags\":{"
4024 "\"type\":\"object\","
4025 "\"origin\":{"
4026 "\"type\":\"string\","
4027 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4028 "\"Config\",\"Product\",\"Unknown\"]"
4029 "},"
4030 "\"nature\":{"
4031 "\"type\":\"string\","
4032 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4033 "\"Rate\",\"Counter\",\"Duration\","
4034 "\"Age\",\"Time\",\"Name\",\"Output\","
4035 "\"Avg\", \"Unknown\"]"
4036 "},"
4037 "\"scope\":{"
4038 "\"type\":\"string\","
4039 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4040 "\"System\",\"Unknown\"]"
4041 "},"
4042 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4043 "},"
4044 "\"typedValue\":{"
4045 "\"type\":\"object\","
4046 "\"oneOf\":["
4047 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4048 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4049 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4050 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4051 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4052 "],"
4053 "\"definitions\":{"
4054 "\"s32Value\":{"
4055 "\"properties\":{"
4056 "\"type\":{"
4057 "\"type\":\"string\","
4058 "\"enum\":[\"s32\"]"
4059 "},"
4060 "\"value\":{"
4061 "\"type\":\"integer\","
4062 "\"minimum\":-2147483648,"
4063 "\"maximum\":2147483647"
4064 "}"
4065 "},"
4066 "\"required\":[\"type\",\"value\"]"
4067 "},"
4068 "\"s64Value\":{"
4069 "\"properties\":{"
4070 "\"type\":{"
4071 "\"type\":\"string\","
4072 "\"enum\":[\"s64\"]"
4073 "},"
4074 "\"value\":{"
4075 "\"type\":\"integer\","
4076 "\"minimum\":-9007199254740991,"
4077 "\"maximum\":9007199254740991"
4078 "}"
4079 "},"
4080 "\"required\":[\"type\",\"value\"]"
4081 "},"
4082 "\"u32Value\":{"
4083 "\"properties\":{"
4084 "\"type\":{"
4085 "\"type\":\"string\","
4086 "\"enum\":[\"u32\"]"
4087 "},"
4088 "\"value\":{"
4089 "\"type\":\"integer\","
4090 "\"minimum\":0,"
4091 "\"maximum\":4294967295"
4092 "}"
4093 "},"
4094 "\"required\":[\"type\",\"value\"]"
4095 "},"
4096 "\"u64Value\":{"
4097 "\"properties\":{"
4098 "\"type\":{"
4099 "\"type\":\"string\","
4100 "\"enum\":[\"u64\"]"
4101 "},"
4102 "\"value\":{"
4103 "\"type\":\"integer\","
4104 "\"minimum\":0,"
4105 "\"maximum\":9007199254740991"
4106 "}"
4107 "},"
4108 "\"required\":[\"type\",\"value\"]"
4109 "},"
4110 "\"strValue\":{"
4111 "\"properties\":{"
4112 "\"type\":{"
4113 "\"type\":\"string\","
4114 "\"enum\":[\"str\"]"
4115 "},"
4116 "\"value\":{\"type\":\"string\"}"
4117 "},"
4118 "\"required\":[\"type\",\"value\"]"
4119 "},"
4120 "\"unknownValue\":{"
4121 "\"properties\":{"
4122 "\"type\":{"
4123 "\"type\":\"integer\","
4124 "\"minimum\":0"
4125 "},"
4126 "\"value\":{"
4127 "\"type\":\"string\","
4128 "\"enum\":[\"unknown\"]"
4129 "}"
4130 "},"
4131 "\"required\":[\"type\",\"value\"]"
4132 "}"
4133 "}"
4134 "}"
4135 "}"
4136 "}");
4137
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004138 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004139 chunk_reset(out);
4140 chunk_appendf(out,
4141 "{\"errorStr\":\"output buffer too short\"}");
4142 }
4143}
4144
4145/* This function dumps the schema onto the stream interface's read buffer.
4146 * It returns 0 as long as it does not complete, non-zero upon completion.
4147 * No state is used.
4148 */
4149static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
4150{
4151 chunk_reset(&trash);
4152
4153 stats_dump_json_schema(&trash);
4154
Willy Tarreau06d80a92017-10-19 14:32:15 +02004155 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004156 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01004157 return 0;
4158 }
4159
4160 return 1;
4161}
4162
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004163static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004164{
4165 struct proxy *px;
4166 struct server *sv;
4167 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004168 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004169 int clrall = 0;
4170
4171 if (strcmp(args[2], "all") == 0)
4172 clrall = 1;
4173
4174 /* check permissions */
4175 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4176 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4177 return 1;
4178
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004179 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004180 if (clrall) {
4181 memset(&px->be_counters, 0, sizeof(px->be_counters));
4182 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4183 }
4184 else {
4185 px->be_counters.conn_max = 0;
4186 px->be_counters.p.http.rps_max = 0;
4187 px->be_counters.sps_max = 0;
4188 px->be_counters.cps_max = 0;
4189 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004190 px->be_counters.qtime_max = 0;
4191 px->be_counters.ctime_max = 0;
4192 px->be_counters.dtime_max = 0;
4193 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004194
4195 px->fe_counters.conn_max = 0;
4196 px->fe_counters.p.http.rps_max = 0;
4197 px->fe_counters.sps_max = 0;
4198 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004199 }
4200
4201 for (sv = px->srv; sv; sv = sv->next)
4202 if (clrall)
4203 memset(&sv->counters, 0, sizeof(sv->counters));
4204 else {
4205 sv->counters.cur_sess_max = 0;
4206 sv->counters.nbpend_max = 0;
4207 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004208 sv->counters.qtime_max = 0;
4209 sv->counters.ctime_max = 0;
4210 sv->counters.dtime_max = 0;
4211 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004212 }
4213
4214 list_for_each_entry(li, &px->conf.listeners, by_fe)
4215 if (li->counters) {
4216 if (clrall)
4217 memset(li->counters, 0, sizeof(*li->counters));
4218 else
4219 li->counters->conn_max = 0;
4220 }
4221 }
4222
4223 global.cps_max = 0;
4224 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004225 global.ssl_max = 0;
4226 global.ssl_fe_keys_max = 0;
4227 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004228
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004229 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4230 if (!mod->clearable && !clrall)
4231 continue;
4232
4233 for (px = proxies_list; px; px = px->next) {
4234 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4235
4236 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4237 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4238 mod,
4239 mod->counters,
4240 mod->counters_size);
4241 }
4242
4243 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4244 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4245 mod,
4246 mod->counters,
4247 mod->counters_size);
4248 }
4249
4250 if (mod_cap & STATS_PX_CAP_SRV) {
4251 for (sv = px->srv; sv; sv = sv->next) {
4252 EXTRA_COUNTERS_INIT(sv->extra_counters,
4253 mod,
4254 mod->counters,
4255 mod->counters_size);
4256 }
4257 }
4258
4259 if (mod_cap & STATS_PX_CAP_LI) {
4260 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4261 EXTRA_COUNTERS_INIT(li->extra_counters,
4262 mod,
4263 mod->counters,
4264 mod->counters_size);
4265 }
4266 }
4267 }
4268 }
4269
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004270 dns_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_DNS]);
4271
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004272 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004273 return 1;
4274}
4275
4276
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004277static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004278{
Willy Tarreau2f397382019-10-09 11:43:59 +02004279 int arg = 2;
4280
Willy Tarreaud25fc792016-12-16 12:33:47 +01004281 appctx->ctx.stats.scope_str = 0;
4282 appctx->ctx.stats.scope_len = 0;
4283 appctx->ctx.stats.flags = 0;
4284
Willy Tarreau2f397382019-10-09 11:43:59 +02004285 while (*args[arg]) {
4286 if (strcmp(args[arg], "typed") == 0)
4287 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4288 else if (strcmp(args[arg], "json") == 0)
4289 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4290 else if (strcmp(args[arg], "desc") == 0)
4291 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4292 arg++;
4293 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004294 return 0;
4295}
4296
4297
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004298static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004299{
Willy Tarreau2f397382019-10-09 11:43:59 +02004300 int arg = 2;
4301
Willy Tarreaud25fc792016-12-16 12:33:47 +01004302 appctx->ctx.stats.scope_str = 0;
4303 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004304 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4305
4306 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
4307 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004308
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004309 /* proxy is the default domain */
4310 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
4311 if (!strcmp(args[arg], "domain")) {
4312 ++args;
4313
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004314 if (!strcmp(args[arg], "proxy")) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004315 ++args;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004316 } else if (!strcmp(args[arg], "dns")) {
4317 appctx->ctx.stats.domain = STATS_DOMAIN_DNS;
4318 ++args;
4319 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004320 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004321 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004322 }
4323
4324 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4325 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004326 struct proxy *px;
4327
Willy Tarreau2f397382019-10-09 11:43:59 +02004328 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004329 if (px)
4330 appctx->ctx.stats.iid = px->uuid;
4331 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004332 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004333
Willy Tarreau9d008692019-08-09 11:21:01 +02004334 if (!appctx->ctx.stats.iid)
4335 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004336
Willy Tarreau2b812e22016-11-22 16:18:05 +01004337 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004338 appctx->ctx.stats.type = atoi(args[arg+1]);
4339 appctx->ctx.stats.sid = atoi(args[arg+2]);
4340 arg += 3;
4341 }
4342
4343 while (*args[arg]) {
4344 if (strcmp(args[arg], "typed") == 0)
4345 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4346 else if (strcmp(args[arg], "json") == 0)
4347 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4348 else if (strcmp(args[arg], "desc") == 0)
4349 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02004350 else if (strcmp(args[arg], "no-maint") == 0)
4351 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02004352 else if (strcmp(args[arg], "up") == 0)
4353 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02004354 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004355 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004356
Willy Tarreau2b812e22016-11-22 16:18:05 +01004357 return 0;
4358}
4359
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004360static int cli_io_handler_dump_info(struct appctx *appctx)
4361{
4362 return stats_dump_info_to_buffer(appctx->owner);
4363}
4364
Willy Tarreau2b812e22016-11-22 16:18:05 +01004365/* This I/O handler runs as an applet embedded in a stream interface. It is
4366 * used to send raw stats over a socket.
4367 */
4368static int cli_io_handler_dump_stat(struct appctx *appctx)
4369{
Christopher Fauletef779222018-10-31 08:47:01 +01004370 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004371}
4372
Simon Horman6f6bb382017-01-04 09:37:26 +01004373static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4374{
4375 return stats_dump_json_schema_to_buffer(appctx->owner);
4376}
4377
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004378static int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
4379 int type, int px_cap)
4380{
4381 struct stats_module *mod;
4382
4383 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
4384
4385 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4386 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4387 continue;
4388
4389 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
4390 }
4391
4392 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
4393
4394 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4395 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4396 continue;
4397
4398 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
4399 }
4400
4401 return 1;
4402
4403 alloc_failed:
4404 return 0;
4405}
4406
4407/* Initialize and allocate all extra counters for a proxy and its attached
4408 * servers/listeners with all already registered stats module
4409 */
4410int stats_allocate_proxy_counters(struct proxy *px)
4411{
4412 struct server *sv;
4413 struct listener *li;
4414
4415 if (px->cap & PR_CAP_FE) {
4416 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
4417 COUNTERS_FE,
4418 STATS_PX_CAP_FE)) {
4419 return 0;
4420 }
4421 }
4422
4423 if (px->cap & PR_CAP_BE) {
4424 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
4425 COUNTERS_BE,
4426 STATS_PX_CAP_BE)) {
4427 return 0;
4428 }
4429 }
4430
4431 for (sv = px->srv; sv; sv = sv->next) {
4432 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
4433 COUNTERS_SV,
4434 STATS_PX_CAP_SRV)) {
4435 return 0;
4436 }
4437 }
4438
4439 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4440 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
4441 COUNTERS_LI,
4442 STATS_PX_CAP_LI)) {
4443 return 0;
4444 }
4445 }
4446
4447 return 1;
4448}
4449
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004450void stats_register_module(struct stats_module *m)
4451{
4452 const uint8_t domain = stats_get_domain(m->domain_flags);
4453
4454 LIST_ADDQ(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004455 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004456}
4457
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004458static int allocate_stats_px_postcheck(void)
4459{
4460 struct stats_module *mod;
4461 size_t i = ST_F_TOTAL_FIELDS;
4462 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004463 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004464
4465 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
4466
4467 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
4468 if (!stat_f[STATS_DOMAIN_PROXY]) {
4469 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
4470 err_code |= ERR_ALERT | ERR_FATAL;
4471 return err_code;
4472 }
4473
4474 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
4475 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
4476
4477 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4478 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
4479 mod->stats,
4480 mod->stats_count * sizeof(struct name_desc));
4481 i += mod->stats_count;
4482 }
4483
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004484 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004485 if (!stats_allocate_proxy_counters(px)) {
4486 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
4487 err_code |= ERR_ALERT | ERR_FATAL;
4488 return err_code;
4489 }
4490 }
4491
4492 stat_l[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct field));
4493 if (!stat_l[STATS_DOMAIN_PROXY]) {
4494 ha_alert("stats: cannot allocate a line for proxy statistics\n");
4495 err_code |= ERR_ALERT | ERR_FATAL;
4496 return err_code;
4497 }
4498
4499 return err_code;
4500}
4501
4502REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
4503
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004504static int allocate_stats_dns_postcheck(void)
4505{
4506 struct stats_module *mod;
4507 size_t i = 0;
4508 int err_code = 0;
4509
4510 stat_f[STATS_DOMAIN_DNS] = malloc(stat_count[STATS_DOMAIN_DNS] * sizeof(struct name_desc));
4511 if (!stat_f[STATS_DOMAIN_DNS]) {
4512 ha_alert("stats: cannot allocate all fields for dns statistics\n");
4513 err_code |= ERR_ALERT | ERR_FATAL;
4514 return err_code;
4515 }
4516
4517 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_DNS], list) {
4518 memcpy(stat_f[STATS_DOMAIN_DNS] + i,
4519 mod->stats,
4520 mod->stats_count * sizeof(struct name_desc));
4521 i += mod->stats_count;
4522 }
4523
4524 if (!dns_allocate_counters(&stats_module_list[STATS_DOMAIN_DNS])) {
4525 ha_alert("stats: cannot allocate all counters for dns statistics\n");
4526 err_code |= ERR_ALERT | ERR_FATAL;
4527 return err_code;
4528 }
4529
4530 stat_l[STATS_DOMAIN_DNS] = malloc(stat_count[STATS_DOMAIN_DNS] * sizeof(struct field));
4531 if (!stat_l[STATS_DOMAIN_DNS]) {
4532 ha_alert("stats: cannot allocate a line for dns statistics\n");
4533 err_code |= ERR_ALERT | ERR_FATAL;
4534 return err_code;
4535 }
4536
4537 return err_code;
4538}
4539
4540REGISTER_CONFIG_POSTPARSER("allocate-stats-dns", allocate_stats_dns_postcheck);
4541
Willy Tarreau2b812e22016-11-22 16:18:05 +01004542/* register cli keywords */
4543static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004544 { { "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 +02004545 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreau3e320362020-10-23 17:28:57 +02004546 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server [desc|json|no-maint|typed|up]*", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01004547 { { "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 +01004548 {{},}
4549}};
4550
Willy Tarreau0108d902018-11-25 19:14:37 +01004551INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4552
William Lallemand74c24fb2016-11-21 17:18:36 +01004553struct applet http_stats_applet = {
4554 .obj_type = OBJ_TYPE_APPLET,
4555 .name = "<STATS>", /* used for logging */
4556 .fct = http_stats_io_handler,
4557 .release = NULL,
4558};
4559
William Lallemand74c24fb2016-11-21 17:18:36 +01004560/*
4561 * Local variables:
4562 * c-indent-level: 8
4563 * c-basic-offset: 8
4564 * End:
4565 */