blob: 97e7fd865acd89949855987e9ee124619fc5889a [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>
William Lallemand74c24fb2016-11-21 17:18:36 +010016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <pwd.h>
20#include <grp.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020027#include <haproxy/activity.h>
Willy Tarreau91cefca2022-05-03 17:08:29 +020028#include <haproxy/applet.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 Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020036#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020037#include <haproxy/debug.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 Tarreaub7fc4c42021-10-06 18:56:42 +020044#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020047#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020048#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020050#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020051#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/pipe.h>
53#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010055#include <haproxy/resolvers.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020056#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020057#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020058#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020059#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020060#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020061#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020063#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020064#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020066#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020067#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Christopher Faulet2b9b6782019-02-27 16:42:58 +010070/* status codes available for the stats admin page (strictly 4 chars length) */
71const char *stat_status_codes[STAT_STATUS_SIZE] = {
72 [STAT_STATUS_DENY] = "DENY",
73 [STAT_STATUS_DONE] = "DONE",
74 [STAT_STATUS_ERRP] = "ERRP",
75 [STAT_STATUS_EXCD] = "EXCD",
76 [STAT_STATUS_NONE] = "NONE",
77 [STAT_STATUS_PART] = "PART",
78 [STAT_STATUS_UNKN] = "UNKN",
79 [STAT_STATUS_IVAL] = "IVAL",
80};
81
Willy Tarreau0baac8c2016-11-22 16:36:53 +010082/* These are the field names for each INF_* field position. Please pay attention
83 * to always use the exact same name except that the strings for new names must
84 * be lower case or CamelCase while the enum entries must be upper case.
85 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020086const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020087 [INF_NAME] = { .name = "Name", .desc = "Product name" },
88 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
89 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
90 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020091 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020092 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020093 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
94 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
95 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010096 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020097 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
William Dauchya8766cf2021-01-15 22:41:37 +010098 [INF_MEMMAX_BYTES] = { .name = "Memmax_bytes", .desc = "Worker process's hard limit on memory usage in byes (-m on command line)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100100 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200101 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100102 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200103 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
104 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
105 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
106 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
107 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
108 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
109 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
110 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
111 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
112 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
113 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
114 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
115 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
116 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
117 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
118 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
119 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
120 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
121 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
122 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
123 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
124 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
125 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
126 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
127 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
129 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
130 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
131 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
132 [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" },
133 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
134 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
136 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
137 [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)" },
138 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
139 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
140 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
141 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
142 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
143 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
144 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
145 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
146 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
147 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
148 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
149 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
150 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
151 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
152 [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 +0200153 [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 +0200154 [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 +0200155 [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 +0200156 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Adis Nezirovicb62b78b2021-01-15 13:12:33 +0100157 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200158 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100159};
160
Willy Tarreaueaa55372019-10-09 07:39:11 +0200161const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200162 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
163 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100164 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
165 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100166 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
167 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200168 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
169 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
170 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
171 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
172 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
173 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
174 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
175 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
176 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
177 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
178 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
179 [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 +0200180 [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 +0200181 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
182 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
183 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
184 [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" },
185 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
186 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
187 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +0200188 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200189 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
190 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
191 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
192 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
193 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
194 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
195 [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)" },
196 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100197 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of sessions per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200198 [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" },
199 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
200 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
201 [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" },
202 [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" },
203 [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" },
204 [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" },
205 [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" },
206 [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)" },
207 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
208 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100209 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of http requests observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200210 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
211 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
212 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
213 [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" },
214 [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" },
215 [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)" },
216 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
217 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
218 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
219 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
220 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
221 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
222 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
223 [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)" },
224 [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" },
225 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
226 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
227 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
228 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
229 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
233 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
234 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
235 [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" },
236 [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" },
237 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
238 [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" },
239 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100240 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of connections per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200241 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
242 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
243 [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" },
244 [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" },
245 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
246 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
247 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
248 [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" },
249 [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" },
250 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
251 [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 +0100252 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
253 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
254 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
255 [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 +0100256 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200257 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
258 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
259 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200260 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200261 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
William Dauchy42d7c402021-11-07 10:18:47 +0100262 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "Backend's aggregated gauge of servers' state check status" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100263};
264
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100265/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100266THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200267
268/* description of statistics (static and dynamic) */
269static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
270static size_t stat_count[STATS_DOMAIN_COUNT];
271
272/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100273THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100274
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200275/* list of all registered stats module */
276static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
277 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200278 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200279};
280
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100281THREAD_LOCAL void *trash_counters;
282
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200283static inline uint8_t stats_get_domain(uint32_t domain)
284{
285 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
286}
287
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200288static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
289{
290 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
291}
292
Christopher Faulet6338a082019-09-09 15:50:54 +0200293static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100294
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200295int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100296{
297 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100298 if (chk->data >= channel_htx_recv_max(chn, htx))
299 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200300 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100301 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100302 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100303 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100304 }
305 else {
306 if (ci_putchk(chn, chk) == -1)
307 return 0;
308 }
309 return 1;
310}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100311
Willy Tarreaucaff6312022-05-27 10:17:46 +0200312static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100313{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200314 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200315 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200316 struct htx *htx = htxbuf(&req->buf);
317 struct htx_blk *blk;
318 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100319
Christopher Fauletb7f88902019-07-15 21:56:43 +0200320 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100321 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200322 ALREADY_CHECKED(blk);
323 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200324 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100325}
326
William Lallemand74c24fb2016-11-21 17:18:36 +0100327/*
328 * http_stats_io_handler()
329 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
330 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100331 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100332 * -> stats_dump_html_head() // emits the HTML headers
333 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
334 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
335 * -> stats_dump_html_px_hdr()
336 * -> stats_dump_fe_stats()
337 * -> stats_dump_li_stats()
338 * -> stats_dump_sv_stats()
339 * -> stats_dump_be_stats()
340 * -> stats_dump_html_px_end()
341 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100342 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100343 */
344
345
William Lallemand74c24fb2016-11-21 17:18:36 +0100346/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
347 * for clearing it if needed.
348 * NOTE: Some tools happen to rely on the field position instead of its name,
349 * so please only append new fields at the end, never in the middle.
350 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200351static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100352{
353 int field;
354
355 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200356 if (stat_f[domain]) {
357 for (field = 0; field < stat_count[domain]; ++field) {
358 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100359
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200360 /* print special delimiter on proxy stats to mark end of
361 static fields */
362 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
363 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200364 }
365 }
366
William Lallemand74c24fb2016-11-21 17:18:36 +0100367 chunk_appendf(&trash, "\n");
368}
369
William Lallemand74c24fb2016-11-21 17:18:36 +0100370/* Emits a stats field without any surrounding element and properly encoded to
371 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
372 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200373int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100374{
375 switch (field_format(f, 0)) {
376 case FF_EMPTY: return 1;
377 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
378 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
379 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
380 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200381 case FF_FLT: {
382 size_t prev_data = out->data;
383 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
384 return out->data;
385 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100386 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
387 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
388 }
389}
390
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200391const char *field_to_html_str(const struct field *f)
392{
393 switch (field_format(f, 0)) {
394 case FF_S32: return U2H(f->u.s32);
395 case FF_S64: return U2H(f->u.s64);
396 case FF_U64: return U2H(f->u.u64);
397 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200398 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200399 case FF_STR: return field_str(f, 0);
400 case FF_EMPTY:
401 default:
402 return "";
403 }
404}
405
William Lallemand74c24fb2016-11-21 17:18:36 +0100406/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
407 * output is supposed to be used on its own line. Returns non-zero on success, 0
408 * if the buffer is full.
409 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200410int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100411{
412 switch (field_format(f, 0)) {
413 case FF_EMPTY: return 1;
414 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
415 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
416 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
417 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200418 case FF_FLT: {
419 size_t prev_data = out->data;
420 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
421 return out->data;
422 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100423 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
424 default: return chunk_appendf(out, "%08x:?", f->type);
425 }
426}
427
Simon Horman05ee2132017-01-04 09:37:25 +0100428/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
429 * the recommendation for interoperable integers in section 6 of RFC 7159.
430 */
431#define JSON_INT_MAX ((1ULL << 53) - 1)
432#define JSON_INT_MIN (0 - JSON_INT_MAX)
433
434/* Emits a stats field value and its type in JSON.
435 * Returns non-zero on success, 0 on error.
436 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200437int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100438{
439 int old_len;
440 char buf[20];
441 const char *type, *value = buf, *quote = "";
442
443 switch (field_format(f, 0)) {
444 case FF_EMPTY: return 1;
445 case FF_S32: type = "\"s32\"";
446 snprintf(buf, sizeof(buf), "%d", f->u.s32);
447 break;
448 case FF_U32: type = "\"u32\"";
449 snprintf(buf, sizeof(buf), "%u", f->u.u32);
450 break;
451 case FF_S64: type = "\"s64\"";
452 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
453 return 0;
454 type = "\"s64\"";
455 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
456 break;
457 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
458 return 0;
459 type = "\"u64\"";
460 snprintf(buf, sizeof(buf), "%llu",
461 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200462 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200463 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200464 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100465 break;
466 case FF_STR: type = "\"str\"";
467 value = field_str(f, 0);
468 quote = "\"";
469 break;
470 default: snprintf(buf, sizeof(buf), "%u", f->type);
471 type = buf;
472 value = "unknown";
473 quote = "\"";
474 break;
475 }
476
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200477 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100478 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
479 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200480 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100481}
482
William Lallemand74c24fb2016-11-21 17:18:36 +0100483/* Emits an encoding of the field type on 3 characters followed by a delimiter.
484 * Returns non-zero on success, 0 if the buffer is full.
485 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200486int stats_emit_field_tags(struct buffer *out, const struct field *f,
487 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100488{
489 char origin, nature, scope;
490
491 switch (field_origin(f, 0)) {
492 case FO_METRIC: origin = 'M'; break;
493 case FO_STATUS: origin = 'S'; break;
494 case FO_KEY: origin = 'K'; break;
495 case FO_CONFIG: origin = 'C'; break;
496 case FO_PRODUCT: origin = 'P'; break;
497 default: origin = '?'; break;
498 }
499
500 switch (field_nature(f, 0)) {
501 case FN_GAUGE: nature = 'G'; break;
502 case FN_LIMIT: nature = 'L'; break;
503 case FN_MIN: nature = 'm'; break;
504 case FN_MAX: nature = 'M'; break;
505 case FN_RATE: nature = 'R'; break;
506 case FN_COUNTER: nature = 'C'; break;
507 case FN_DURATION: nature = 'D'; break;
508 case FN_AGE: nature = 'A'; break;
509 case FN_TIME: nature = 'T'; break;
510 case FN_NAME: nature = 'N'; break;
511 case FN_OUTPUT: nature = 'O'; break;
512 case FN_AVG: nature = 'a'; break;
513 default: nature = '?'; break;
514 }
515
516 switch (field_scope(f, 0)) {
517 case FS_PROCESS: scope = 'P'; break;
518 case FS_SERVICE: scope = 'S'; break;
519 case FS_SYSTEM: scope = 's'; break;
520 case FS_CLUSTER: scope = 'C'; break;
521 default: scope = '?'; break;
522 }
523
524 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
525}
526
Simon Horman05ee2132017-01-04 09:37:25 +0100527/* Emits an encoding of the field type as JSON.
528 * Returns non-zero on success, 0 if the buffer is full.
529 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200530int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100531{
532 const char *origin, *nature, *scope;
533 int old_len;
534
535 switch (field_origin(f, 0)) {
536 case FO_METRIC: origin = "Metric"; break;
537 case FO_STATUS: origin = "Status"; break;
538 case FO_KEY: origin = "Key"; break;
539 case FO_CONFIG: origin = "Config"; break;
540 case FO_PRODUCT: origin = "Product"; break;
541 default: origin = "Unknown"; break;
542 }
543
544 switch (field_nature(f, 0)) {
545 case FN_GAUGE: nature = "Gauge"; break;
546 case FN_LIMIT: nature = "Limit"; break;
547 case FN_MIN: nature = "Min"; break;
548 case FN_MAX: nature = "Max"; break;
549 case FN_RATE: nature = "Rate"; break;
550 case FN_COUNTER: nature = "Counter"; break;
551 case FN_DURATION: nature = "Duration"; break;
552 case FN_AGE: nature = "Age"; break;
553 case FN_TIME: nature = "Time"; break;
554 case FN_NAME: nature = "Name"; break;
555 case FN_OUTPUT: nature = "Output"; break;
556 case FN_AVG: nature = "Avg"; break;
557 default: nature = "Unknown"; break;
558 }
559
560 switch (field_scope(f, 0)) {
561 case FS_PROCESS: scope = "Process"; break;
562 case FS_SERVICE: scope = "Service"; break;
563 case FS_SYSTEM: scope = "System"; break;
564 case FS_CLUSTER: scope = "Cluster"; break;
565 default: scope = "Unknown"; break;
566 }
567
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200568 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100569 chunk_appendf(out, "\"tags\":{"
570 "\"origin\":\"%s\","
571 "\"nature\":\"%s\","
572 "\"scope\":\"%s\""
573 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200574 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100575}
William Lallemand74c24fb2016-11-21 17:18:36 +0100576
577/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200578static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200579 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200580 unsigned int flags,
581 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100582{
583 int field;
584
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200585 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100586 if (!stats_emit_raw_data_field(out, &stats[field]))
587 return 0;
588 if (!chunk_strcat(out, ","))
589 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200590
591 /* print special delimiter on proxy stats to mark end of
592 static fields */
593 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
594 if (!chunk_strcat(out, "-,"))
595 return 0;
596 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100597 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200598
William Lallemand74c24fb2016-11-21 17:18:36 +0100599 chunk_strcat(out, "\n");
600 return 1;
601}
602
603/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200604static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200605 const struct field *stats,
606 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200607 unsigned int flags,
608 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100609{
610 int field;
611
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200612 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100613 if (!stats[field].type)
614 continue;
615
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200616 switch (domain) {
617 case STATS_DOMAIN_PROXY:
618 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
619 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
620 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
621 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
622 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
623 '?',
624 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
625 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200626 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200627 stats[ST_F_PID].u.u32);
628 break;
629
Emeric Brunf8642ee2021-10-29 17:59:18 +0200630 case STATS_DOMAIN_RESOLVERS:
631 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200632 stat_f[domain][field].name);
633 break;
634
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200635 default:
636 break;
637 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100638
639 if (!stats_emit_field_tags(out, &stats[field], ':'))
640 return 0;
641 if (!stats_emit_typed_data_field(out, &stats[field]))
642 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200643
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200644 if (flags & STAT_SHOW_FDESC &&
645 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200646 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200647 }
648
William Lallemand74c24fb2016-11-21 17:18:36 +0100649 if (!chunk_strcat(out, "\n"))
650 return 0;
651 }
652 return 1;
653}
654
Simon Horman05ee2132017-01-04 09:37:25 +0100655/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200656static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200657 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100658{
659 int field;
660 int started = 0;
661
662 if (!chunk_strcat(out, "["))
663 return 0;
664
665 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
666 int old_len;
667
668 if (!field_format(info, field))
669 continue;
670
671 if (started && !chunk_strcat(out, ","))
672 goto err;
673 started = 1;
674
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200675 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100676 chunk_appendf(out,
677 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
678 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200679 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100680 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200681 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100682 goto err;
683
684 if (!stats_emit_json_field_tags(out, &info[field]))
685 goto err;
686
687 if (!stats_emit_json_data_field(out, &info[field]))
688 goto err;
689
690 if (!chunk_strcat(out, "}"))
691 goto err;
692 }
693
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200694 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100695 goto err;
696 return 1;
697
698err:
699 chunk_reset(out);
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200700 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
Simon Horman05ee2132017-01-04 09:37:25 +0100701 return 0;
702}
703
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200704static void stats_print_proxy_field_json(struct buffer *out,
705 const struct field *stat,
706 const char *name,
707 int pos,
708 uint32_t field_type,
709 uint32_t iid,
710 uint32_t sid,
711 uint32_t pid)
712{
713 const char *obj_type;
714 switch (field_type) {
715 case STATS_TYPE_FE: obj_type = "Frontend"; break;
716 case STATS_TYPE_BE: obj_type = "Backend"; break;
717 case STATS_TYPE_SO: obj_type = "Listener"; break;
718 case STATS_TYPE_SV: obj_type = "Server"; break;
719 default: obj_type = "Unknown"; break;
720 }
721
722 chunk_appendf(out,
723 "{"
724 "\"objType\":\"%s\","
725 "\"proxyId\":%u,"
726 "\"id\":%u,"
727 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
728 "\"processNum\":%u,",
729 obj_type, iid, sid, pos, name, pid);
730}
731
Emeric Brunf8642ee2021-10-29 17:59:18 +0200732static void stats_print_rslv_field_json(struct buffer *out,
733 const struct field *stat,
734 const char *name,
735 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200736{
737 chunk_appendf(out,
738 "{"
739 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
740 pos, name);
741}
742
743
Simon Horman05ee2132017-01-04 09:37:25 +0100744/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200745static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200746 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200747 unsigned int flags,
748 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100749{
750 int field;
751 int started = 0;
752
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200753 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100754 return 0;
755 if (!chunk_strcat(out, "["))
756 return 0;
757
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200758 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100759 int old_len;
760
761 if (!stats[field].type)
762 continue;
763
764 if (started && !chunk_strcat(out, ","))
765 goto err;
766 started = 1;
767
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200769 if (domain == STATS_DOMAIN_PROXY) {
770 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200771 stat_f[domain][field].name,
772 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200773 stats[ST_F_TYPE].u.u32,
774 stats[ST_F_IID].u.u32,
775 stats[ST_F_SID].u.u32,
776 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200777 } else if (domain == STATS_DOMAIN_RESOLVERS) {
778 stats_print_rslv_field_json(out, &stats[field],
779 stat_f[domain][field].name,
780 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200781 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200782
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200783 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100784 goto err;
785
786 if (!stats_emit_json_field_tags(out, &stats[field]))
787 goto err;
788
789 if (!stats_emit_json_data_field(out, &stats[field]))
790 goto err;
791
792 if (!chunk_strcat(out, "}"))
793 goto err;
794 }
795
796 if (!chunk_strcat(out, "]"))
797 goto err;
798
799 return 1;
800
801err:
802 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200803 if (flags & STAT_STARTED)
804 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100805 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
806 return 0;
807}
808
William Lallemand74c24fb2016-11-21 17:18:36 +0100809/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200810 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200811 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
812 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
813 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100814 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200815static int stats_dump_fields_html(struct buffer *out,
816 const struct field *stats,
817 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100818{
Willy Tarreau83061a82018-07-13 11:56:34 +0200819 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200820 struct stats_module *mod;
821 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100822
823 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
824 chunk_appendf(out,
825 /* name, queue */
826 "<tr class=\"frontend\">");
827
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200828 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100829 /* Column sub-heading for Enable or Disable server */
830 chunk_appendf(out, "<td></td>");
831 }
832
833 chunk_appendf(out,
834 "<td class=ac>"
835 "<a name=\"%s/Frontend\"></a>"
836 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
837 "<td colspan=3></td>"
838 "",
839 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
840
841 chunk_appendf(out,
842 /* sessions rate : current */
843 "<td><u>%s<div class=tips><table class=det>"
844 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
845 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
846 "",
847 U2H(stats[ST_F_RATE].u.u32),
848 U2H(stats[ST_F_CONN_RATE].u.u32),
849 U2H(stats[ST_F_RATE].u.u32));
850
851 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
852 chunk_appendf(out,
853 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
854 U2H(stats[ST_F_REQ_RATE].u.u32));
855
856 chunk_appendf(out,
857 "</table></div></u></td>"
858 /* sessions rate : max */
859 "<td><u>%s<div class=tips><table class=det>"
860 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
861 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
862 "",
863 U2H(stats[ST_F_RATE_MAX].u.u32),
864 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
865 U2H(stats[ST_F_RATE_MAX].u.u32));
866
867 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
868 chunk_appendf(out,
869 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
870 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
871
872 chunk_appendf(out,
873 "</table></div></u></td>"
874 /* sessions rate : limit */
875 "<td>%s</td>",
876 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
877
878 chunk_appendf(out,
879 /* sessions: current, max, limit, total */
880 "<td>%s</td><td>%s</td><td>%s</td>"
881 "<td><u>%s<div class=tips><table class=det>"
882 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
883 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
884 "",
885 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
886 U2H(stats[ST_F_STOT].u.u64),
887 U2H(stats[ST_F_CONN_TOT].u.u64),
888 U2H(stats[ST_F_STOT].u.u64));
889
890 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
891 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
892 chunk_appendf(out,
893 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
894 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
895 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
896 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
897 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
898 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
899 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
900 "<tr><th>- other responses:</th><td>%s</td></tr>"
901 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100902 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
903 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200904 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100905 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100906 "",
907 U2H(stats[ST_F_REQ_TOT].u.u64),
908 U2H(stats[ST_F_HRSP_1XX].u.u64),
909 U2H(stats[ST_F_HRSP_2XX].u.u64),
910 U2H(stats[ST_F_COMP_RSP].u.u64),
911 stats[ST_F_HRSP_2XX].u.u64 ?
912 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
913 U2H(stats[ST_F_HRSP_3XX].u.u64),
914 U2H(stats[ST_F_HRSP_4XX].u.u64),
915 U2H(stats[ST_F_HRSP_5XX].u.u64),
916 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200917 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100918 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
919 U2H(stats[ST_F_CACHE_HITS].u.u64),
920 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
921 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100922 U2H(stats[ST_F_WREW].u.u64),
923 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100924 }
925
926 chunk_appendf(out,
927 "</table></div></u></td>"
928 /* sessions: lbtot, lastsess */
929 "<td></td><td></td>"
930 /* bytes : in */
931 "<td>%s</td>"
932 "",
933 U2H(stats[ST_F_BIN].u.u64));
934
935 chunk_appendf(out,
936 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
937 "<td>%s%s<div class=tips><table class=det>"
938 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
939 "<tr><th>Compression in:</th><td>%s</td></tr>"
940 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
941 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
942 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
943 "</table></div>%s</td>",
944 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
945 U2H(stats[ST_F_BOUT].u.u64),
946 U2H(stats[ST_F_BOUT].u.u64),
947 U2H(stats[ST_F_COMP_IN].u.u64),
948 U2H(stats[ST_F_COMP_OUT].u.u64),
949 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
950 U2H(stats[ST_F_COMP_BYP].u.u64),
951 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
952 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,
953 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
954
955 chunk_appendf(out,
956 /* denied: req, resp */
957 "<td>%s</td><td>%s</td>"
958 /* errors : request, connect, response */
959 "<td>%s</td><td></td><td></td>"
960 /* warnings: retries, redispatches */
961 "<td></td><td></td>"
962 /* server status : reflect frontend status */
963 "<td class=ac>%s</td>"
964 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200965 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100966 "",
967 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
968 U2H(stats[ST_F_EREQ].u.u64),
969 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200970
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200971 if (flags & STAT_SHMODULES) {
972 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100973 chunk_appendf(out, "<td>");
974
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200975 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100976 chunk_appendf(out,
977 "<u>%s<div class=tips><table class=det>",
978 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200979 for (j = 0; j < mod->stats_count; ++j) {
980 chunk_appendf(out,
981 "<tr><th>%s</th><td>%s</td></tr>",
982 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
983 ++i;
984 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100985 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200986 } else {
987 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200988 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200989
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100990 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200991 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200992 }
993
994 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100995 }
996 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
997 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200998 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100999 /* Column sub-heading for Enable or Disable server */
1000 chunk_appendf(out, "<td></td>");
1001 }
1002
1003 chunk_appendf(out,
1004 /* frontend name, listener name */
1005 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1006 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1007 "",
1008 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001009 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001010 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1011
Willy Tarreau708c4162019-10-09 10:19:16 +02001012 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001013 chunk_appendf(out, "<div class=tips>");
1014
Willy Tarreau90807112020-02-25 08:16:33 +01001015 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001016 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1017 else if (*field_str(stats, ST_F_ADDR) == '[')
1018 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1019 else if (*field_str(stats, ST_F_ADDR))
1020 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1021
1022 /* id */
1023 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1024 }
1025
1026 chunk_appendf(out,
1027 /* queue */
1028 "%s</td><td colspan=3></td>"
1029 /* sessions rate: current, max, limit */
1030 "<td colspan=3>&nbsp;</td>"
1031 /* sessions: current, max, limit, total, lbtot, lastsess */
1032 "<td>%s</td><td>%s</td><td>%s</td>"
1033 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1034 /* bytes: in, out */
1035 "<td>%s</td><td>%s</td>"
1036 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001037 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001038 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1039 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1040
1041 chunk_appendf(out,
1042 /* denied: req, resp */
1043 "<td>%s</td><td>%s</td>"
1044 /* errors: request, connect, response */
1045 "<td>%s</td><td></td><td></td>"
1046 /* warnings: retries, redispatches */
1047 "<td></td><td></td>"
1048 /* server status: reflect listener status */
1049 "<td class=ac>%s</td>"
1050 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001051 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001052 "",
1053 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1054 U2H(stats[ST_F_EREQ].u.u64),
1055 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001056
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001057 if (flags & STAT_SHMODULES) {
1058 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001059 chunk_appendf(out, "<td>");
1060
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001061 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001062 chunk_appendf(out,
1063 "<u>%s<div class=tips><table class=det>",
1064 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001065 for (j = 0; j < mod->stats_count; ++j) {
1066 chunk_appendf(out,
1067 "<tr><th>%s</th><td>%s</td></tr>",
1068 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1069 ++i;
1070 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001071 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001072 } else {
1073 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001074 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001075
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001076 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001077 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001078 }
1079
1080 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001081 }
1082 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1083 const char *style;
1084
1085 /* determine the style to use depending on the server's state,
1086 * its health and weight. There isn't a 1-to-1 mapping between
1087 * state and styles for the cases where the server is (still)
1088 * up. The reason is that we don't want to report nolb and
1089 * drain with the same color.
1090 */
1091
1092 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1093 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1094 style = "down";
1095 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001096 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001097 style = "going_up";
1098 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001099 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1100 style = "draining";
1101 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001102 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001103 style = "going_down";
1104 }
1105 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1106 style = "nolb";
1107 }
1108 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1109 style = "no_check";
1110 }
1111 else if (!stats[ST_F_CHKFAIL].type ||
1112 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1113 /* no check or max health = UP */
1114 if (stats[ST_F_WEIGHT].u.u32)
1115 style = "up";
1116 else
1117 style = "draining";
1118 }
1119 else {
1120 style = "going_down";
1121 }
1122
Willy Tarreau7b524852020-08-11 10:26:36 +02001123 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001124 chunk_appendf(out, "<tr class=\"maintain\">");
1125 else
1126 chunk_appendf(out,
1127 "<tr class=\"%s_%s\">",
1128 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1129
1130
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001131 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001132 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001133 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1134 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001135 field_str(stats, ST_F_SVNAME));
1136
1137 chunk_appendf(out,
1138 "<td class=ac><a name=\"%s/%s\"></a>%s"
1139 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1140 "",
1141 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001142 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001143 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1144
Willy Tarreau708c4162019-10-09 10:19:16 +02001145 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001146 chunk_appendf(out, "<div class=tips>");
1147
Willy Tarreau90807112020-02-25 08:16:33 +01001148 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001149 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1150 else if (*field_str(stats, ST_F_ADDR) == '[')
1151 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1152 else if (*field_str(stats, ST_F_ADDR))
1153 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1154
1155 /* id */
1156 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1157
1158 /* cookie */
1159 if (stats[ST_F_COOKIE].type) {
1160 chunk_appendf(out, ", cookie: '");
1161 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1162 chunk_htmlencode(out, &src);
1163 chunk_appendf(out, "'");
1164 }
1165
1166 chunk_appendf(out, "</div>");
1167 }
1168
1169 chunk_appendf(out,
1170 /* queue : current, max, limit */
1171 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1172 /* sessions rate : current, max, limit */
1173 "<td>%s</td><td>%s</td><td></td>"
1174 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001175 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001176 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1177 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1178
1179 chunk_appendf(out,
1180 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001181 "<td><u>%s<div class=tips>"
1182 "<table class=det>"
1183 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001184 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001185 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001186 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1187 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001188 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001189 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1190 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1191 "</table></div></u>"
1192 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001193 "<td><u>%s<div class=tips><table class=det>"
1194 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1195 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001196 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001197 U2H(stats[ST_F_SCUR].u.u32),
1198 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1199 U2H(stats[ST_F_SRV_ICUR].u.u32),
1200 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1201 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001202 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001203
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001204 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1205 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1206 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001207 U2H(stats[ST_F_STOT].u.u64),
1208 U2H(stats[ST_F_STOT].u.u64));
1209
1210 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1211 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001212 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001213 "<tr><th>New connections:</th><td>%s</td></tr>"
1214 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001215 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001216 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1217 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1218 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1219 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1220 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1221 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001222 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001223 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001224 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001225 U2H(stats[ST_F_CONNECT].u.u64),
1226 U2H(stats[ST_F_REUSE].u.u64),
1227 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1228 (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 +02001229 U2H(stats[ST_F_REQ_TOT].u.u64),
1230 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1231 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1232 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1233 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1234 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1235 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1236 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1237 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1238 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1239 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1240 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1241 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001242 U2H(stats[ST_F_WREW].u.u64),
1243 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001244 }
1245
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001246 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1247 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1248 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1249 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1250 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001251 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001252 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1253 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1254 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1255 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001256
1257 chunk_appendf(out,
1258 "</table></div></u></td>"
1259 /* sessions: lbtot, last */
1260 "<td>%s</td><td>%s</td>",
1261 U2H(stats[ST_F_LBTOT].u.u64),
1262 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1263
1264 chunk_appendf(out,
1265 /* bytes : in, out */
1266 "<td>%s</td><td>%s</td>"
1267 /* denied: req, resp */
1268 "<td></td><td>%s</td>"
1269 /* errors : request, connect */
1270 "<td></td><td>%s</td>"
1271 /* errors : response */
1272 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1273 /* warnings: retries, redispatches */
1274 "<td>%lld</td><td>%lld</td>"
1275 "",
1276 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1277 U2H(stats[ST_F_DRESP].u.u64),
1278 U2H(stats[ST_F_ECON].u.u64),
1279 U2H(stats[ST_F_ERESP].u.u64),
1280 (long long)stats[ST_F_CLI_ABRT].u.u64,
1281 (long long)stats[ST_F_SRV_ABRT].u.u64,
1282 (long long)stats[ST_F_WRETR].u.u64,
1283 (long long)stats[ST_F_WREDIS].u.u64);
1284
1285 /* status, last change */
1286 chunk_appendf(out, "<td class=ac>");
1287
1288 /* FIXME!!!!
1289 * LASTCHG should contain the last change for *this* server and must be computed
1290 * properly above, as was done below, ie: this server if maint, otherwise ref server
1291 * if tracking. Note that ref is either local or remote depending on tracking.
1292 */
1293
1294
Willy Tarreau7b524852020-08-11 10:26:36 +02001295 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001296 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1297 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001298 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001299 chunk_strcat(out, "<i>no check</i>");
1300 }
1301 else {
1302 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 +02001303 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001304 if (stats[ST_F_CHECK_HEALTH].u.u32)
1305 chunk_strcat(out, " &uarr;");
1306 }
1307 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1308 chunk_strcat(out, " &darr;");
1309 }
1310
Willy Tarreau7b524852020-08-11 10:26:36 +02001311 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001312 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1313 chunk_appendf(out,
1314 "</td><td class=ac><u> %s",
1315 field_str(stats, ST_F_AGENT_STATUS));
1316
1317 if (stats[ST_F_AGENT_CODE].type)
1318 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1319
1320 if (stats[ST_F_AGENT_DURATION].type)
1321 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1322
1323 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1324
1325 if (*field_str(stats, ST_F_LAST_AGT)) {
1326 chunk_appendf(out, ": ");
1327 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1328 chunk_htmlencode(out, &src);
1329 }
1330 chunk_appendf(out, "</div></u>");
1331 }
1332 else if (stats[ST_F_CHECK_STATUS].type) {
1333 chunk_appendf(out,
1334 "</td><td class=ac><u> %s",
1335 field_str(stats, ST_F_CHECK_STATUS));
1336
1337 if (stats[ST_F_CHECK_CODE].type)
1338 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1339
1340 if (stats[ST_F_CHECK_DURATION].type)
1341 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1342
1343 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1344
1345 if (*field_str(stats, ST_F_LAST_CHK)) {
1346 chunk_appendf(out, ": ");
1347 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1348 chunk_htmlencode(out, &src);
1349 }
1350 chunk_appendf(out, "</div></u>");
1351 }
1352 else
1353 chunk_appendf(out, "</td><td>");
1354
1355 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001356 /* weight / uweight */
1357 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001358 /* act, bck */
1359 "<td class=ac>%s</td><td class=ac>%s</td>"
1360 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001361 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001362 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1363 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1364
1365 /* check failures: unique, fatal, down time */
1366 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1367 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1368 }
1369 else if (stats[ST_F_CHKFAIL].type) {
1370 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1371
1372 if (stats[ST_F_HANAFAIL].type)
1373 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1374
1375 chunk_appendf(out,
1376 "<div class=tips>Failed Health Checks%s</div></u></td>"
1377 "<td>%lld</td><td>%s</td>"
1378 "",
1379 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1380 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1381 }
1382 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1383 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1384 chunk_appendf(out,
1385 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1386 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1387 }
1388 else
1389 chunk_appendf(out, "<td colspan=3></td>");
1390
1391 /* throttle */
1392 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001393 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001394 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001395 chunk_appendf(out, "<td class=ac>-</td>");
1396
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001397 if (flags & STAT_SHMODULES) {
1398 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001399 chunk_appendf(out, "<td>");
1400
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001401 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001402 chunk_appendf(out,
1403 "<u>%s<div class=tips><table class=det>",
1404 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001405 for (j = 0; j < mod->stats_count; ++j) {
1406 chunk_appendf(out,
1407 "<tr><th>%s</th><td>%s</td></tr>",
1408 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1409 ++i;
1410 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001411 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001412 } else {
1413 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001414 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001415
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001416 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001417 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001418 }
1419
1420 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001421 }
1422 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1423 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001424 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001425 /* Column sub-heading for Enable or Disable server */
1426 chunk_appendf(out, "<td></td>");
1427 }
1428 chunk_appendf(out,
1429 "<td class=ac>"
1430 /* name */
1431 "%s<a name=\"%s/Backend\"></a>"
1432 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1433 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001434 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001435 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1436
Willy Tarreau708c4162019-10-09 10:19:16 +02001437 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001438 /* balancing */
1439 chunk_appendf(out, "<div class=tips>balancing: %s",
1440 field_str(stats, ST_F_ALGO));
1441
1442 /* cookie */
1443 if (stats[ST_F_COOKIE].type) {
1444 chunk_appendf(out, ", cookie: '");
1445 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1446 chunk_htmlencode(out, &src);
1447 chunk_appendf(out, "'");
1448 }
1449 chunk_appendf(out, "</div>");
1450 }
1451
1452 chunk_appendf(out,
1453 "%s</td>"
1454 /* queue : current, max */
1455 "<td>%s</td><td>%s</td><td></td>"
1456 /* sessions rate : current, max, limit */
1457 "<td>%s</td><td>%s</td><td></td>"
1458 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001459 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001460 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1461 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1462
1463 chunk_appendf(out,
1464 /* sessions: current, max, limit, total */
1465 "<td>%s</td><td>%s</td><td>%s</td>"
1466 "<td><u>%s<div class=tips><table class=det>"
1467 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1468 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001469 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 +01001470 U2H(stats[ST_F_STOT].u.u64),
1471 U2H(stats[ST_F_STOT].u.u64));
1472
1473 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1474 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1475 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001476 "<tr><th>New connections:</th><td>%s</td></tr>"
1477 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001478 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1479 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1480 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1481 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1482 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1483 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1484 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1485 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001486 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1487 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001488 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001489 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001490 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001491 U2H(stats[ST_F_CONNECT].u.u64),
1492 U2H(stats[ST_F_REUSE].u.u64),
1493 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1494 (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 +01001495 U2H(stats[ST_F_REQ_TOT].u.u64),
1496 U2H(stats[ST_F_HRSP_1XX].u.u64),
1497 U2H(stats[ST_F_HRSP_2XX].u.u64),
1498 U2H(stats[ST_F_COMP_RSP].u.u64),
1499 stats[ST_F_HRSP_2XX].u.u64 ?
1500 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1501 U2H(stats[ST_F_HRSP_3XX].u.u64),
1502 U2H(stats[ST_F_HRSP_4XX].u.u64),
1503 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001504 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001505 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1506 U2H(stats[ST_F_CACHE_HITS].u.u64),
1507 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1508 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001509 U2H(stats[ST_F_WREW].u.u64),
1510 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001511 }
1512
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001513 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1514 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1515 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1516 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1517 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001518 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001519 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1520 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1521 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1522 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001523
1524 chunk_appendf(out,
1525 "</table></div></u></td>"
1526 /* sessions: lbtot, last */
1527 "<td>%s</td><td>%s</td>"
1528 /* bytes: in */
1529 "<td>%s</td>"
1530 "",
1531 U2H(stats[ST_F_LBTOT].u.u64),
1532 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1533 U2H(stats[ST_F_BIN].u.u64));
1534
1535 chunk_appendf(out,
1536 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1537 "<td>%s%s<div class=tips><table class=det>"
1538 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1539 "<tr><th>Compression in:</th><td>%s</td></tr>"
1540 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1541 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1542 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1543 "</table></div>%s</td>",
1544 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1545 U2H(stats[ST_F_BOUT].u.u64),
1546 U2H(stats[ST_F_BOUT].u.u64),
1547 U2H(stats[ST_F_COMP_IN].u.u64),
1548 U2H(stats[ST_F_COMP_OUT].u.u64),
1549 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1550 U2H(stats[ST_F_COMP_BYP].u.u64),
1551 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1552 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,
1553 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1554
1555 chunk_appendf(out,
1556 /* denied: req, resp */
1557 "<td>%s</td><td>%s</td>"
1558 /* errors : request, connect */
1559 "<td></td><td>%s</td>"
1560 /* errors : response */
1561 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1562 /* warnings: retries, redispatches */
1563 "<td>%lld</td><td>%lld</td>"
1564 /* backend status: reflect backend status (up/down): we display UP
1565 * if the backend has known working servers or if it has no server at
1566 * all (eg: for stats). Then we display the total weight, number of
1567 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001568 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001569 "<td class=ac>%d</td><td class=ac>%d</td>"
1570 "",
1571 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1572 U2H(stats[ST_F_ECON].u.u64),
1573 U2H(stats[ST_F_ERESP].u.u64),
1574 (long long)stats[ST_F_CLI_ABRT].u.u64,
1575 (long long)stats[ST_F_SRV_ABRT].u.u64,
1576 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1577 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1578 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 +02001579 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001580 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1581
1582 chunk_appendf(out,
1583 /* rest of backend: nothing, down transitions, total downtime, throttle */
1584 "<td class=ac>&nbsp;</td><td>%d</td>"
1585 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001586 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001587 stats[ST_F_CHKDOWN].u.u32,
1588 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001589
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001590 if (flags & STAT_SHMODULES) {
1591 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001592 chunk_appendf(out, "<td>");
1593
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001594 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001595 chunk_appendf(out,
1596 "<u>%s<div class=tips><table class=det>",
1597 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001598 for (j = 0; j < mod->stats_count; ++j) {
1599 chunk_appendf(out,
1600 "<tr><th>%s</th><td>%s</td></tr>",
1601 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1602 ++i;
1603 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001604 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001605 } else {
1606 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001607 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001608
1609 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001610 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001611 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001612
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001613 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001614 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001615
William Lallemand74c24fb2016-11-21 17:18:36 +01001616 return 1;
1617}
1618
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001619int stats_dump_one_line(const struct field *stats, size_t stats_count,
1620 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001621{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001622 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001623 int ret;
1624
Willy Tarreau91cefca2022-05-03 17:08:29 +02001625 if (ctx->flags & STAT_FMT_HTML)
1626 ret = stats_dump_fields_html(&trash, stats, ctx->flags);
1627 else if (ctx->flags & STAT_FMT_TYPED)
1628 ret = stats_dump_fields_typed(&trash, stats, stats_count, ctx->flags, ctx->domain);
1629 else if (ctx->flags & STAT_FMT_JSON)
1630 ret = stats_dump_fields_json(&trash, stats, stats_count, ctx->flags, ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001631 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02001632 ret = stats_dump_fields_csv(&trash, stats, stats_count, ctx->flags, ctx->domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001633
1634 if (ret)
Willy Tarreau91cefca2022-05-03 17:08:29 +02001635 ctx->flags |= STAT_STARTED;
Simon Horman05ee2132017-01-04 09:37:25 +01001636
1637 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001638}
1639
William Dauchy0ef54392021-01-17 18:27:45 +01001640/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001641 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1642 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1643 * this value, or if the selected field is not implemented for frontends, the
1644 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001645 */
William Dauchy0ef54392021-01-17 18:27:45 +01001646int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1647 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001648{
William Dauchy0ef54392021-01-17 18:27:45 +01001649 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001650
William Lallemand74c24fb2016-11-21 17:18:36 +01001651 if (len < ST_F_TOTAL_FIELDS)
1652 return 0;
1653
William Dauchy0ef54392021-01-17 18:27:45 +01001654 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001655 struct field metric = { 0 };
1656
William Dauchy0ef54392021-01-17 18:27:45 +01001657 switch (current_field) {
1658 case ST_F_PXNAME:
1659 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1660 break;
1661 case ST_F_SVNAME:
1662 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1663 break;
1664 case ST_F_MODE:
1665 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1666 break;
1667 case ST_F_SCUR:
1668 metric = mkf_u32(0, px->feconn);
1669 break;
1670 case ST_F_SMAX:
1671 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1672 break;
1673 case ST_F_SLIM:
1674 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1675 break;
1676 case ST_F_STOT:
1677 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1678 break;
1679 case ST_F_BIN:
1680 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1681 break;
1682 case ST_F_BOUT:
1683 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1684 break;
1685 case ST_F_DREQ:
1686 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1687 break;
1688 case ST_F_DRESP:
1689 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1690 break;
1691 case ST_F_EREQ:
1692 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1693 break;
1694 case ST_F_DCON:
1695 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1696 break;
1697 case ST_F_DSES:
1698 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1699 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001700 case ST_F_STATUS: {
1701 const char *state;
1702
1703 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1704 state = "STOP";
1705 else if (px->flags & PR_FL_PAUSED)
1706 state = "PAUSED";
1707 else
1708 state = "OPEN";
1709 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001710 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001711 }
William Dauchy0ef54392021-01-17 18:27:45 +01001712 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001713 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001714 break;
1715 case ST_F_IID:
1716 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1717 break;
1718 case ST_F_SID:
1719 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1720 break;
1721 case ST_F_TYPE:
1722 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1723 break;
1724 case ST_F_RATE:
1725 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1726 break;
1727 case ST_F_RATE_LIM:
1728 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1729 break;
1730 case ST_F_RATE_MAX:
1731 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1732 break;
1733 case ST_F_WREW:
1734 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1735 break;
1736 case ST_F_EINT:
1737 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1738 break;
1739 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001740 if (px->mode == PR_MODE_HTTP)
1741 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001742 break;
1743 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001744 if (px->mode == PR_MODE_HTTP)
1745 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001746 break;
1747 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001748 if (px->mode == PR_MODE_HTTP)
1749 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001750 break;
1751 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001752 if (px->mode == PR_MODE_HTTP)
1753 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001754 break;
1755 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001756 if (px->mode == PR_MODE_HTTP)
1757 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001758 break;
1759 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001760 if (px->mode == PR_MODE_HTTP)
1761 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001762 break;
1763 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001764 if (px->mode == PR_MODE_HTTP)
1765 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001766 break;
1767 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001768 if (px->mode == PR_MODE_HTTP)
1769 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001770 break;
1771 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001772 if (px->mode == PR_MODE_HTTP)
1773 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001774 break;
1775 case ST_F_REQ_RATE:
1776 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1777 break;
1778 case ST_F_REQ_RATE_MAX:
1779 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1780 break;
1781 case ST_F_REQ_TOT:
1782 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1783 break;
1784 case ST_F_COMP_IN:
1785 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1786 break;
1787 case ST_F_COMP_OUT:
1788 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1789 break;
1790 case ST_F_COMP_BYP:
1791 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1792 break;
1793 case ST_F_COMP_RSP:
1794 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1795 break;
1796 case ST_F_CONN_RATE:
1797 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1798 break;
1799 case ST_F_CONN_RATE_MAX:
1800 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1801 break;
1802 case ST_F_CONN_TOT:
1803 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1804 break;
1805 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001806 /* not used for frontends. If a specific metric
1807 * is requested, return an error. Otherwise continue.
1808 */
1809 if (selected_field != NULL)
1810 return 0;
1811 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001812 }
1813 stats[current_field] = metric;
1814 if (selected_field != NULL)
1815 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001816 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001817 return 1;
1818}
1819
1820/* Dumps a frontend's line to the trash for the current proxy <px> and uses
Willy Tarreaucaff6312022-05-27 10:17:46 +02001821 * the state from stream connector <sc>. The caller is responsible for clearing
William Lallemand74c24fb2016-11-21 17:18:36 +01001822 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1823 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001824static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001825{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001826 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001827 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001828 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1829 struct stats_module *mod;
1830 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001831
1832 if (!(px->cap & PR_CAP_FE))
1833 return 0;
1834
Willy Tarreau91cefca2022-05-03 17:08:29 +02001835 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001836 return 0;
1837
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001838 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1839
William Dauchy0ef54392021-01-17 18:27:45 +01001840 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001841 return 0;
1842
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001843 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1844 void *counters;
1845
1846 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1847 stats_count += mod->stats_count;
1848 continue;
1849 }
1850
1851 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1852 mod->fill_stats(counters, stats + stats_count);
1853 stats_count += mod->stats_count;
1854 }
1855
1856 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001857}
1858
William Dauchy655e14e2021-02-14 23:22:54 +01001859/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1860 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1861 * this length is less then this value, the function returns 0, otherwise, it
1862 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1863 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001864 */
1865int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001866 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001867{
William Dauchy655e14e2021-02-14 23:22:54 +01001868 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001869 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001870
1871 if (len < ST_F_TOTAL_FIELDS)
1872 return 0;
1873
1874 if (!l->counters)
1875 return 0;
1876
1877 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001878
William Dauchy655e14e2021-02-14 23:22:54 +01001879 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1880 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001881
William Dauchy655e14e2021-02-14 23:22:54 +01001882 switch (current_field) {
1883 case ST_F_PXNAME:
1884 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1885 break;
1886 case ST_F_SVNAME:
1887 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1888 break;
1889 case ST_F_MODE:
1890 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1891 break;
1892 case ST_F_SCUR:
1893 metric = mkf_u32(0, l->nbconn);
1894 break;
1895 case ST_F_SMAX:
1896 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1897 break;
1898 case ST_F_SLIM:
1899 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1900 break;
1901 case ST_F_STOT:
1902 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1903 break;
1904 case ST_F_BIN:
1905 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1906 break;
1907 case ST_F_BOUT:
1908 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1909 break;
1910 case ST_F_DREQ:
1911 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1912 break;
1913 case ST_F_DRESP:
1914 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1915 break;
1916 case ST_F_EREQ:
1917 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1918 break;
1919 case ST_F_DCON:
1920 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1921 break;
1922 case ST_F_DSES:
1923 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1924 break;
1925 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001926 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001927 break;
1928 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001929 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01001930 break;
1931 case ST_F_IID:
1932 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1933 break;
1934 case ST_F_SID:
1935 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1936 break;
1937 case ST_F_TYPE:
1938 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1939 break;
1940 case ST_F_WREW:
1941 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1942 break;
1943 case ST_F_EINT:
1944 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1945 break;
1946 case ST_F_ADDR:
1947 if (flags & STAT_SHLGNDS) {
1948 char str[INET6_ADDRSTRLEN];
1949 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001950
William Dauchy655e14e2021-02-14 23:22:54 +01001951 port = get_host_port(&l->rx.addr);
1952 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1953 case AF_INET:
1954 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1955 chunk_appendf(out, "%s:%d", str, port);
1956 break;
1957 case AF_INET6:
1958 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1959 chunk_appendf(out, "[%s]:%d", str, port);
1960 break;
1961 case AF_UNIX:
1962 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1963 break;
1964 case -1:
1965 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1966 chunk_strcat(out, strerror(errno));
1967 break;
1968 default: /* address family not supported */
1969 break;
1970 }
1971 }
1972 break;
1973 default:
1974 /* not used for listen. If a specific metric
1975 * is requested, return an error. Otherwise continue.
1976 */
1977 if (selected_field != NULL)
1978 return 0;
1979 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001980 }
William Dauchy655e14e2021-02-14 23:22:54 +01001981 stats[current_field] = metric;
1982 if (selected_field != NULL)
1983 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001984 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001985 return 1;
1986}
1987
1988/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreaucaff6312022-05-27 10:17:46 +02001989 * from stream connector <sc>. The caller is responsible for clearing the trash
Willy Tarreau43241ff2019-10-09 11:27:51 +02001990 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001991 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001992static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001993{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001994 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001995 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001996 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1997 struct stats_module *mod;
1998 size_t stats_count = ST_F_TOTAL_FIELDS;
1999
2000 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002001
Willy Tarreau91cefca2022-05-03 17:08:29 +02002002 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002003 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002004 return 0;
2005
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002006 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2007 void *counters;
2008
2009 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2010 stats_count += mod->stats_count;
2011 continue;
2012 }
2013
2014 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2015 mod->fill_stats(counters, stats + stats_count);
2016 stats_count += mod->stats_count;
2017 }
2018
2019 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002020}
2021
2022enum srv_stats_state {
2023 SRV_STATS_STATE_DOWN = 0,
2024 SRV_STATS_STATE_DOWN_AGENT,
2025 SRV_STATS_STATE_GOING_UP,
2026 SRV_STATS_STATE_UP_GOING_DOWN,
2027 SRV_STATS_STATE_UP,
2028 SRV_STATS_STATE_NOLB_GOING_DOWN,
2029 SRV_STATS_STATE_NOLB,
2030 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2031 SRV_STATS_STATE_DRAIN,
2032 SRV_STATS_STATE_DRAIN_AGENT,
2033 SRV_STATS_STATE_NO_CHECK,
2034
2035 SRV_STATS_STATE_COUNT, /* Must be last */
2036};
2037
2038static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2039 [SRV_STATS_STATE_DOWN] = "DOWN",
2040 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2041 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2042 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2043 [SRV_STATS_STATE_UP] = "UP",
2044 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2045 [SRV_STATS_STATE_NOLB] = "NOLB",
2046 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2047 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2048 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2049 [SRV_STATS_STATE_NO_CHECK] = "no check"
2050};
2051
William Dauchyd3a9a492021-01-25 17:29:03 +01002052/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002053 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002054static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2055 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002056{
Emeric Brun52a91d32017-08-31 14:41:55 +02002057 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002058 if ((ref->check.state & CHK_ST_ENABLED) &&
2059 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002060 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002061 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002062 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002063 }
2064
Emeric Brun52a91d32017-08-31 14:41:55 +02002065 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002066 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002067 *state = SRV_STATS_STATE_DRAIN_AGENT;
2068 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2069 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002070 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002071 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002072 }
2073
William Dauchyd3a9a492021-01-25 17:29:03 +01002074 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2075 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002076 }
2077 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002078 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002079 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2080 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002081 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002082 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002083 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002084 }
2085 }
2086 else { /* stopped */
2087 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002088 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002089 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002090 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002091 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002092 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002093 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002094 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002095 }
2096 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002097}
William Lallemand74c24fb2016-11-21 17:18:36 +01002098
William Dauchyd3a9a492021-01-25 17:29:03 +01002099/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2100 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2101 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2102 * this value, or if the selected field is not implemented for servers, the
2103 * function returns 0, otherwise, it returns 1. <flags> can take the value
2104 * STAT_SHLGNDS.
2105 */
2106int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2107 struct field *stats, int len,
2108 enum stat_field *selected_field)
2109{
2110 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2111 struct server *via = sv->track ? sv->track : sv;
2112 struct server *ref = via;
2113 enum srv_stats_state state = 0;
2114 char str[INET6_ADDRSTRLEN];
2115 struct buffer *out = get_trash_chunk();
2116 char *fld_status;
2117 long long srv_samples_counter;
2118 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002119
William Dauchyd3a9a492021-01-25 17:29:03 +01002120 if (len < ST_F_TOTAL_FIELDS)
2121 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002122
William Dauchyd3a9a492021-01-25 17:29:03 +01002123 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002124
William Dauchyd3a9a492021-01-25 17:29:03 +01002125 /* compute state for later use */
2126 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2127 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2128 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2129 /* we have "via" which is the tracked server as described in the configuration,
2130 * and "ref" which is the checked server and the end of the chain.
2131 */
2132 while (ref->track)
2133 ref = ref->track;
2134 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002135 }
2136
William Dauchyd3a9a492021-01-25 17:29:03 +01002137 /* compue time values for later use */
2138 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2139 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2140 *selected_field == ST_F_TTIME) {
2141 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2142 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2143 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002144 }
2145
William Dauchyd3a9a492021-01-25 17:29:03 +01002146 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2147 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002148
William Dauchyd3a9a492021-01-25 17:29:03 +01002149 switch (current_field) {
2150 case ST_F_PXNAME:
2151 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2152 break;
2153 case ST_F_SVNAME:
2154 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2155 break;
2156 case ST_F_MODE:
2157 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002158 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002159 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002160 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002161 break;
2162 case ST_F_QMAX:
2163 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2164 break;
2165 case ST_F_SCUR:
2166 metric = mkf_u32(0, sv->cur_sess);
2167 break;
2168 case ST_F_SMAX:
2169 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2170 break;
2171 case ST_F_SLIM:
2172 if (sv->maxconn)
2173 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2174 break;
2175 case ST_F_SRV_ICUR:
2176 metric = mkf_u32(0, sv->curr_idle_conns);
2177 break;
2178 case ST_F_SRV_ILIM:
2179 if (sv->max_idle_conns != -1)
2180 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2181 break;
2182 case ST_F_STOT:
2183 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2184 break;
2185 case ST_F_BIN:
2186 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2187 break;
2188 case ST_F_BOUT:
2189 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2190 break;
2191 case ST_F_DRESP:
2192 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2193 break;
2194 case ST_F_ECON:
2195 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2196 break;
2197 case ST_F_ERESP:
2198 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2199 break;
2200 case ST_F_WRETR:
2201 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2202 break;
2203 case ST_F_WREDIS:
2204 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2205 break;
2206 case ST_F_WREW:
2207 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2208 break;
2209 case ST_F_EINT:
2210 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2211 break;
2212 case ST_F_CONNECT:
2213 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2214 break;
2215 case ST_F_REUSE:
2216 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2217 break;
2218 case ST_F_IDLE_CONN_CUR:
2219 metric = mkf_u32(0, sv->curr_idle_nb);
2220 break;
2221 case ST_F_SAFE_CONN_CUR:
2222 metric = mkf_u32(0, sv->curr_safe_nb);
2223 break;
2224 case ST_F_USED_CONN_CUR:
2225 metric = mkf_u32(0, sv->curr_used_conns);
2226 break;
2227 case ST_F_NEED_CONN_EST:
2228 metric = mkf_u32(0, sv->est_need_conns);
2229 break;
2230 case ST_F_STATUS:
2231 fld_status = chunk_newstr(out);
2232 if (sv->cur_admin & SRV_ADMF_RMAINT)
2233 chunk_appendf(out, "MAINT (resolution)");
2234 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2235 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2236 else if (sv->cur_admin & SRV_ADMF_MAINT)
2237 chunk_appendf(out, "MAINT");
2238 else
2239 chunk_appendf(out,
2240 srv_hlt_st[state],
2241 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2242 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002243
William Dauchyd3a9a492021-01-25 17:29:03 +01002244 metric = mkf_str(FO_STATUS, fld_status);
2245 break;
2246 case ST_F_LASTCHG:
2247 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2248 break;
2249 case ST_F_WEIGHT:
2250 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2251 break;
2252 case ST_F_UWEIGHT:
2253 metric = mkf_u32(FN_AVG, sv->uweight);
2254 break;
2255 case ST_F_ACT:
2256 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2257 break;
2258 case ST_F_BCK:
2259 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2260 break;
2261 case ST_F_CHKFAIL:
2262 if (sv->check.state & CHK_ST_ENABLED)
2263 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2264 break;
2265 case ST_F_CHKDOWN:
2266 if (sv->check.state & CHK_ST_ENABLED)
2267 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2268 break;
2269 case ST_F_DOWNTIME:
2270 if (sv->check.state & CHK_ST_ENABLED)
2271 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2272 break;
2273 case ST_F_QLIMIT:
2274 if (sv->maxqueue)
2275 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2276 break;
2277 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002278 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002279 break;
2280 case ST_F_IID:
2281 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2282 break;
2283 case ST_F_SID:
2284 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2285 break;
2286 case ST_F_THROTTLE:
2287 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2288 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2289 break;
2290 case ST_F_LBTOT:
2291 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2292 break;
2293 case ST_F_TRACKED:
2294 if (sv->track) {
2295 char *fld_track = chunk_newstr(out);
2296 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2297 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2298 }
2299 break;
2300 case ST_F_TYPE:
2301 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2302 break;
2303 case ST_F_RATE:
2304 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2305 break;
2306 case ST_F_RATE_MAX:
2307 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2308 break;
2309 case ST_F_CHECK_STATUS:
2310 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2311 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002312
William Dauchyd3a9a492021-01-25 17:29:03 +01002313 fld_chksts = chunk_newstr(out);
2314 chunk_strcat(out, "* "); // for check in progress
2315 chunk_strcat(out, get_check_status_info(sv->check.status));
2316 if (!(sv->check.state & CHK_ST_INPROGRESS))
2317 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002318 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002319 }
2320 break;
2321 case ST_F_CHECK_CODE:
2322 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2323 sv->check.status >= HCHK_STATUS_L57DATA)
2324 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2325 break;
2326 case ST_F_CHECK_DURATION:
2327 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2328 sv->check.status >= HCHK_STATUS_CHECKED)
2329 metric = mkf_u64(FN_DURATION, sv->check.duration);
2330 break;
2331 case ST_F_CHECK_DESC:
2332 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2333 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2334 break;
2335 case ST_F_LAST_CHK:
2336 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2337 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2338 break;
2339 case ST_F_CHECK_RISE:
2340 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2341 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2342 break;
2343 case ST_F_CHECK_FALL:
2344 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2345 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2346 break;
2347 case ST_F_CHECK_HEALTH:
2348 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2349 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2350 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002351 case ST_F_AGENT_STATUS:
2352 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2353 const char *fld_chksts;
2354
2355 fld_chksts = chunk_newstr(out);
2356 chunk_strcat(out, "* "); // for check in progress
2357 chunk_strcat(out, get_check_status_info(sv->agent.status));
2358 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2359 fld_chksts += 2; // skip "* "
2360 metric = mkf_str(FN_OUTPUT, fld_chksts);
2361 }
2362 break;
2363 case ST_F_AGENT_CODE:
2364 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2365 (sv->agent.status >= HCHK_STATUS_L57DATA))
2366 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2367 break;
2368 case ST_F_AGENT_DURATION:
2369 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2370 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2371 break;
2372 case ST_F_AGENT_DESC:
2373 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2374 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2375 break;
2376 case ST_F_LAST_AGT:
2377 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2378 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2379 break;
2380 case ST_F_AGENT_RISE:
2381 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2382 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2383 break;
2384 case ST_F_AGENT_FALL:
2385 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2386 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2387 break;
2388 case ST_F_AGENT_HEALTH:
2389 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2390 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2391 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002392 case ST_F_REQ_TOT:
2393 if (px->mode == PR_MODE_HTTP)
2394 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2395 break;
2396 case ST_F_HRSP_1XX:
2397 if (px->mode == PR_MODE_HTTP)
2398 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2399 break;
2400 case ST_F_HRSP_2XX:
2401 if (px->mode == PR_MODE_HTTP)
2402 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2403 break;
2404 case ST_F_HRSP_3XX:
2405 if (px->mode == PR_MODE_HTTP)
2406 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2407 break;
2408 case ST_F_HRSP_4XX:
2409 if (px->mode == PR_MODE_HTTP)
2410 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2411 break;
2412 case ST_F_HRSP_5XX:
2413 if (px->mode == PR_MODE_HTTP)
2414 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2415 break;
2416 case ST_F_HRSP_OTHER:
2417 if (px->mode == PR_MODE_HTTP)
2418 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2419 break;
2420 case ST_F_HANAFAIL:
2421 if (ref->observe)
2422 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2423 break;
2424 case ST_F_CLI_ABRT:
2425 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2426 break;
2427 case ST_F_SRV_ABRT:
2428 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2429 break;
2430 case ST_F_LASTSESS:
2431 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2432 break;
2433 case ST_F_QTIME:
2434 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2435 break;
2436 case ST_F_CTIME:
2437 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2438 break;
2439 case ST_F_RTIME:
2440 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2441 break;
2442 case ST_F_TTIME:
2443 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2444 break;
2445 case ST_F_QT_MAX:
2446 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2447 break;
2448 case ST_F_CT_MAX:
2449 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2450 break;
2451 case ST_F_RT_MAX:
2452 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2453 break;
2454 case ST_F_TT_MAX:
2455 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2456 break;
2457 case ST_F_ADDR:
2458 if (flags & STAT_SHLGNDS) {
2459 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2460 case AF_INET:
2461 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2462 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2463 break;
2464 case AF_INET6:
2465 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2466 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2467 break;
2468 case AF_UNIX:
2469 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2470 break;
2471 case -1:
2472 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2473 chunk_strcat(out, strerror(errno));
2474 break;
2475 default: /* address family not supported */
2476 break;
2477 }
2478 }
2479 break;
2480 case ST_F_COOKIE:
2481 if (flags & STAT_SHLGNDS && sv->cookie)
2482 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2483 break;
2484 default:
2485 /* not used for servers. If a specific metric
2486 * is requested, return an error. Otherwise continue.
2487 */
2488 if (selected_field != NULL)
2489 return 0;
2490 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002491 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002492 stats[current_field] = metric;
2493 if (selected_field != NULL)
2494 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002495 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002496 return 1;
2497}
2498
2499/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreaucaff6312022-05-27 10:17:46 +02002500 * from stream connector <sc>, and server state <state>. The caller is
Willy Tarreau43241ff2019-10-09 11:27:51 +02002501 * responsible for clearing the trash if needed. Returns non-zero if it emits
2502 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002503 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002504static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002505{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002506 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002507 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002508 struct stats_module *mod;
2509 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2510 size_t stats_count = ST_F_TOTAL_FIELDS;
2511
2512 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002513
Willy Tarreau91cefca2022-05-03 17:08:29 +02002514 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002515 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002516 return 0;
2517
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002518 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2519 void *counters;
2520
2521 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2522 continue;
2523
2524 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2525 stats_count += mod->stats_count;
2526 continue;
2527 }
2528
2529 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2530 mod->fill_stats(counters, stats + stats_count);
2531 stats_count += mod->stats_count;
2532 }
2533
2534 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002535}
2536
William Dauchyda3b4662021-01-25 17:29:01 +01002537/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002538 */
William Dauchyda3b4662021-01-25 17:29:01 +01002539static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002540{
William Dauchyda3b4662021-01-25 17:29:01 +01002541 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002542 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002543
William Dauchyda3b4662021-01-25 17:29:01 +01002544 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002545 for (srv = px->srv; srv; srv = srv->next) {
2546 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002547 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002548 if (srv_currently_usable(srv) &&
2549 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002550 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002551 }
William Dauchyda3b4662021-01-25 17:29:01 +01002552 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002553 }
2554
Willy Tarreaubd715102020-10-23 22:44:30 +02002555 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2556 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002557 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002558 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2559
William Dauchyda3b4662021-01-25 17:29:01 +01002560 /* use tmp variable then assign result to make gcc happy */
2561 *nbup = nbup_tmp;
2562 *nbsrv = nbsrv_tmp;
2563 *totuw = totuw_tmp;
2564}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002565
William Dauchyda3b4662021-01-25 17:29:01 +01002566/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2567 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2568 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2569 * this value, or if the selected field is not implemented for backends, the
2570 * function returns 0, otherwise, it returns 1. <flags> can take the value
2571 * STAT_SHLGNDS.
2572 */
2573int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2574 enum stat_field *selected_field)
2575{
2576 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2577 long long be_samples_counter;
2578 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2579 struct buffer *out = get_trash_chunk();
2580 int nbup, nbsrv, totuw;
2581 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002582
William Dauchyda3b4662021-01-25 17:29:01 +01002583 if (len < ST_F_TOTAL_FIELDS)
2584 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002585
William Dauchyda3b4662021-01-25 17:29:01 +01002586 nbup = nbsrv = totuw = 0;
2587 /* some srv values compute for later if we either select all fields or
2588 * need them for one of the mentioned ones */
2589 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2590 *selected_field == ST_F_UWEIGHT)
2591 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002592
William Dauchyda3b4662021-01-25 17:29:01 +01002593 /* same here but specific to time fields */
2594 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2595 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2596 *selected_field == ST_F_TTIME) {
2597 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2598 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2599 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002600 }
2601
William Dauchyda3b4662021-01-25 17:29:01 +01002602 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2603 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002604
William Dauchyda3b4662021-01-25 17:29:01 +01002605 switch (current_field) {
2606 case ST_F_PXNAME:
2607 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2608 break;
2609 case ST_F_SVNAME:
2610 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2611 break;
2612 case ST_F_MODE:
2613 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2614 break;
2615 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002616 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002617 break;
2618 case ST_F_QMAX:
2619 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2620 break;
2621 case ST_F_SCUR:
2622 metric = mkf_u32(0, px->beconn);
2623 break;
2624 case ST_F_SMAX:
2625 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2626 break;
2627 case ST_F_SLIM:
2628 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2629 break;
2630 case ST_F_STOT:
2631 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2632 break;
2633 case ST_F_BIN:
2634 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2635 break;
2636 case ST_F_BOUT:
2637 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2638 break;
2639 case ST_F_DREQ:
2640 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2641 break;
2642 case ST_F_DRESP:
2643 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2644 break;
2645 case ST_F_ECON:
2646 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2647 break;
2648 case ST_F_ERESP:
2649 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2650 break;
2651 case ST_F_WRETR:
2652 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2653 break;
2654 case ST_F_WREDIS:
2655 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2656 break;
2657 case ST_F_WREW:
2658 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2659 break;
2660 case ST_F_EINT:
2661 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2662 break;
2663 case ST_F_CONNECT:
2664 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2665 break;
2666 case ST_F_REUSE:
2667 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2668 break;
2669 case ST_F_STATUS:
2670 fld = chunk_newstr(out);
2671 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2672 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2673 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2674 metric = mkf_str(FO_STATUS, fld);
2675 break;
William Dauchy42d7c402021-11-07 10:18:47 +01002676 case ST_F_AGG_SRV_CHECK_STATUS:
2677 metric = mkf_u32(FN_GAUGE, 0);
2678 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002679 case ST_F_WEIGHT:
2680 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2681 break;
2682 case ST_F_UWEIGHT:
2683 metric = mkf_u32(FN_AVG, totuw);
2684 break;
2685 case ST_F_ACT:
2686 metric = mkf_u32(0, px->srv_act);
2687 break;
2688 case ST_F_BCK:
2689 metric = mkf_u32(0, px->srv_bck);
2690 break;
2691 case ST_F_CHKDOWN:
2692 metric = mkf_u64(FN_COUNTER, px->down_trans);
2693 break;
2694 case ST_F_LASTCHG:
2695 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2696 break;
2697 case ST_F_DOWNTIME:
2698 if (px->srv)
2699 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2700 break;
2701 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002702 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002703 break;
2704 case ST_F_IID:
2705 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2706 break;
2707 case ST_F_SID:
2708 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2709 break;
2710 case ST_F_LBTOT:
2711 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2712 break;
2713 case ST_F_TYPE:
2714 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2715 break;
2716 case ST_F_RATE:
2717 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2718 break;
2719 case ST_F_RATE_MAX:
2720 metric = mkf_u32(0, px->be_counters.sps_max);
2721 break;
2722 case ST_F_COOKIE:
2723 if (flags & STAT_SHLGNDS && px->cookie_name)
2724 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2725 break;
2726 case ST_F_ALGO:
2727 if (flags & STAT_SHLGNDS)
2728 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2729 break;
2730 case ST_F_REQ_TOT:
2731 if (px->mode == PR_MODE_HTTP)
2732 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2733 break;
2734 case ST_F_HRSP_1XX:
2735 if (px->mode == PR_MODE_HTTP)
2736 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2737 break;
2738 case ST_F_HRSP_2XX:
2739 if (px->mode == PR_MODE_HTTP)
2740 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2741 break;
2742 case ST_F_HRSP_3XX:
2743 if (px->mode == PR_MODE_HTTP)
2744 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2745 break;
2746 case ST_F_HRSP_4XX:
2747 if (px->mode == PR_MODE_HTTP)
2748 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2749 break;
2750 case ST_F_HRSP_5XX:
2751 if (px->mode == PR_MODE_HTTP)
2752 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2753 break;
2754 case ST_F_HRSP_OTHER:
2755 if (px->mode == PR_MODE_HTTP)
2756 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2757 break;
2758 case ST_F_CACHE_LOOKUPS:
2759 if (px->mode == PR_MODE_HTTP)
2760 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2761 break;
2762 case ST_F_CACHE_HITS:
2763 if (px->mode == PR_MODE_HTTP)
2764 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2765 break;
2766 case ST_F_CLI_ABRT:
2767 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2768 break;
2769 case ST_F_SRV_ABRT:
2770 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2771 break;
2772 case ST_F_COMP_IN:
2773 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2774 break;
2775 case ST_F_COMP_OUT:
2776 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2777 break;
2778 case ST_F_COMP_BYP:
2779 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2780 break;
2781 case ST_F_COMP_RSP:
2782 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2783 break;
2784 case ST_F_LASTSESS:
2785 metric = mkf_s32(FN_AGE, be_lastsession(px));
2786 break;
2787 case ST_F_QTIME:
2788 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2789 break;
2790 case ST_F_CTIME:
2791 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2792 break;
2793 case ST_F_RTIME:
2794 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2795 break;
2796 case ST_F_TTIME:
2797 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2798 break;
2799 case ST_F_QT_MAX:
2800 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2801 break;
2802 case ST_F_CT_MAX:
2803 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2804 break;
2805 case ST_F_RT_MAX:
2806 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2807 break;
2808 case ST_F_TT_MAX:
2809 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2810 break;
2811 default:
2812 /* not used for backends. If a specific metric
2813 * is requested, return an error. Otherwise continue.
2814 */
2815 if (selected_field != NULL)
2816 return 0;
2817 continue;
2818 }
2819 stats[current_field] = metric;
2820 if (selected_field != NULL)
2821 break;
2822 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002823 return 1;
2824}
2825
2826/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002827 * interface <si>. The caller is responsible for clearing the trash if needed.
2828 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002829 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002830static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002831{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002832 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002833 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002834 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2835 struct stats_module *mod;
2836 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002837
2838 if (!(px->cap & PR_CAP_BE))
2839 return 0;
2840
Willy Tarreau91cefca2022-05-03 17:08:29 +02002841 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002842 return 0;
2843
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002844 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2845
Willy Tarreau91cefca2022-05-03 17:08:29 +02002846 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002847 return 0;
2848
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002849 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2850 struct extra_counters *counters;
2851
2852 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2853 continue;
2854
2855 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2856 stats_count += mod->stats_count;
2857 continue;
2858 }
2859
2860 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2861 mod->fill_stats(counters, stats + stats_count);
2862 stats_count += mod->stats_count;
2863 }
2864
2865 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002866}
2867
2868/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02002869 * stream connector <sc> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01002870 * for clearing the trash if needed.
2871 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002872static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002873{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002874 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002875 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01002876 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002877 struct stats_module *mod;
2878 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002879
Willy Tarreau91cefca2022-05-03 17:08:29 +02002880 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002881 /* A form to enable/disable this proxy servers */
2882
Willy Tarreau91cefca2022-05-03 17:08:29 +02002883 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002884 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02002885 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02002886 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002887
William Lallemand74c24fb2016-11-21 17:18:36 +01002888 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002889 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
2890 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002891 }
2892
2893 chunk_appendf(&trash,
2894 "<form method=\"post\">");
2895 }
2896
2897 /* print a new table */
2898 chunk_appendf(&trash,
2899 "<table class=\"tbl\" width=\"100%%\">\n"
2900 "<tr class=\"titre\">"
2901 "<th class=\"pxname\" width=\"10%%\">");
2902
2903 chunk_appendf(&trash,
2904 "<a name=\"%s\"></a>%s"
2905 "<a class=px href=\"#%s\">%s</a>",
2906 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02002907 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002908 px->id, px->id);
2909
Willy Tarreau91cefca2022-05-03 17:08:29 +02002910 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002911 /* cap, mode, id */
2912 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2913 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2914 px->uuid);
2915 chunk_appendf(&trash, "</div>");
2916 }
2917
2918 chunk_appendf(&trash,
2919 "%s</th>"
2920 "<th class=\"%s\" width=\"90%%\">%s</th>"
2921 "</tr>\n"
2922 "</table>\n"
2923 "<table class=\"tbl\" width=\"100%%\">\n"
2924 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02002925 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002926 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2927
Willy Tarreau91cefca2022-05-03 17:08:29 +02002928 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002929 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002930 if ((px->cap & PR_CAP_BE) && px->srv)
2931 chunk_appendf(&trash,
2932 "<th rowspan=2 width=1><input type=\"checkbox\" "
2933 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2934 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2935 px->id,
2936 px->id);
2937 else
2938 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002939 }
2940
2941 chunk_appendf(&trash,
2942 "<th rowspan=2></th>"
2943 "<th colspan=3>Queue</th>"
2944 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2945 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2946 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002947 "<th colspan=9>Server</th>");
2948
Willy Tarreau91cefca2022-05-03 17:08:29 +02002949 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002950 // calculate the count of module for colspan attribute
2951 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2952 ++stats_module_len;
2953 }
2954 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2955 stats_module_len);
2956 }
2957
2958 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002959 "</tr>\n"
2960 "<tr class=\"titre\">"
2961 "<th>Cur</th><th>Max</th><th>Limit</th>"
2962 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2963 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2964 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2965 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2966 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2967 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002968 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002969
Willy Tarreau91cefca2022-05-03 17:08:29 +02002970 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002971 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2972 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2973 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002974 }
2975
2976 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002977}
2978
2979/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02002980 * stream connector <sc>. The caller is responsible for clearing the trash if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002981 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002982static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002983{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002984 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002985 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01002986
William Lallemand74c24fb2016-11-21 17:18:36 +01002987 chunk_appendf(&trash, "</table>");
2988
Willy Tarreau91cefca2022-05-03 17:08:29 +02002989 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002990 /* close the form used to enable/disable this proxy servers */
2991 chunk_appendf(&trash,
2992 "Choose the action to perform on the checked servers : "
2993 "<select name=action>"
2994 "<option value=\"\"></option>"
2995 "<option value=\"ready\">Set state to READY</option>"
2996 "<option value=\"drain\">Set state to DRAIN</option>"
2997 "<option value=\"maint\">Set state to MAINT</option>"
2998 "<option value=\"dhlth\">Health: disable checks</option>"
2999 "<option value=\"ehlth\">Health: enable checks</option>"
3000 "<option value=\"hrunn\">Health: force UP</option>"
3001 "<option value=\"hnolb\">Health: force NOLB</option>"
3002 "<option value=\"hdown\">Health: force DOWN</option>"
3003 "<option value=\"dagent\">Agent: disable checks</option>"
3004 "<option value=\"eagent\">Agent: enable checks</option>"
3005 "<option value=\"arunn\">Agent: force UP</option>"
3006 "<option value=\"adown\">Agent: force DOWN</option>"
3007 "<option value=\"shutdown\">Kill Sessions</option>"
3008 "</select>"
3009 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3010 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3011 "</form>",
3012 px->uuid);
3013 }
3014
3015 chunk_appendf(&trash, "<p>\n");
3016}
3017
3018/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003019 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003020 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3021 * buffer space, or non-zero if everything completed. This function is used
3022 * both by the CLI and the HTTP entry points, and is able to dump the output
3023 * in HTML or CSV formats. If the later, <uri> must be NULL.
3024 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003025int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003026 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003027{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003028 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003029 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003030 struct stream *s = __sc_strm(sc);
3031 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003032 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3033 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01003034
3035 chunk_reset(&trash);
3036
Willy Tarreau91cefca2022-05-03 17:08:29 +02003037 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003038 case STAT_PX_ST_INIT:
3039 /* we are on a new proxy */
3040 if (uri && uri->scope) {
3041 /* we have a limited scope, we have to check the proxy name */
3042 struct stat_scope *scope;
3043 int len;
3044
3045 len = strlen(px->id);
3046 scope = uri->scope;
3047
3048 while (scope) {
3049 /* match exact proxy name */
3050 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3051 break;
3052
3053 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003054 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003055 break;
3056 scope = scope->next;
3057 }
3058
3059 /* proxy name not found : don't dump anything */
3060 if (scope == NULL)
3061 return 1;
3062 }
3063
3064 /* if the user has requested a limited output and the proxy
3065 * name does not match, skip it.
3066 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003067 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003068 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003069
Willy Tarreau91cefca2022-05-03 17:08:29 +02003070 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003071 return 1;
3072 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003073
Willy Tarreau91cefca2022-05-03 17:08:29 +02003074 if ((ctx->flags & STAT_BOUND) &&
3075 (ctx->iid != -1) &&
3076 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003077 return 1;
3078
Willy Tarreau91cefca2022-05-03 17:08:29 +02003079 ctx->px_st = STAT_PX_ST_TH;
William Lallemand74c24fb2016-11-21 17:18:36 +01003080 /* fall through */
3081
3082 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003083 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003084 stats_dump_html_px_hdr(sc, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003085 if (!stats_putchk(rep, htx, &trash))
3086 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003087 }
3088
Willy Tarreau91cefca2022-05-03 17:08:29 +02003089 ctx->px_st = STAT_PX_ST_FE;
William Lallemand74c24fb2016-11-21 17:18:36 +01003090 /* fall through */
3091
3092 case STAT_PX_ST_FE:
3093 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003094 if (stats_dump_fe_stats(sc, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003095 if (!stats_putchk(rep, htx, &trash))
3096 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003097 }
3098
Willy Tarreau91cefca2022-05-03 17:08:29 +02003099 ctx->obj2 = px->conf.listeners.n;
3100 ctx->px_st = STAT_PX_ST_LI;
William Lallemand74c24fb2016-11-21 17:18:36 +01003101 /* fall through */
3102
3103 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003104 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003105 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003106 if (htx) {
3107 if (htx_almost_full(htx))
3108 goto full;
3109 }
3110 else {
3111 if (buffer_almost_full(&rep->buf))
3112 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003113 }
3114
Willy Tarreau91cefca2022-05-03 17:08:29 +02003115 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003116 if (!l->counters)
3117 continue;
3118
Willy Tarreau91cefca2022-05-03 17:08:29 +02003119 if (ctx->flags & STAT_BOUND) {
3120 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003121 break;
3122
Willy Tarreau91cefca2022-05-03 17:08:29 +02003123 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003124 continue;
3125 }
3126
3127 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003128 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003129 if (!stats_putchk(rep, htx, &trash))
3130 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003131 }
3132 }
3133
Willy Tarreau91cefca2022-05-03 17:08:29 +02003134 ctx->obj2 = px->srv; /* may be NULL */
3135 ctx->px_st = STAT_PX_ST_SV;
William Lallemand74c24fb2016-11-21 17:18:36 +01003136 /* fall through */
3137
3138 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003139 /* obj2 points to servers list as initialized above.
3140 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003141 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003142 * Temporarily increment its refcount to prevent its
3143 * anticipated cleaning. Call free_server to release it.
3144 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003145 for (; ctx->obj2 != NULL;
3146 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003147
Willy Tarreau91cefca2022-05-03 17:08:29 +02003148 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003149 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003150
Christopher Fauletef779222018-10-31 08:47:01 +01003151 if (htx) {
3152 if (htx_almost_full(htx))
3153 goto full;
3154 }
3155 else {
3156 if (buffer_almost_full(&rep->buf))
3157 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003158 }
3159
Willy Tarreau91cefca2022-05-03 17:08:29 +02003160 if (ctx->flags & STAT_BOUND) {
3161 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003162 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003163 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003164 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003165
Willy Tarreau91cefca2022-05-03 17:08:29 +02003166 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003167 continue;
3168 }
3169
Willy Tarreau3e320362020-10-23 17:28:57 +02003170 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003171 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003172 sv->cur_admin & SRV_ADMF_MAINT) {
3173 continue;
3174 }
3175
William Lallemand74c24fb2016-11-21 17:18:36 +01003176 svs = sv;
3177 while (svs->track)
3178 svs = svs->track;
3179
3180 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003181 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003182 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3183 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003184 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3185 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3186 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3187 continue;
3188 }
3189
Willy Tarreaucaff6312022-05-27 10:17:46 +02003190 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003191 if (!stats_putchk(rep, htx, &trash))
3192 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003193 }
3194 } /* for sv */
3195
Willy Tarreau91cefca2022-05-03 17:08:29 +02003196 ctx->px_st = STAT_PX_ST_BE;
William Lallemand74c24fb2016-11-21 17:18:36 +01003197 /* fall through */
3198
3199 case STAT_PX_ST_BE:
3200 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003201 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003202 if (!stats_putchk(rep, htx, &trash))
3203 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003204 }
3205
Willy Tarreau91cefca2022-05-03 17:08:29 +02003206 ctx->px_st = STAT_PX_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003207 /* fall through */
3208
3209 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003210 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003211 stats_dump_html_px_end(sc, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003212 if (!stats_putchk(rep, htx, &trash))
3213 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003214 }
3215
Willy Tarreau91cefca2022-05-03 17:08:29 +02003216 ctx->px_st = STAT_PX_ST_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003217 /* fall through */
3218
3219 case STAT_PX_ST_FIN:
3220 return 1;
3221
3222 default:
3223 /* unknown state, we should put an abort() here ! */
3224 return 1;
3225 }
Christopher Fauletef779222018-10-31 08:47:01 +01003226
3227 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003228 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003229 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003230}
3231
3232/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3233 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3234 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003235static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003236{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003237 struct show_stat_ctx *ctx = appctx->svcctx;
3238
William Lallemand74c24fb2016-11-21 17:18:36 +01003239 /* WARNING! This must fit in the first buffer !!! */
3240 chunk_appendf(&trash,
3241 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3242 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3243 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003244 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003245 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3246 "<style type=\"text/css\"><!--\n"
3247 "body {"
3248 " font-family: arial, helvetica, sans-serif;"
3249 " font-size: 12px;"
3250 " font-weight: normal;"
3251 " color: black;"
3252 " background: white;"
3253 "}\n"
3254 "th,td {"
3255 " font-size: 10px;"
3256 "}\n"
3257 "h1 {"
3258 " font-size: x-large;"
3259 " margin-bottom: 0.5em;"
3260 "}\n"
3261 "h2 {"
3262 " font-family: helvetica, arial;"
3263 " font-size: x-large;"
3264 " font-weight: bold;"
3265 " font-style: italic;"
3266 " color: #6020a0;"
3267 " margin-top: 0em;"
3268 " margin-bottom: 0em;"
3269 "}\n"
3270 "h3 {"
3271 " font-family: helvetica, arial;"
3272 " font-size: 16px;"
3273 " font-weight: bold;"
3274 " color: #b00040;"
3275 " background: #e8e8d0;"
3276 " margin-top: 0em;"
3277 " margin-bottom: 0em;"
3278 "}\n"
3279 "li {"
3280 " margin-top: 0.25em;"
3281 " margin-right: 2em;"
3282 "}\n"
3283 ".hr {margin-top: 0.25em;"
3284 " border-color: black;"
3285 " border-bottom-style: solid;"
3286 "}\n"
3287 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3288 ".total {background: #20D0D0;color: #ffff80;}\n"
3289 ".frontend {background: #e8e8d0;}\n"
3290 ".socket {background: #d0d0d0;}\n"
3291 ".backend {background: #e8e8d0;}\n"
3292 ".active_down {background: #ff9090;}\n"
3293 ".active_going_up {background: #ffd020;}\n"
3294 ".active_going_down {background: #ffffa0;}\n"
3295 ".active_up {background: #c0ffc0;}\n"
3296 ".active_nolb {background: #20a0ff;}\n"
3297 ".active_draining {background: #20a0FF;}\n"
3298 ".active_no_check {background: #e0e0e0;}\n"
3299 ".backup_down {background: #ff9090;}\n"
3300 ".backup_going_up {background: #ff80ff;}\n"
3301 ".backup_going_down {background: #c060ff;}\n"
3302 ".backup_up {background: #b0d0ff;}\n"
3303 ".backup_nolb {background: #90b0e0;}\n"
3304 ".backup_draining {background: #cc9900;}\n"
3305 ".backup_no_check {background: #e0e0e0;}\n"
3306 ".maintain {background: #c07820;}\n"
3307 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3308 "\n"
3309 "a.px:link {color: #ffff40; text-decoration: none;}"
3310 "a.px:visited {color: #ffff40; text-decoration: none;}"
3311 "a.px:hover {color: #ffffff; text-decoration: none;}"
3312 "a.lfsb:link {color: #000000; text-decoration: none;}"
3313 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3314 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3315 "\n"
3316 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3317 "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"
3318 "table.tbl td.ac { text-align: center;}\n"
3319 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3320 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3321 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3322 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3323 "\n"
3324 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3325 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3326 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3327 "table.det { border-collapse: collapse; border-style: none; }\n"
3328 "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"
3329 "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"
3330 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003331 "div.tips {\n"
3332 " display:block;\n"
3333 " visibility:hidden;\n"
3334 " z-index:2147483647;\n"
3335 " position:absolute;\n"
3336 " padding:2px 4px 3px;\n"
3337 " background:#f0f060; color:#000000;\n"
3338 " border:1px solid #7040c0;\n"
3339 " white-space:nowrap;\n"
3340 " font-style:normal;font-size:11px;font-weight:normal;\n"
3341 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3342 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3343 "}\n"
3344 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003345 "@media (prefers-color-scheme: dark) {\n"
3346 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3347 " h1 { color: #a265e0!important; }\n"
3348 " h2 { color: #a265e0; }\n"
3349 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3350 " a { color: #3391ff; }\n"
3351 " input { background-color: #2f3437; }\n"
3352 " .hr { border-color: #8c8273; }\n"
3353 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3354 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003355 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003356 " .backend {background: #2f3437;}\n"
3357 " .active_down {background: #760000;}\n"
3358 " .active_going_up {background: #b99200;}\n"
3359 " .active_going_down {background: #6c6c00;}\n"
3360 " .active_up {background: #165900;}\n"
3361 " .active_nolb {background: #006ab9;}\n"
3362 " .active_draining {background: #006ab9;}\n"
3363 " .active_no_check {background: #2a2d2f;}\n"
3364 " .backup_down {background: #760000;}\n"
3365 " .backup_going_up {background: #7f007f;}\n"
3366 " .backup_going_down {background: #580092;}\n"
3367 " .backup_up {background: #2e3234;}\n"
3368 " .backup_nolb {background: #1e3c6a;}\n"
3369 " .backup_draining {background: #a37a00;}\n"
3370 " .backup_no_check {background: #2a2d2f;}\n"
3371 " .maintain {background: #9a601a;}\n"
3372 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3373 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3374 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3375 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3376 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3377 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3378 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003379 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003380 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3381 " table.tbl th { border-color: #808080; }\n"
3382 " table.tbl td { border-color: #808080; }\n"
3383 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3384 " div.tips {\n"
3385 " background:#8e8e0d;\n"
3386 " color:#e8e6e3;\n"
3387 " border-color: #4e2c86;\n"
3388 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3389 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3390 " box-shadow: #60686c 2px 2px 3px;\n"
3391 " }\n"
3392 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003393 "-->\n"
3394 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003395 (ctx->flags & STAT_SHNODE) ? " on " : "",
3396 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003397 );
3398}
3399
3400/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02003401 * stream connector <sc> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01003402 * for clearing the trash if needed.
3403 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003404static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003405{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003406 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003407 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003408 unsigned int up = (now.tv_sec - start_date.tv_sec);
3409 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003410 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau1713c032019-05-23 12:23:55 +02003411 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3412
3413 /* Turn the bytes per second to bits per second and take care of the
3414 * usual ethernet overhead in order to help figure how far we are from
3415 * interface saturation since it's the only case which usually matters.
3416 * For this we count the total size of an Ethernet frame on the wire
3417 * including preamble and IFG (1538) for the largest TCP segment it
3418 * transports (1448 with TCP timestamps). This is not valid for smaller
3419 * packets (under-estimated), but it gives a reasonably accurate
3420 * estimation of how far we are from uplink saturation.
3421 */
3422 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003423
3424 /* WARNING! this has to fit the first packet too.
3425 * We are around 3.5 kB, add adding entries will
3426 * become tricky if we want to support 4kB buffers !
3427 */
3428 chunk_appendf(&trash,
3429 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3430 PRODUCT_NAME "%s</a></h1>\n"
3431 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3432 "<hr width=\"100%%\" class=\"hr\">\n"
3433 "<h3>&gt; General process information</h3>\n"
3434 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003435 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003436 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3437 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3438 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003439 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003440 "Running tasks: %d/%d; idle = %d %%<br>\n"
3441 "</td><td align=\"center\" nowrap>\n"
3442 "<table class=\"lgd\"><tr>\n"
3443 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3444 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3445 "</tr><tr>\n"
3446 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3447 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3448 "</tr><tr>\n"
3449 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3450 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3451 "</tr><tr>\n"
3452 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3453 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3454 "</tr><tr>\n"
3455 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3456 "</tr><tr>\n"
3457 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3458 "</tr></table>\n"
3459 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3460 "</td>"
3461 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3462 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3463 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003464 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3465 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3466 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3467 (ctx->flags & STAT_SHDESC) ? ": " : "",
3468 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003469 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003470 up / 86400, (up % 86400) / 3600,
3471 (up % 3600) / 60, (up % 60),
3472 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3473 global.rlimit_memmax ? " MB" : "",
3474 global.rlimit_nofile,
3475 global.maxsock, global.maxconn, global.maxpipes,
3476 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003477 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3478 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003479 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003480 );
3481
Willy Tarreau91cefca2022-05-03 17:08:29 +02003482 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3483 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3484 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003485
3486 chunk_appendf(&trash,
3487 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003488 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003489 STAT_SCOPE_TXT_MAXLEN);
3490
Willy Tarreau91cefca2022-05-03 17:08:29 +02003491 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003492 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003493 if (ctx->scope_len) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003494 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003495 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3496 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003497 }
3498
Willy Tarreau91cefca2022-05-03 17:08:29 +02003499 if (ctx->flags & STAT_HIDE_DOWN)
William Lallemand74c24fb2016-11-21 17:18:36 +01003500 chunk_appendf(&trash,
3501 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3502 uri->uri_prefix,
3503 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003504 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003505 scope_txt);
3506 else
3507 chunk_appendf(&trash,
3508 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3509 uri->uri_prefix,
3510 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003511 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003512 scope_txt);
3513
3514 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003515 if (ctx->flags & STAT_NO_REFRESH)
William Lallemand74c24fb2016-11-21 17:18:36 +01003516 chunk_appendf(&trash,
3517 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3518 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003519 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003520 "",
3521 scope_txt);
3522 else
3523 chunk_appendf(&trash,
3524 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3525 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003526 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003527 ";norefresh",
3528 scope_txt);
3529 }
3530
3531 chunk_appendf(&trash,
3532 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3533 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003534 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3535 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003536 scope_txt);
3537
3538 chunk_appendf(&trash,
3539 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3540 uri->uri_prefix,
3541 (uri->refresh > 0) ? ";norefresh" : "",
3542 scope_txt);
3543
3544 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003545 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3546 uri->uri_prefix,
3547 (uri->refresh > 0) ? ";norefresh" : "",
3548 scope_txt, uri->uri_prefix);
3549
3550 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003551 "</ul></td>"
3552 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3553 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3554 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3555 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3556 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3557 "</ul>"
3558 "</td>"
3559 "</tr></table>\n"
3560 ""
3561 );
3562
Willy Tarreau91cefca2022-05-03 17:08:29 +02003563 if (ctx->st_code) {
3564 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003565 case STAT_STATUS_DONE:
3566 chunk_appendf(&trash,
3567 "<p><div class=active_up>"
3568 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3569 "Action processed successfully."
3570 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003571 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3572 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003573 scope_txt);
3574 break;
3575 case STAT_STATUS_NONE:
3576 chunk_appendf(&trash,
3577 "<p><div class=active_going_down>"
3578 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3579 "Nothing has changed."
3580 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003581 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3582 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003583 scope_txt);
3584 break;
3585 case STAT_STATUS_PART:
3586 chunk_appendf(&trash,
3587 "<p><div class=active_going_down>"
3588 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3589 "Action partially processed.<br>"
3590 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3591 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003592 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3593 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003594 scope_txt);
3595 break;
3596 case STAT_STATUS_ERRP:
3597 chunk_appendf(&trash,
3598 "<p><div class=active_down>"
3599 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3600 "Action not processed because of invalid parameters."
3601 "<ul>"
3602 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003603 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003604 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3605 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3606 "</ul>"
3607 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003608 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3609 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003610 scope_txt);
3611 break;
3612 case STAT_STATUS_EXCD:
3613 chunk_appendf(&trash,
3614 "<p><div class=active_down>"
3615 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3616 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3617 "You should retry with less servers at a time.</b>"
3618 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003619 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3620 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003621 scope_txt);
3622 break;
3623 case STAT_STATUS_DENY:
3624 chunk_appendf(&trash,
3625 "<p><div class=active_down>"
3626 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3627 "<b>Action denied.</b>"
3628 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003629 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3630 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003631 scope_txt);
3632 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003633 case STAT_STATUS_IVAL:
3634 chunk_appendf(&trash,
3635 "<p><div class=active_down>"
3636 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3637 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3638 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003639 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3640 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003641 scope_txt);
3642 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003643 default:
3644 chunk_appendf(&trash,
3645 "<p><div class=active_no_check>"
3646 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3647 "Unexpected result."
3648 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003649 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3650 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003651 scope_txt);
3652 }
3653 chunk_appendf(&trash, "<p>\n");
3654 }
3655}
3656
3657/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3658 * for clearing the trash if needed.
3659 */
3660static void stats_dump_html_end()
3661{
3662 chunk_appendf(&trash, "</body></html>\n");
3663}
3664
Simon Horman05ee2132017-01-04 09:37:25 +01003665/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3666 * for clearing it if needed.
3667 */
3668static void stats_dump_json_header()
3669{
3670 chunk_strcat(&trash, "[");
3671}
3672
3673
3674/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3675 * for clearing the trash if needed.
3676 */
3677static void stats_dump_json_end()
3678{
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02003679 chunk_strcat(&trash, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003680}
3681
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003682/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3683 * a pointer to the current server/listener.
3684 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003685static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003686 struct htx *htx,
3687 struct uri_auth *uri)
3688{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003689 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003690 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003691 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003692 struct proxy *px;
3693
3694 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003695 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003696 if (htx) {
3697 if (htx_almost_full(htx))
3698 goto full;
3699 }
3700 else {
3701 if (buffer_almost_full(&rep->buf))
3702 goto full;
3703 }
3704
Willy Tarreau91cefca2022-05-03 17:08:29 +02003705 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003706 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003707 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003708 * This change allows retrieving stats from "old" proxies after a reload.
3709 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003710 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003711 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003712 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003713 return 0;
3714 }
3715
Willy Tarreau91cefca2022-05-03 17:08:29 +02003716 ctx->obj1 = px->next;
3717 ctx->px_st = STAT_PX_ST_INIT;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003718 }
3719
3720 return 1;
3721
3722 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003723 sc_need_room(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003724 return 0;
3725}
3726
Willy Tarreau4596fe22022-05-17 19:07:51 +02003727/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003728 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3729 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3730 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3731 * and the stream must be closed, or -1 in case of any error. This function is
3732 * used by both the CLI and the HTTP handlers.
3733 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003734static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003735 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003736{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003737 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003738 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003739 struct channel *rep = sc_ic(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003740 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003741
3742 chunk_reset(&trash);
3743
Willy Tarreau41f88522022-05-03 18:39:27 +02003744 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003745 case STAT_STATE_INIT:
3746 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
William Lallemand74c24fb2016-11-21 17:18:36 +01003747 /* fall through */
3748
Willy Tarreau6ef16482022-05-06 18:07:53 +02003749 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003750 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003751 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003752 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Faulet6338a082019-09-09 15:50:54 +02003753 stats_dump_json_schema(&trash);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003754 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003755 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003756 else if (!(ctx->flags & STAT_FMT_TYPED))
3757 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003758
Christopher Fauletef779222018-10-31 08:47:01 +01003759 if (!stats_putchk(rep, htx, &trash))
3760 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003761
Willy Tarreau91cefca2022-05-03 17:08:29 +02003762 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003763 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003764 return 1;
3765 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003766 ctx->state = STAT_STATE_INFO;
William Lallemand74c24fb2016-11-21 17:18:36 +01003767 /* fall through */
3768
Willy Tarreau6ef16482022-05-06 18:07:53 +02003769 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003770 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003771 stats_dump_html_info(sc, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003772 if (!stats_putchk(rep, htx, &trash))
3773 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003774 }
3775
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003776 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003777 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003778
Willy Tarreau91cefca2022-05-03 17:08:29 +02003779 ctx->px_st = STAT_PX_ST_INIT;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003780 ctx->state = STAT_STATE_LIST;
William Lallemand74c24fb2016-11-21 17:18:36 +01003781 /* fall through */
3782
Willy Tarreau6ef16482022-05-06 18:07:53 +02003783 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003784 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003785 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003786 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003787 stat_count[domain],
3788 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003789 return 0;
3790 }
3791 break;
3792
3793 case STATS_DOMAIN_PROXY:
3794 default:
3795 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003796 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003797 return 0;
3798 break;
3799 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003800
Willy Tarreau6ef16482022-05-06 18:07:53 +02003801 ctx->state = STAT_STATE_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003802 /* fall through */
3803
Willy Tarreau6ef16482022-05-06 18:07:53 +02003804 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003805 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3806 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003807 stats_dump_html_end();
3808 else
3809 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003810 if (!stats_putchk(rep, htx, &trash))
3811 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003812 }
3813
Willy Tarreau6ef16482022-05-06 18:07:53 +02003814 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003815 /* fall through */
3816
Willy Tarreau6ef16482022-05-06 18:07:53 +02003817 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003818 return 1;
3819
3820 default:
3821 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02003822 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003823 return -1;
3824 }
Christopher Fauletef779222018-10-31 08:47:01 +01003825
3826 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003827 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003828 return 0;
3829
William Lallemand74c24fb2016-11-21 17:18:36 +01003830}
3831
3832/* We reached the stats page through a POST request. The appctx is
3833 * expected to have already been allocated by the caller.
3834 * Parse the posted data and enable/disable servers if necessary.
3835 * Returns 1 if request was parsed or zero if it needs more data.
3836 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003837static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01003838{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003839 struct stream *s = __sc_strm(sc);
3840 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003841 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003842
3843 struct proxy *px = NULL;
3844 struct server *sv = NULL;
3845
3846 char key[LINESIZE];
3847 int action = ST_ADM_ACTION_NONE;
3848 int reprocess = 0;
3849
3850 int total_servers = 0;
3851 int altered_servers = 0;
3852
3853 char *first_param, *cur_param, *next_param, *end_params;
3854 char *st_cur_param = NULL;
3855 char *st_next_param = NULL;
3856
Christopher Fauleta3618372018-12-13 21:59:56 +01003857 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003858
Christopher Fauletb7f88902019-07-15 21:56:43 +02003859 struct htx *htx = htxbuf(&s->req.buf);
3860 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003861
Christopher Fauletb7f88902019-07-15 21:56:43 +02003862 /* we need more data */
3863 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3864 /* check if we can receive more */
3865 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003866 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02003867 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003868 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003869 goto wait;
3870 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003871
Christopher Fauletb7f88902019-07-15 21:56:43 +02003872 /* The request was fully received. Copy data */
3873 blk = htx_get_head_blk(htx);
3874 while (blk) {
3875 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003876
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003877 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003878 break;
3879 if (type == HTX_BLK_DATA) {
3880 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003881
Christopher Fauletb7f88902019-07-15 21:56:43 +02003882 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003883 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003884 goto out;
3885 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003886 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003887 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003888 }
3889
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003890 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003891 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003892 cur_param = next_param = end_params;
3893 *end_params = '\0';
3894
Willy Tarreau91cefca2022-05-03 17:08:29 +02003895 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01003896
3897 /*
3898 * Parse the parameters in reverse order to only store the last value.
3899 * From the html form, the backend and the action are at the end.
3900 */
3901 while (cur_param > first_param) {
3902 char *value;
3903 int poffset, plen;
3904
3905 cur_param--;
3906
3907 if ((*cur_param == '&') || (cur_param == first_param)) {
3908 reprocess_servers:
3909 /* Parse the key */
3910 poffset = (cur_param != first_param ? 1 : 0);
3911 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3912 if ((plen > 0) && (plen <= sizeof(key))) {
3913 strncpy(key, cur_param + poffset, plen);
3914 key[plen - 1] = '\0';
3915 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003916 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003917 goto out;
3918 }
3919
3920 /* Parse the value */
3921 value = key;
3922 while (*value != '\0' && *value != '=') {
3923 value++;
3924 }
3925 if (*value == '=') {
3926 /* Ok, a value is found, we can mark the end of the key */
3927 *value++ = '\0';
3928 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003929 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003930 break;
3931
3932 /* Now we can check the key to see what to do */
3933 if (!px && (strcmp(key, "b") == 0)) {
3934 if ((px = proxy_be_by_name(value)) == NULL) {
3935 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003936 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003937 goto out;
3938 }
3939 }
3940 else if (!action && (strcmp(key, "action") == 0)) {
3941 if (strcmp(value, "ready") == 0) {
3942 action = ST_ADM_ACTION_READY;
3943 }
3944 else if (strcmp(value, "drain") == 0) {
3945 action = ST_ADM_ACTION_DRAIN;
3946 }
3947 else if (strcmp(value, "maint") == 0) {
3948 action = ST_ADM_ACTION_MAINT;
3949 }
3950 else if (strcmp(value, "shutdown") == 0) {
3951 action = ST_ADM_ACTION_SHUTDOWN;
3952 }
3953 else if (strcmp(value, "dhlth") == 0) {
3954 action = ST_ADM_ACTION_DHLTH;
3955 }
3956 else if (strcmp(value, "ehlth") == 0) {
3957 action = ST_ADM_ACTION_EHLTH;
3958 }
3959 else if (strcmp(value, "hrunn") == 0) {
3960 action = ST_ADM_ACTION_HRUNN;
3961 }
3962 else if (strcmp(value, "hnolb") == 0) {
3963 action = ST_ADM_ACTION_HNOLB;
3964 }
3965 else if (strcmp(value, "hdown") == 0) {
3966 action = ST_ADM_ACTION_HDOWN;
3967 }
3968 else if (strcmp(value, "dagent") == 0) {
3969 action = ST_ADM_ACTION_DAGENT;
3970 }
3971 else if (strcmp(value, "eagent") == 0) {
3972 action = ST_ADM_ACTION_EAGENT;
3973 }
3974 else if (strcmp(value, "arunn") == 0) {
3975 action = ST_ADM_ACTION_ARUNN;
3976 }
3977 else if (strcmp(value, "adown") == 0) {
3978 action = ST_ADM_ACTION_ADOWN;
3979 }
3980 /* else these are the old supported methods */
3981 else if (strcmp(value, "disable") == 0) {
3982 action = ST_ADM_ACTION_DISABLE;
3983 }
3984 else if (strcmp(value, "enable") == 0) {
3985 action = ST_ADM_ACTION_ENABLE;
3986 }
3987 else if (strcmp(value, "stop") == 0) {
3988 action = ST_ADM_ACTION_STOP;
3989 }
3990 else if (strcmp(value, "start") == 0) {
3991 action = ST_ADM_ACTION_START;
3992 }
3993 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003994 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003995 goto out;
3996 }
3997 }
3998 else if (strcmp(key, "s") == 0) {
3999 if (!(px && action)) {
4000 /*
4001 * Indicates that we'll need to reprocess the parameters
4002 * as soon as backend and action are known
4003 */
4004 if (!reprocess) {
4005 st_cur_param = cur_param;
4006 st_next_param = next_param;
4007 }
4008 reprocess = 1;
4009 }
4010 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004011 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004012 switch (action) {
4013 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004014 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004015 altered_servers++;
4016 total_servers++;
4017 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
4018 }
4019 break;
4020 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004021 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004022 altered_servers++;
4023 total_servers++;
4024 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4025 }
4026 break;
4027 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004028 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004029 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4030 altered_servers++;
4031 total_servers++;
4032 }
4033 break;
4034 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004035 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004036 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4037 altered_servers++;
4038 total_servers++;
4039 }
4040 break;
4041 case ST_ADM_ACTION_DHLTH:
4042 if (sv->check.state & CHK_ST_CONFIGURED) {
4043 sv->check.state &= ~CHK_ST_ENABLED;
4044 altered_servers++;
4045 total_servers++;
4046 }
4047 break;
4048 case ST_ADM_ACTION_EHLTH:
4049 if (sv->check.state & CHK_ST_CONFIGURED) {
4050 sv->check.state |= CHK_ST_ENABLED;
4051 altered_servers++;
4052 total_servers++;
4053 }
4054 break;
4055 case ST_ADM_ACTION_HRUNN:
4056 if (!(sv->track)) {
4057 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004058 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004059 altered_servers++;
4060 total_servers++;
4061 }
4062 break;
4063 case ST_ADM_ACTION_HNOLB:
4064 if (!(sv->track)) {
4065 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004066 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004067 altered_servers++;
4068 total_servers++;
4069 }
4070 break;
4071 case ST_ADM_ACTION_HDOWN:
4072 if (!(sv->track)) {
4073 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004074 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004075 altered_servers++;
4076 total_servers++;
4077 }
4078 break;
4079 case ST_ADM_ACTION_DAGENT:
4080 if (sv->agent.state & CHK_ST_CONFIGURED) {
4081 sv->agent.state &= ~CHK_ST_ENABLED;
4082 altered_servers++;
4083 total_servers++;
4084 }
4085 break;
4086 case ST_ADM_ACTION_EAGENT:
4087 if (sv->agent.state & CHK_ST_CONFIGURED) {
4088 sv->agent.state |= CHK_ST_ENABLED;
4089 altered_servers++;
4090 total_servers++;
4091 }
4092 break;
4093 case ST_ADM_ACTION_ARUNN:
4094 if (sv->agent.state & CHK_ST_ENABLED) {
4095 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004096 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004097 altered_servers++;
4098 total_servers++;
4099 }
4100 break;
4101 case ST_ADM_ACTION_ADOWN:
4102 if (sv->agent.state & CHK_ST_ENABLED) {
4103 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004104 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004105 altered_servers++;
4106 total_servers++;
4107 }
4108 break;
4109 case ST_ADM_ACTION_READY:
4110 srv_adm_set_ready(sv);
4111 altered_servers++;
4112 total_servers++;
4113 break;
4114 case ST_ADM_ACTION_DRAIN:
4115 srv_adm_set_drain(sv);
4116 altered_servers++;
4117 total_servers++;
4118 break;
4119 case ST_ADM_ACTION_MAINT:
4120 srv_adm_set_maint(sv);
4121 altered_servers++;
4122 total_servers++;
4123 break;
4124 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004125 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004126 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004127 altered_servers++;
4128 total_servers++;
4129 }
4130 break;
4131 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004132 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004133 } else {
4134 /* the server name is unknown or ambiguous (duplicate names) */
4135 total_servers++;
4136 }
4137 }
4138 if (reprocess && px && action) {
4139 /* Now, we know the backend and the action chosen by the user.
4140 * We can safely restart from the first server parameter
4141 * to reprocess them
4142 */
4143 cur_param = st_cur_param;
4144 next_param = st_next_param;
4145 reprocess = 0;
4146 goto reprocess_servers;
4147 }
4148
4149 next_param = cur_param;
4150 }
4151 }
4152
4153 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004154 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004155 }
4156 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004157 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004158 }
4159 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004160 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004161 }
4162 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004163 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004164 }
4165 out:
4166 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004167 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004168 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004169 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004170}
4171
4172
Willy Tarreaucaff6312022-05-27 10:17:46 +02004173static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004174{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004175 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004176 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004177 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004178 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004179 struct htx_sl *sl;
4180 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004181
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004182 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);
4183 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4184 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004185 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004186 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004187
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004188 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004189 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004190 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004191 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4192 goto full;
4193 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004194 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004195 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4196 goto full;
4197 }
Christopher Fauletef779222018-10-31 08:47:01 +01004198 else {
4199 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4200 goto full;
4201 }
4202
Willy Tarreau91cefca2022-05-03 17:08:29 +02004203 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004204 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004205 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004206 goto full;
4207 }
4208
Willy Tarreau91cefca2022-05-03 17:08:29 +02004209 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004210 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4211 goto full;
4212 }
4213
4214 if (!htx_add_endof(htx, HTX_BLK_EOH))
4215 goto full;
4216
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004217 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004218 return 1;
4219
4220 full:
4221 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004222 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004223 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004224}
4225
Christopher Fauletef779222018-10-31 08:47:01 +01004226
Willy Tarreaucaff6312022-05-27 10:17:46 +02004227static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004228{
4229 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004230 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004231 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004232 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004233 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004234 struct htx_sl *sl;
4235 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004236
Willy Tarreau91cefca2022-05-03 17:08:29 +02004237 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004238 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004239 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004240 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004241
Christopher Fauletef779222018-10-31 08:47:01 +01004242 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004243 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4244 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004245 }
4246
4247 /* We don't want to land on the posted stats page because a refresh will
4248 * repost the data. We don't want this to happen on accident so we redirect
4249 * the browse to the stats page with a GET.
4250 */
4251 chunk_printf(&trash, "%s;st=%s%s%s%s",
4252 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004253 ((ctx->st_code > STAT_STATUS_INIT) &&
4254 (ctx->st_code < STAT_STATUS_SIZE) &&
4255 stat_status_codes[ctx->st_code]) ?
4256 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004257 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004258 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4259 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004260 scope_txt);
4261
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004262 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4263 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4264 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004265 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004266 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004267
4268 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004269 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4270 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4271 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4272 goto full;
4273
4274 if (!htx_add_endof(htx, HTX_BLK_EOH))
4275 goto full;
4276
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004277 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004278 return 1;
4279
4280full:
4281 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004282 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004283 return 0;
4284}
William Lallemand74c24fb2016-11-21 17:18:36 +01004285
Simon Horman05ee2132017-01-04 09:37:25 +01004286
Willy Tarreau4596fe22022-05-17 19:07:51 +02004287/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004288 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4289 * appctx->st0 contains the operation in progress (dump, done). The handler
4290 * automatically unregisters itself once transfer is complete.
4291 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004292static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004293{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004294 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004295 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004296 struct stream *s = __sc_strm(sc);
4297 struct channel *req = sc_oc(sc);
4298 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004299 struct htx *req_htx, *res_htx;
4300
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004301 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004302 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004303
Christopher Fauletef779222018-10-31 08:47:01 +01004304 res_htx = htx_from_buf(&res->buf);
4305
Willy Tarreaucaff6312022-05-27 10:17:46 +02004306 if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO))
Christopher Fauletef779222018-10-31 08:47:01 +01004307 goto out;
4308
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004309 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004310 if (!b_size(&res->buf)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004311 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004312 goto out;
4313 }
4314
4315 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004316 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4317 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004318
4319 /* all states are processed in sequence */
4320 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004321 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004322 if (s->txn->meth == HTTP_METH_HEAD)
4323 appctx->st0 = STAT_HTTP_DONE;
4324 else
4325 appctx->st0 = STAT_HTTP_DUMP;
4326 }
4327 }
4328
4329 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004330 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004331 appctx->st0 = STAT_HTTP_DONE;
4332 }
4333
4334 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004335 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004336 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004337 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004338 appctx->st0 = STAT_HTTP_DONE;
4339 }
4340
4341 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004342 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004343 appctx->st0 = STAT_HTTP_DONE;
4344 }
4345
4346 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004347 /* no more data are expected. If the response buffer is empty,
4348 * be sure to add something (EOT block in this case) to have
4349 * something to send. It is important to be sure the EOM flags
4350 * will be handled by the endpoint.
4351 */
4352 if (htx_is_empty(res_htx)) {
4353 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004354 sc_need_room(sc);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004355 goto out;
4356 }
4357 channel_add_input(res, 1);
4358 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004359 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3fa5d192022-03-07 15:52:42 +01004360 res->flags |= CF_EOI;
Willy Tarreaud869e132022-05-17 18:05:31 +02004361 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004362 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004363 }
4364
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004365 if (appctx->st0 == STAT_HTTP_END) {
4366 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004367 res->flags |= CF_READ_NULL;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004368 sc_shutr(sc);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004369 }
4370
4371 /* eat the whole request */
4372 if (co_data(req)) {
4373 req_htx = htx_from_buf(&req->buf);
4374 co_htx_skip(req, req_htx, co_data(req));
4375 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004376 }
4377 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004378
Christopher Fauletef779222018-10-31 08:47:01 +01004379 out:
4380 /* we have left the request in the buffer for the case where we
4381 * process a POST, and this automatically re-enables activity on
4382 * read. It's better to indicate that we want to stop reading when
4383 * we're sending, so that we know there's at most one direction
4384 * deciding to wake the applet up. It saves it from looping when
4385 * emitting large blocks into small TCP windows.
4386 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004387 htx_to_buf(res_htx, &res->buf);
4388 if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004389 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004390}
4391
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004392/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004393static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004394 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004395{
4396 int field;
4397
4398 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4399 if (!field_format(info, field))
4400 continue;
4401
Willy Tarreaueaa55372019-10-09 07:39:11 +02004402 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004403 return 0;
4404 if (!stats_emit_raw_data_field(out, &info[field]))
4405 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004406 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4407 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004408 if (!chunk_strcat(out, "\n"))
4409 return 0;
4410 }
4411 return 1;
4412}
4413
4414/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004415static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004416 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004417{
4418 int field;
4419
4420 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4421 if (!field_format(info, field))
4422 continue;
4423
Willy Tarreaueaa55372019-10-09 07:39:11 +02004424 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 +01004425 return 0;
4426 if (!stats_emit_field_tags(out, &info[field], ':'))
4427 return 0;
4428 if (!stats_emit_typed_data_field(out, &info[field]))
4429 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004430 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4431 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004432 if (!chunk_strcat(out, "\n"))
4433 return 0;
4434 }
4435 return 1;
4436}
4437
Willy Tarreau0b26b382021-05-08 07:43:53 +02004438/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4439 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4440 * less then this value, the function returns 0, otherwise, it returns 1. Some
4441 * fields' presence or precision may depend on some of the STAT_* flags present
4442 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004443 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004444int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004445{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004446 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004447 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004448
4449#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004450 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4451 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4452 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004453
Willy Tarreauc5977722021-05-08 08:14:04 +02004454 if (ssl_key_rate < ssl_sess_rate)
4455 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004456#endif
4457
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004458 tv_remain(&start_date, &now, &up);
4459
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004460 if (len < INF_TOTAL_FIELDS)
4461 return 0;
4462
4463 chunk_reset(out);
4464 memset(info, 0, sizeof(*info) * len);
4465
4466 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004467 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004468 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004469 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004470
Yves Lafon95317282018-02-26 11:10:37 +01004471 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004472 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004473 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004474 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4475
4476 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004477 chunk_appendf(out, "%ud %uh%02um%02us", (uint)up.tv_sec / 86400, ((uint)up.tv_sec % 86400) / 3600, ((uint)up.tv_sec % 3600) / 60, ((uint)up.tv_sec % 60));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004478
Willy Tarreaue8abc322021-05-08 07:56:56 +02004479 info[INF_UPTIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, up.tv_sec + up.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, up.tv_sec);
4480 info[INF_START_TIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, start_date.tv_sec + start_date.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, start_date.tv_sec);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004481 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004482 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004483 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004484 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004485 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004486 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004487 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4488 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4489 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4490 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4491 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4492 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4493 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4494 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4495#ifdef USE_OPENSSL
4496 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004497 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4498 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004499#endif
4500 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4501 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4502 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004503 info[INF_CONN_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.conn_per_sec)) : mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004504 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4505 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004506 info[INF_SESS_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.sess_per_sec)) : mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004507 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4508 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4509
4510#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004511 info[INF_SSL_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_sess_rate) : mkf_u32(FN_RATE, ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004512 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4513 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004514 info[INF_SSL_FRONTEND_KEY_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_key_rate) : mkf_u32(0, ssl_key_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004515 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004516 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4517 info[INF_SSL_BACKEND_KEY_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.ssl_be_keys_per_sec)) : mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004518 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4519 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4520 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4521#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004522 info[INF_COMPRESS_BPS_IN] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.comp_bps_in)) : mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
4523 info[INF_COMPRESS_BPS_OUT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.comp_bps_out)) : mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004524 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4525#ifdef USE_ZLIB
4526 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4527 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4528#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004529 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004530 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004531 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004532 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4533 if (global.desc)
4534 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004535 info[INF_STOPPING] = mkf_u32(0, stopping);
4536 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004537 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004538 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004539 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004540 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004541 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004542 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004543 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004544 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004545 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004546 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 +02004547 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004548 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004549
4550 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4551 chunk_appendf(out, "%#x", get_tainted());
4552
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004553 return 1;
4554}
4555
Willy Tarreau4596fe22022-05-17 19:07:51 +02004556/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004557 * It returns 0 as long as it does not complete, non-zero upon completion.
4558 * No state is used.
4559 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004560static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004561{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004562 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004563 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004564
Willy Tarreau91cefca2022-05-03 17:08:29 +02004565 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004566 return 0;
4567
4568 chunk_reset(&trash);
4569
Willy Tarreau91cefca2022-05-03 17:08:29 +02004570 if (ctx->flags & STAT_FMT_TYPED)
4571 stats_dump_typed_info_fields(&trash, info, ctx->flags);
4572 else if (ctx->flags & STAT_FMT_JSON)
4573 stats_dump_json_info_fields(&trash, info, ctx->flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004574 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02004575 stats_dump_info_fields(&trash, info, ctx->flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004576
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004577 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004578 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004579
4580 return 1;
4581}
4582
Willy Tarreau4596fe22022-05-17 19:07:51 +02004583/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004584 * It returns 0 as long as it does not complete, non-zero upon completion.
4585 * No state is used.
4586 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004587 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004588 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4589 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004590static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004591{
4592
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004593 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004594
4595 chunk_strcat(out,
4596 "{"
4597 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4598 "\"oneOf\":["
4599 "{"
4600 "\"title\":\"Info\","
4601 "\"type\":\"array\","
4602 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004603 "\"title\":\"InfoItem\","
4604 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004605 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004606 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4607 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4608 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4609 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4610 "},"
4611 "\"required\":[\"field\",\"processNum\",\"tags\","
4612 "\"value\"]"
4613 "}"
4614 "},"
4615 "{"
4616 "\"title\":\"Stat\","
4617 "\"type\":\"array\","
4618 "\"items\":{"
4619 "\"title\":\"InfoItem\","
4620 "\"type\":\"object\","
4621 "\"properties\":{"
4622 "\"objType\":{"
4623 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4624 "\"Server\",\"Unknown\"]"
4625 "},"
4626 "\"proxyId\":{"
4627 "\"type\":\"integer\","
4628 "\"minimum\":0"
4629 "},"
4630 "\"id\":{"
4631 "\"type\":\"integer\","
4632 "\"minimum\":0"
4633 "},"
4634 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4635 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4636 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4637 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4638 "},"
4639 "\"required\":[\"objType\",\"proxyId\",\"id\","
4640 "\"field\",\"processNum\",\"tags\","
4641 "\"value\"]"
4642 "}"
4643 "},"
4644 "{"
4645 "\"title\":\"Error\","
4646 "\"type\":\"object\","
4647 "\"properties\":{"
4648 "\"errorStr\":{"
4649 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004650 "}"
4651 "},"
4652 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004653 "}"
4654 "],"
4655 "\"definitions\":{"
4656 "\"field\":{"
4657 "\"type\":\"object\","
4658 "\"pos\":{"
4659 "\"type\":\"integer\","
4660 "\"minimum\":0"
4661 "},"
4662 "\"name\":{"
4663 "\"type\":\"string\""
4664 "},"
4665 "\"required\":[\"pos\",\"name\"]"
4666 "},"
4667 "\"processNum\":{"
4668 "\"type\":\"integer\","
4669 "\"minimum\":1"
4670 "},"
4671 "\"tags\":{"
4672 "\"type\":\"object\","
4673 "\"origin\":{"
4674 "\"type\":\"string\","
4675 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4676 "\"Config\",\"Product\",\"Unknown\"]"
4677 "},"
4678 "\"nature\":{"
4679 "\"type\":\"string\","
4680 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4681 "\"Rate\",\"Counter\",\"Duration\","
4682 "\"Age\",\"Time\",\"Name\",\"Output\","
4683 "\"Avg\", \"Unknown\"]"
4684 "},"
4685 "\"scope\":{"
4686 "\"type\":\"string\","
4687 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4688 "\"System\",\"Unknown\"]"
4689 "},"
4690 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4691 "},"
4692 "\"typedValue\":{"
4693 "\"type\":\"object\","
4694 "\"oneOf\":["
4695 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4696 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4697 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4698 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4699 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4700 "],"
4701 "\"definitions\":{"
4702 "\"s32Value\":{"
4703 "\"properties\":{"
4704 "\"type\":{"
4705 "\"type\":\"string\","
4706 "\"enum\":[\"s32\"]"
4707 "},"
4708 "\"value\":{"
4709 "\"type\":\"integer\","
4710 "\"minimum\":-2147483648,"
4711 "\"maximum\":2147483647"
4712 "}"
4713 "},"
4714 "\"required\":[\"type\",\"value\"]"
4715 "},"
4716 "\"s64Value\":{"
4717 "\"properties\":{"
4718 "\"type\":{"
4719 "\"type\":\"string\","
4720 "\"enum\":[\"s64\"]"
4721 "},"
4722 "\"value\":{"
4723 "\"type\":\"integer\","
4724 "\"minimum\":-9007199254740991,"
4725 "\"maximum\":9007199254740991"
4726 "}"
4727 "},"
4728 "\"required\":[\"type\",\"value\"]"
4729 "},"
4730 "\"u32Value\":{"
4731 "\"properties\":{"
4732 "\"type\":{"
4733 "\"type\":\"string\","
4734 "\"enum\":[\"u32\"]"
4735 "},"
4736 "\"value\":{"
4737 "\"type\":\"integer\","
4738 "\"minimum\":0,"
4739 "\"maximum\":4294967295"
4740 "}"
4741 "},"
4742 "\"required\":[\"type\",\"value\"]"
4743 "},"
4744 "\"u64Value\":{"
4745 "\"properties\":{"
4746 "\"type\":{"
4747 "\"type\":\"string\","
4748 "\"enum\":[\"u64\"]"
4749 "},"
4750 "\"value\":{"
4751 "\"type\":\"integer\","
4752 "\"minimum\":0,"
4753 "\"maximum\":9007199254740991"
4754 "}"
4755 "},"
4756 "\"required\":[\"type\",\"value\"]"
4757 "},"
4758 "\"strValue\":{"
4759 "\"properties\":{"
4760 "\"type\":{"
4761 "\"type\":\"string\","
4762 "\"enum\":[\"str\"]"
4763 "},"
4764 "\"value\":{\"type\":\"string\"}"
4765 "},"
4766 "\"required\":[\"type\",\"value\"]"
4767 "},"
4768 "\"unknownValue\":{"
4769 "\"properties\":{"
4770 "\"type\":{"
4771 "\"type\":\"integer\","
4772 "\"minimum\":0"
4773 "},"
4774 "\"value\":{"
4775 "\"type\":\"string\","
4776 "\"enum\":[\"unknown\"]"
4777 "}"
4778 "},"
4779 "\"required\":[\"type\",\"value\"]"
4780 "}"
4781 "}"
4782 "}"
4783 "}"
4784 "}");
4785
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004786 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004787 chunk_reset(out);
4788 chunk_appendf(out,
4789 "{\"errorStr\":\"output buffer too short\"}");
4790 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02004791 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01004792}
4793
Willy Tarreau4596fe22022-05-17 19:07:51 +02004794/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004795 * It returns 0 as long as it does not complete, non-zero upon completion.
4796 * No state is used.
4797 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004798static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01004799{
4800 chunk_reset(&trash);
4801
4802 stats_dump_json_schema(&trash);
4803
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004804 if (applet_putchk(appctx, &trash) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01004805 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01004806
4807 return 1;
4808}
4809
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004810static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004811{
4812 struct proxy *px;
4813 struct server *sv;
4814 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004815 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004816 int clrall = 0;
4817
4818 if (strcmp(args[2], "all") == 0)
4819 clrall = 1;
4820
4821 /* check permissions */
4822 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4823 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4824 return 1;
4825
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004826 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004827 if (clrall) {
4828 memset(&px->be_counters, 0, sizeof(px->be_counters));
4829 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4830 }
4831 else {
4832 px->be_counters.conn_max = 0;
4833 px->be_counters.p.http.rps_max = 0;
4834 px->be_counters.sps_max = 0;
4835 px->be_counters.cps_max = 0;
4836 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004837 px->be_counters.qtime_max = 0;
4838 px->be_counters.ctime_max = 0;
4839 px->be_counters.dtime_max = 0;
4840 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004841
4842 px->fe_counters.conn_max = 0;
4843 px->fe_counters.p.http.rps_max = 0;
4844 px->fe_counters.sps_max = 0;
4845 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004846 }
4847
4848 for (sv = px->srv; sv; sv = sv->next)
4849 if (clrall)
4850 memset(&sv->counters, 0, sizeof(sv->counters));
4851 else {
4852 sv->counters.cur_sess_max = 0;
4853 sv->counters.nbpend_max = 0;
4854 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004855 sv->counters.qtime_max = 0;
4856 sv->counters.ctime_max = 0;
4857 sv->counters.dtime_max = 0;
4858 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004859 }
4860
4861 list_for_each_entry(li, &px->conf.listeners, by_fe)
4862 if (li->counters) {
4863 if (clrall)
4864 memset(li->counters, 0, sizeof(*li->counters));
4865 else
4866 li->counters->conn_max = 0;
4867 }
4868 }
4869
4870 global.cps_max = 0;
4871 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004872 global.ssl_max = 0;
4873 global.ssl_fe_keys_max = 0;
4874 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004875
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004876 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4877 if (!mod->clearable && !clrall)
4878 continue;
4879
4880 for (px = proxies_list; px; px = px->next) {
4881 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4882
4883 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4884 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4885 mod,
4886 mod->counters,
4887 mod->counters_size);
4888 }
4889
4890 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4891 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4892 mod,
4893 mod->counters,
4894 mod->counters_size);
4895 }
4896
4897 if (mod_cap & STATS_PX_CAP_SRV) {
4898 for (sv = px->srv; sv; sv = sv->next) {
4899 EXTRA_COUNTERS_INIT(sv->extra_counters,
4900 mod,
4901 mod->counters,
4902 mod->counters_size);
4903 }
4904 }
4905
4906 if (mod_cap & STATS_PX_CAP_LI) {
4907 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4908 EXTRA_COUNTERS_INIT(li->extra_counters,
4909 mod,
4910 mod->counters,
4911 mod->counters_size);
4912 }
4913 }
4914 }
4915 }
4916
Emeric Brunf8642ee2021-10-29 17:59:18 +02004917 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004918
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004919 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004920 return 1;
4921}
4922
4923
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004924static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004925{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004926 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02004927 int arg = 2;
4928
Willy Tarreau91cefca2022-05-03 17:08:29 +02004929 ctx->scope_str = 0;
4930 ctx->scope_len = 0;
4931 ctx->flags = 0;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004932
Willy Tarreau2f397382019-10-09 11:43:59 +02004933 while (*args[arg]) {
4934 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004935 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02004936 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004937 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02004938 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004939 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02004940 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004941 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02004942 arg++;
4943 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004944 return 0;
4945}
4946
4947
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004948static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004949{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004950 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02004951 int arg = 2;
4952
Willy Tarreau91cefca2022-05-03 17:08:29 +02004953 ctx->scope_str = 0;
4954 ctx->scope_len = 0;
4955 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004956
Willy Tarreau0698c802022-05-11 14:09:57 +02004957 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004958 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004959
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004960 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004961 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004962 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004963 ++args;
4964
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004965 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004966 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02004967 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004968 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004969 ++args;
4970 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004971 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004972 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004973 }
4974
Willy Tarreau91cefca2022-05-03 17:08:29 +02004975 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004976 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004977 struct proxy *px;
4978
Willy Tarreau2f397382019-10-09 11:43:59 +02004979 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004980 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004981 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004982 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02004983 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004984
Willy Tarreau91cefca2022-05-03 17:08:29 +02004985 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02004986 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004987
Willy Tarreau91cefca2022-05-03 17:08:29 +02004988 ctx->flags |= STAT_BOUND;
4989 ctx->type = atoi(args[arg+1]);
4990 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02004991 arg += 3;
4992 }
4993
4994 while (*args[arg]) {
4995 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004996 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02004997 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004998 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02004999 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005000 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005001 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005002 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005003 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005004 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005005 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005006 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005007
Willy Tarreau2b812e22016-11-22 16:18:05 +01005008 return 0;
5009}
5010
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005011static int cli_io_handler_dump_info(struct appctx *appctx)
5012{
Willy Tarreauc12b3212022-05-27 11:08:15 +02005013 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005014}
5015
Willy Tarreau4596fe22022-05-17 19:07:51 +02005016/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005017 * used to send raw stats over a socket.
5018 */
5019static int cli_io_handler_dump_stat(struct appctx *appctx)
5020{
Willy Tarreauc12b3212022-05-27 11:08:15 +02005021 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005022}
5023
Simon Horman6f6bb382017-01-04 09:37:26 +01005024static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5025{
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005026 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005027}
5028
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005029int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5030 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005031{
5032 struct stats_module *mod;
5033
5034 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5035
5036 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5037 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5038 continue;
5039
5040 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5041 }
5042
5043 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5044
5045 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5046 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5047 continue;
5048
5049 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5050 }
5051
5052 return 1;
5053
5054 alloc_failed:
5055 return 0;
5056}
5057
5058/* Initialize and allocate all extra counters for a proxy and its attached
5059 * servers/listeners with all already registered stats module
5060 */
5061int stats_allocate_proxy_counters(struct proxy *px)
5062{
5063 struct server *sv;
5064 struct listener *li;
5065
5066 if (px->cap & PR_CAP_FE) {
5067 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5068 COUNTERS_FE,
5069 STATS_PX_CAP_FE)) {
5070 return 0;
5071 }
5072 }
5073
5074 if (px->cap & PR_CAP_BE) {
5075 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5076 COUNTERS_BE,
5077 STATS_PX_CAP_BE)) {
5078 return 0;
5079 }
5080 }
5081
5082 for (sv = px->srv; sv; sv = sv->next) {
5083 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5084 COUNTERS_SV,
5085 STATS_PX_CAP_SRV)) {
5086 return 0;
5087 }
5088 }
5089
5090 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5091 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5092 COUNTERS_LI,
5093 STATS_PX_CAP_LI)) {
5094 return 0;
5095 }
5096 }
5097
5098 return 1;
5099}
5100
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005101void stats_register_module(struct stats_module *m)
5102{
5103 const uint8_t domain = stats_get_domain(m->domain_flags);
5104
Willy Tarreau2b718102021-04-21 07:32:39 +02005105 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005106 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005107}
5108
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005109static int allocate_stats_px_postcheck(void)
5110{
5111 struct stats_module *mod;
5112 size_t i = ST_F_TOTAL_FIELDS;
5113 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005114 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005115
5116 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5117
5118 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5119 if (!stat_f[STATS_DOMAIN_PROXY]) {
5120 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5121 err_code |= ERR_ALERT | ERR_FATAL;
5122 return err_code;
5123 }
5124
5125 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5126 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5127
5128 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5129 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5130 mod->stats,
5131 mod->stats_count * sizeof(struct name_desc));
5132 i += mod->stats_count;
5133 }
5134
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005135 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005136 if (!stats_allocate_proxy_counters(px)) {
5137 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5138 err_code |= ERR_ALERT | ERR_FATAL;
5139 return err_code;
5140 }
5141 }
5142
Christopher Fauletde79cd22021-01-06 07:41:56 +01005143 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005144
5145 return err_code;
5146}
5147
5148REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5149
Emeric Brunf8642ee2021-10-29 17:59:18 +02005150static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005151{
5152 struct stats_module *mod;
5153 size_t i = 0;
5154 int err_code = 0;
5155
Emeric Brunf8642ee2021-10-29 17:59:18 +02005156 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5157 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5158 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005159 err_code |= ERR_ALERT | ERR_FATAL;
5160 return err_code;
5161 }
5162
Emeric Brunf8642ee2021-10-29 17:59:18 +02005163 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5164 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005165 mod->stats,
5166 mod->stats_count * sizeof(struct name_desc));
5167 i += mod->stats_count;
5168 }
5169
Emeric Brunf8642ee2021-10-29 17:59:18 +02005170 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5171 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005172 err_code |= ERR_ALERT | ERR_FATAL;
5173 return err_code;
5174 }
5175
Christopher Fauletde79cd22021-01-06 07:41:56 +01005176 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005177
5178 return err_code;
5179}
5180
Emeric Brunf8642ee2021-10-29 17:59:18 +02005181REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005182
Christopher Fauletde79cd22021-01-06 07:41:56 +01005183static int allocate_stat_lines_per_thread(void)
5184{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005185 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005186
5187 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5188 const int domain = domains[i];
5189
5190 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5191 if (!stat_l[domain])
5192 return 0;
5193 }
5194 return 1;
5195}
5196
5197REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5198
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005199static int allocate_trash_counters(void)
5200{
5201 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005202 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005203 size_t max_counters_size = 0;
5204
5205 /* calculate the greatest counters used by any stats modules */
5206 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5207 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5208 max_counters_size = mod->counters_size > max_counters_size ?
5209 mod->counters_size : max_counters_size;
5210 }
5211 }
5212
5213 /* allocate the trash with the size of the greatest counters */
5214 if (max_counters_size) {
5215 trash_counters = malloc(max_counters_size);
5216 if (!trash_counters) {
5217 ha_alert("stats: cannot allocate trash counters for statistics\n");
5218 return 0;
5219 }
5220 }
5221
5222 return 1;
5223}
5224
5225REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5226
Christopher Fauletde79cd22021-01-06 07:41:56 +01005227static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005228{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005229 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005230
5231 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5232 const int domain = domains[i];
5233
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005234 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005235 }
5236}
5237
5238
5239REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5240
5241static void deinit_stats(void)
5242{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005243 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005244
5245 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5246 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005247
5248 if (stat_f[domain])
5249 free(stat_f[domain]);
5250 }
5251}
5252
5253REGISTER_POST_DEINIT(deinit_stats);
5254
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005255static void free_trash_counters(void)
5256{
5257 if (trash_counters)
5258 free(trash_counters);
5259}
5260
5261REGISTER_PER_THREAD_FREE(free_trash_counters);
5262
Willy Tarreau2b812e22016-11-22 16:18:05 +01005263/* register cli keywords */
5264static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005265 { { "clear", "counters", NULL }, "clear counters [all] : clear max statistics counters (or all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreau27456202021-05-08 07:54:24 +02005266 { { "show", "info", NULL }, "show info [desc|json|typed|float]* : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005267 { { "show", "stat", NULL }, "show stat [desc|json|no-maint|typed|up]*: report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
5268 { { "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 +01005269 {{},}
5270}};
5271
Willy Tarreau0108d902018-11-25 19:14:37 +01005272INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5273
William Lallemand74c24fb2016-11-21 17:18:36 +01005274struct applet http_stats_applet = {
5275 .obj_type = OBJ_TYPE_APPLET,
5276 .name = "<STATS>", /* used for logging */
5277 .fct = http_stats_io_handler,
5278 .release = NULL,
5279};
5280
William Lallemand74c24fb2016-11-21 17:18:36 +01005281/*
5282 * Local variables:
5283 * c-indent-level: 8
5284 * c-basic-offset: 8
5285 * End:
5286 */