blob: da6fc363a2dc65ad54dcc5127cf24cd9bc145fe7 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020028#include <haproxy/activity.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020029#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020030#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020032#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020033#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020034#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020035#include <haproxy/cli.h>
Willy Tarreau55542642021-10-08 09:33:24 +020036#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020037#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020038#include <haproxy/debug.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020039#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/fd.h>
41#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020042#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020043#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020044#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020045#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020046#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020047#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020048#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020049#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020050#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020051#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020052#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020053#include <haproxy/pipe.h>
54#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020055#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010056#include <haproxy/resolvers.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 Tarreaudfd3de82020-06-04 23:46:14 +020060#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020061#include <haproxy/stream_interface.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 Lallemand74c24fb2016-11-21 17:18:36 +0100262};
263
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100264/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100265THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200266
267/* description of statistics (static and dynamic) */
268static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
269static size_t stat_count[STATS_DOMAIN_COUNT];
270
271/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100272THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100273
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200274/* list of all registered stats module */
275static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
276 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200277 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_DNS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200278};
279
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100280THREAD_LOCAL void *trash_counters;
281
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200282static inline uint8_t stats_get_domain(uint32_t domain)
283{
284 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
285}
286
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200287static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
288{
289 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
290}
291
Christopher Faulet6338a082019-09-09 15:50:54 +0200292static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100293
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200294int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100295{
296 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100297 if (chk->data >= channel_htx_recv_max(chn, htx))
298 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200299 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100300 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100301 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100302 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100303 }
304 else {
305 if (ci_putchk(chn, chk) == -1)
306 return 0;
307 }
308 return 1;
309}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100310
Christopher Fauleted7a0662019-01-14 11:07:34 +0100311static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
312{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200313 struct channel *req = si_oc(si);
314 struct htx *htx = htxbuf(&req->buf);
315 struct htx_blk *blk;
316 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100317
Christopher Fauletb7f88902019-07-15 21:56:43 +0200318 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100319 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200320 ALREADY_CHECKED(blk);
321 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
322 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100323}
324
William Lallemand74c24fb2016-11-21 17:18:36 +0100325/*
326 * http_stats_io_handler()
327 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
328 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100329 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100330 * -> stats_dump_html_head() // emits the HTML headers
331 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
332 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
333 * -> stats_dump_html_px_hdr()
334 * -> stats_dump_fe_stats()
335 * -> stats_dump_li_stats()
336 * -> stats_dump_sv_stats()
337 * -> stats_dump_be_stats()
338 * -> stats_dump_html_px_end()
339 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100340 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100341 */
342
343
William Lallemand74c24fb2016-11-21 17:18:36 +0100344/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
345 * for clearing it if needed.
346 * NOTE: Some tools happen to rely on the field position instead of its name,
347 * so please only append new fields at the end, never in the middle.
348 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200349static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100350{
351 int field;
352
353 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200354 if (stat_f[domain]) {
355 for (field = 0; field < stat_count[domain]; ++field) {
356 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100357
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200358 /* print special delimiter on proxy stats to mark end of
359 static fields */
360 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
361 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200362 }
363 }
364
William Lallemand74c24fb2016-11-21 17:18:36 +0100365 chunk_appendf(&trash, "\n");
366}
367
William Lallemand74c24fb2016-11-21 17:18:36 +0100368/* Emits a stats field without any surrounding element and properly encoded to
369 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
370 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200371int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100372{
373 switch (field_format(f, 0)) {
374 case FF_EMPTY: return 1;
375 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
376 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
377 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
378 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200379 case FF_FLT: {
380 size_t prev_data = out->data;
381 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
382 return out->data;
383 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100384 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
385 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
386 }
387}
388
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200389const char *field_to_html_str(const struct field *f)
390{
391 switch (field_format(f, 0)) {
392 case FF_S32: return U2H(f->u.s32);
393 case FF_S64: return U2H(f->u.s64);
394 case FF_U64: return U2H(f->u.u64);
395 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200396 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200397 case FF_STR: return field_str(f, 0);
398 case FF_EMPTY:
399 default:
400 return "";
401 }
402}
403
William Lallemand74c24fb2016-11-21 17:18:36 +0100404/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
405 * output is supposed to be used on its own line. Returns non-zero on success, 0
406 * if the buffer is full.
407 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200408int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100409{
410 switch (field_format(f, 0)) {
411 case FF_EMPTY: return 1;
412 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
413 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
414 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
415 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200416 case FF_FLT: {
417 size_t prev_data = out->data;
418 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
419 return out->data;
420 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100421 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
422 default: return chunk_appendf(out, "%08x:?", f->type);
423 }
424}
425
Simon Horman05ee2132017-01-04 09:37:25 +0100426/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
427 * the recommendation for interoperable integers in section 6 of RFC 7159.
428 */
429#define JSON_INT_MAX ((1ULL << 53) - 1)
430#define JSON_INT_MIN (0 - JSON_INT_MAX)
431
432/* Emits a stats field value and its type in JSON.
433 * Returns non-zero on success, 0 on error.
434 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200435int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100436{
437 int old_len;
438 char buf[20];
439 const char *type, *value = buf, *quote = "";
440
441 switch (field_format(f, 0)) {
442 case FF_EMPTY: return 1;
443 case FF_S32: type = "\"s32\"";
444 snprintf(buf, sizeof(buf), "%d", f->u.s32);
445 break;
446 case FF_U32: type = "\"u32\"";
447 snprintf(buf, sizeof(buf), "%u", f->u.u32);
448 break;
449 case FF_S64: type = "\"s64\"";
450 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
451 return 0;
452 type = "\"s64\"";
453 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
454 break;
455 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
456 return 0;
457 type = "\"u64\"";
458 snprintf(buf, sizeof(buf), "%llu",
459 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200460 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200461 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200462 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100463 break;
464 case FF_STR: type = "\"str\"";
465 value = field_str(f, 0);
466 quote = "\"";
467 break;
468 default: snprintf(buf, sizeof(buf), "%u", f->type);
469 type = buf;
470 value = "unknown";
471 quote = "\"";
472 break;
473 }
474
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200475 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100476 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
477 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200478 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100479}
480
William Lallemand74c24fb2016-11-21 17:18:36 +0100481/* Emits an encoding of the field type on 3 characters followed by a delimiter.
482 * Returns non-zero on success, 0 if the buffer is full.
483 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200484int stats_emit_field_tags(struct buffer *out, const struct field *f,
485 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100486{
487 char origin, nature, scope;
488
489 switch (field_origin(f, 0)) {
490 case FO_METRIC: origin = 'M'; break;
491 case FO_STATUS: origin = 'S'; break;
492 case FO_KEY: origin = 'K'; break;
493 case FO_CONFIG: origin = 'C'; break;
494 case FO_PRODUCT: origin = 'P'; break;
495 default: origin = '?'; break;
496 }
497
498 switch (field_nature(f, 0)) {
499 case FN_GAUGE: nature = 'G'; break;
500 case FN_LIMIT: nature = 'L'; break;
501 case FN_MIN: nature = 'm'; break;
502 case FN_MAX: nature = 'M'; break;
503 case FN_RATE: nature = 'R'; break;
504 case FN_COUNTER: nature = 'C'; break;
505 case FN_DURATION: nature = 'D'; break;
506 case FN_AGE: nature = 'A'; break;
507 case FN_TIME: nature = 'T'; break;
508 case FN_NAME: nature = 'N'; break;
509 case FN_OUTPUT: nature = 'O'; break;
510 case FN_AVG: nature = 'a'; break;
511 default: nature = '?'; break;
512 }
513
514 switch (field_scope(f, 0)) {
515 case FS_PROCESS: scope = 'P'; break;
516 case FS_SERVICE: scope = 'S'; break;
517 case FS_SYSTEM: scope = 's'; break;
518 case FS_CLUSTER: scope = 'C'; break;
519 default: scope = '?'; break;
520 }
521
522 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
523}
524
Simon Horman05ee2132017-01-04 09:37:25 +0100525/* Emits an encoding of the field type as JSON.
526 * Returns non-zero on success, 0 if the buffer is full.
527 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200528int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100529{
530 const char *origin, *nature, *scope;
531 int old_len;
532
533 switch (field_origin(f, 0)) {
534 case FO_METRIC: origin = "Metric"; break;
535 case FO_STATUS: origin = "Status"; break;
536 case FO_KEY: origin = "Key"; break;
537 case FO_CONFIG: origin = "Config"; break;
538 case FO_PRODUCT: origin = "Product"; break;
539 default: origin = "Unknown"; break;
540 }
541
542 switch (field_nature(f, 0)) {
543 case FN_GAUGE: nature = "Gauge"; break;
544 case FN_LIMIT: nature = "Limit"; break;
545 case FN_MIN: nature = "Min"; break;
546 case FN_MAX: nature = "Max"; break;
547 case FN_RATE: nature = "Rate"; break;
548 case FN_COUNTER: nature = "Counter"; break;
549 case FN_DURATION: nature = "Duration"; break;
550 case FN_AGE: nature = "Age"; break;
551 case FN_TIME: nature = "Time"; break;
552 case FN_NAME: nature = "Name"; break;
553 case FN_OUTPUT: nature = "Output"; break;
554 case FN_AVG: nature = "Avg"; break;
555 default: nature = "Unknown"; break;
556 }
557
558 switch (field_scope(f, 0)) {
559 case FS_PROCESS: scope = "Process"; break;
560 case FS_SERVICE: scope = "Service"; break;
561 case FS_SYSTEM: scope = "System"; break;
562 case FS_CLUSTER: scope = "Cluster"; break;
563 default: scope = "Unknown"; break;
564 }
565
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200566 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100567 chunk_appendf(out, "\"tags\":{"
568 "\"origin\":\"%s\","
569 "\"nature\":\"%s\","
570 "\"scope\":\"%s\""
571 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200572 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100573}
William Lallemand74c24fb2016-11-21 17:18:36 +0100574
575/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200576static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200577 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200578 unsigned int flags,
579 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100580{
581 int field;
582
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200583 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100584 if (!stats_emit_raw_data_field(out, &stats[field]))
585 return 0;
586 if (!chunk_strcat(out, ","))
587 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200588
589 /* print special delimiter on proxy stats to mark end of
590 static fields */
591 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
592 if (!chunk_strcat(out, "-,"))
593 return 0;
594 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100595 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200596
William Lallemand74c24fb2016-11-21 17:18:36 +0100597 chunk_strcat(out, "\n");
598 return 1;
599}
600
601/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200602static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200603 const struct field *stats,
604 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200605 unsigned int flags,
606 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100607{
608 int field;
609
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200610 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100611 if (!stats[field].type)
612 continue;
613
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200614 switch (domain) {
615 case STATS_DOMAIN_PROXY:
616 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
617 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
618 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
619 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
620 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
621 '?',
622 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
623 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200624 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200625 stats[ST_F_PID].u.u32);
626 break;
627
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200628 case STATS_DOMAIN_DNS:
629 chunk_appendf(out, "D.%d.%s:", field,
630 stat_f[domain][field].name);
631 break;
632
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200633 default:
634 break;
635 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100636
637 if (!stats_emit_field_tags(out, &stats[field], ':'))
638 return 0;
639 if (!stats_emit_typed_data_field(out, &stats[field]))
640 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200641
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200642 if (flags & STAT_SHOW_FDESC &&
643 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200644 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200645 }
646
William Lallemand74c24fb2016-11-21 17:18:36 +0100647 if (!chunk_strcat(out, "\n"))
648 return 0;
649 }
650 return 1;
651}
652
Simon Horman05ee2132017-01-04 09:37:25 +0100653/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200654static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200655 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100656{
657 int field;
658 int started = 0;
659
660 if (!chunk_strcat(out, "["))
661 return 0;
662
663 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
664 int old_len;
665
666 if (!field_format(info, field))
667 continue;
668
669 if (started && !chunk_strcat(out, ","))
670 goto err;
671 started = 1;
672
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200673 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100674 chunk_appendf(out,
675 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
676 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200677 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100678 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100680 goto err;
681
682 if (!stats_emit_json_field_tags(out, &info[field]))
683 goto err;
684
685 if (!stats_emit_json_data_field(out, &info[field]))
686 goto err;
687
688 if (!chunk_strcat(out, "}"))
689 goto err;
690 }
691
692 if (!chunk_strcat(out, "]"))
693 goto err;
694 return 1;
695
696err:
697 chunk_reset(out);
698 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
699 return 0;
700}
701
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200702static void stats_print_proxy_field_json(struct buffer *out,
703 const struct field *stat,
704 const char *name,
705 int pos,
706 uint32_t field_type,
707 uint32_t iid,
708 uint32_t sid,
709 uint32_t pid)
710{
711 const char *obj_type;
712 switch (field_type) {
713 case STATS_TYPE_FE: obj_type = "Frontend"; break;
714 case STATS_TYPE_BE: obj_type = "Backend"; break;
715 case STATS_TYPE_SO: obj_type = "Listener"; break;
716 case STATS_TYPE_SV: obj_type = "Server"; break;
717 default: obj_type = "Unknown"; break;
718 }
719
720 chunk_appendf(out,
721 "{"
722 "\"objType\":\"%s\","
723 "\"proxyId\":%u,"
724 "\"id\":%u,"
725 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
726 "\"processNum\":%u,",
727 obj_type, iid, sid, pos, name, pid);
728}
729
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200730static void stats_print_dns_field_json(struct buffer *out,
731 const struct field *stat,
732 const char *name,
733 int pos)
734{
735 chunk_appendf(out,
736 "{"
737 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
738 pos, name);
739}
740
741
Simon Horman05ee2132017-01-04 09:37:25 +0100742/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200743static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200744 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200745 unsigned int flags,
746 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100747{
748 int field;
749 int started = 0;
750
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200751 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100752 return 0;
753 if (!chunk_strcat(out, "["))
754 return 0;
755
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200756 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100757 int old_len;
758
759 if (!stats[field].type)
760 continue;
761
762 if (started && !chunk_strcat(out, ","))
763 goto err;
764 started = 1;
765
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200766 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200767 if (domain == STATS_DOMAIN_PROXY) {
768 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200769 stat_f[domain][field].name,
770 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200771 stats[ST_F_TYPE].u.u32,
772 stats[ST_F_IID].u.u32,
773 stats[ST_F_SID].u.u32,
774 stats[ST_F_PID].u.u32);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200775 } else if (domain == STATS_DOMAIN_DNS) {
776 stats_print_dns_field_json(out, &stats[field],
777 stat_f[domain][field].name,
778 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200779 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200780
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200781 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100782 goto err;
783
784 if (!stats_emit_json_field_tags(out, &stats[field]))
785 goto err;
786
787 if (!stats_emit_json_data_field(out, &stats[field]))
788 goto err;
789
790 if (!chunk_strcat(out, "}"))
791 goto err;
792 }
793
794 if (!chunk_strcat(out, "]"))
795 goto err;
796
797 return 1;
798
799err:
800 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200801 if (flags & STAT_STARTED)
802 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100803 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
804 return 0;
805}
806
William Lallemand74c24fb2016-11-21 17:18:36 +0100807/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200808 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200809 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
810 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
811 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100812 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200813static int stats_dump_fields_html(struct buffer *out,
814 const struct field *stats,
815 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100816{
Willy Tarreau83061a82018-07-13 11:56:34 +0200817 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200818 struct stats_module *mod;
819 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100820
821 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
822 chunk_appendf(out,
823 /* name, queue */
824 "<tr class=\"frontend\">");
825
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200826 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100827 /* Column sub-heading for Enable or Disable server */
828 chunk_appendf(out, "<td></td>");
829 }
830
831 chunk_appendf(out,
832 "<td class=ac>"
833 "<a name=\"%s/Frontend\"></a>"
834 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
835 "<td colspan=3></td>"
836 "",
837 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
838
839 chunk_appendf(out,
840 /* sessions rate : current */
841 "<td><u>%s<div class=tips><table class=det>"
842 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
843 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
844 "",
845 U2H(stats[ST_F_RATE].u.u32),
846 U2H(stats[ST_F_CONN_RATE].u.u32),
847 U2H(stats[ST_F_RATE].u.u32));
848
849 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
850 chunk_appendf(out,
851 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
852 U2H(stats[ST_F_REQ_RATE].u.u32));
853
854 chunk_appendf(out,
855 "</table></div></u></td>"
856 /* sessions rate : max */
857 "<td><u>%s<div class=tips><table class=det>"
858 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
859 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
860 "",
861 U2H(stats[ST_F_RATE_MAX].u.u32),
862 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
863 U2H(stats[ST_F_RATE_MAX].u.u32));
864
865 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
866 chunk_appendf(out,
867 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
868 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
869
870 chunk_appendf(out,
871 "</table></div></u></td>"
872 /* sessions rate : limit */
873 "<td>%s</td>",
874 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
875
876 chunk_appendf(out,
877 /* sessions: current, max, limit, total */
878 "<td>%s</td><td>%s</td><td>%s</td>"
879 "<td><u>%s<div class=tips><table class=det>"
880 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
881 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
882 "",
883 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
884 U2H(stats[ST_F_STOT].u.u64),
885 U2H(stats[ST_F_CONN_TOT].u.u64),
886 U2H(stats[ST_F_STOT].u.u64));
887
888 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
889 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
890 chunk_appendf(out,
891 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
892 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
893 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
894 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
895 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
896 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
897 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
898 "<tr><th>- other responses:</th><td>%s</td></tr>"
899 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100900 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
901 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200902 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100903 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100904 "",
905 U2H(stats[ST_F_REQ_TOT].u.u64),
906 U2H(stats[ST_F_HRSP_1XX].u.u64),
907 U2H(stats[ST_F_HRSP_2XX].u.u64),
908 U2H(stats[ST_F_COMP_RSP].u.u64),
909 stats[ST_F_HRSP_2XX].u.u64 ?
910 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
911 U2H(stats[ST_F_HRSP_3XX].u.u64),
912 U2H(stats[ST_F_HRSP_4XX].u.u64),
913 U2H(stats[ST_F_HRSP_5XX].u.u64),
914 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200915 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100916 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
917 U2H(stats[ST_F_CACHE_HITS].u.u64),
918 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
919 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100920 U2H(stats[ST_F_WREW].u.u64),
921 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100922 }
923
924 chunk_appendf(out,
925 "</table></div></u></td>"
926 /* sessions: lbtot, lastsess */
927 "<td></td><td></td>"
928 /* bytes : in */
929 "<td>%s</td>"
930 "",
931 U2H(stats[ST_F_BIN].u.u64));
932
933 chunk_appendf(out,
934 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
935 "<td>%s%s<div class=tips><table class=det>"
936 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
937 "<tr><th>Compression in:</th><td>%s</td></tr>"
938 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
939 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
940 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
941 "</table></div>%s</td>",
942 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
943 U2H(stats[ST_F_BOUT].u.u64),
944 U2H(stats[ST_F_BOUT].u.u64),
945 U2H(stats[ST_F_COMP_IN].u.u64),
946 U2H(stats[ST_F_COMP_OUT].u.u64),
947 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
948 U2H(stats[ST_F_COMP_BYP].u.u64),
949 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
950 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,
951 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
952
953 chunk_appendf(out,
954 /* denied: req, resp */
955 "<td>%s</td><td>%s</td>"
956 /* errors : request, connect, response */
957 "<td>%s</td><td></td><td></td>"
958 /* warnings: retries, redispatches */
959 "<td></td><td></td>"
960 /* server status : reflect frontend status */
961 "<td class=ac>%s</td>"
962 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200963 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100964 "",
965 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
966 U2H(stats[ST_F_EREQ].u.u64),
967 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200968
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200969 if (flags & STAT_SHMODULES) {
970 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100971 chunk_appendf(out, "<td>");
972
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200973 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100974 chunk_appendf(out,
975 "<u>%s<div class=tips><table class=det>",
976 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200977 for (j = 0; j < mod->stats_count; ++j) {
978 chunk_appendf(out,
979 "<tr><th>%s</th><td>%s</td></tr>",
980 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
981 ++i;
982 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100983 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200984 } else {
985 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200986 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200987
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100988 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200989 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200990 }
991
992 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100993 }
994 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
995 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200996 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100997 /* Column sub-heading for Enable or Disable server */
998 chunk_appendf(out, "<td></td>");
999 }
1000
1001 chunk_appendf(out,
1002 /* frontend name, listener name */
1003 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1004 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1005 "",
1006 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001007 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001008 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1009
Willy Tarreau708c4162019-10-09 10:19:16 +02001010 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001011 chunk_appendf(out, "<div class=tips>");
1012
Willy Tarreau90807112020-02-25 08:16:33 +01001013 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001014 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1015 else if (*field_str(stats, ST_F_ADDR) == '[')
1016 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1017 else if (*field_str(stats, ST_F_ADDR))
1018 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1019
1020 /* id */
1021 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1022 }
1023
1024 chunk_appendf(out,
1025 /* queue */
1026 "%s</td><td colspan=3></td>"
1027 /* sessions rate: current, max, limit */
1028 "<td colspan=3>&nbsp;</td>"
1029 /* sessions: current, max, limit, total, lbtot, lastsess */
1030 "<td>%s</td><td>%s</td><td>%s</td>"
1031 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1032 /* bytes: in, out */
1033 "<td>%s</td><td>%s</td>"
1034 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001035 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001036 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1037 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1038
1039 chunk_appendf(out,
1040 /* denied: req, resp */
1041 "<td>%s</td><td>%s</td>"
1042 /* errors: request, connect, response */
1043 "<td>%s</td><td></td><td></td>"
1044 /* warnings: retries, redispatches */
1045 "<td></td><td></td>"
1046 /* server status: reflect listener status */
1047 "<td class=ac>%s</td>"
1048 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001049 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001050 "",
1051 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1052 U2H(stats[ST_F_EREQ].u.u64),
1053 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001054
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001055 if (flags & STAT_SHMODULES) {
1056 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001057 chunk_appendf(out, "<td>");
1058
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001059 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001060 chunk_appendf(out,
1061 "<u>%s<div class=tips><table class=det>",
1062 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001063 for (j = 0; j < mod->stats_count; ++j) {
1064 chunk_appendf(out,
1065 "<tr><th>%s</th><td>%s</td></tr>",
1066 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1067 ++i;
1068 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001069 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001070 } else {
1071 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001072 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001073
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001074 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001075 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001076 }
1077
1078 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001079 }
1080 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1081 const char *style;
1082
1083 /* determine the style to use depending on the server's state,
1084 * its health and weight. There isn't a 1-to-1 mapping between
1085 * state and styles for the cases where the server is (still)
1086 * up. The reason is that we don't want to report nolb and
1087 * drain with the same color.
1088 */
1089
1090 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1091 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1092 style = "down";
1093 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001094 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001095 style = "going_up";
1096 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001097 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1098 style = "draining";
1099 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001100 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001101 style = "going_down";
1102 }
1103 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1104 style = "nolb";
1105 }
1106 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1107 style = "no_check";
1108 }
1109 else if (!stats[ST_F_CHKFAIL].type ||
1110 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1111 /* no check or max health = UP */
1112 if (stats[ST_F_WEIGHT].u.u32)
1113 style = "up";
1114 else
1115 style = "draining";
1116 }
1117 else {
1118 style = "going_down";
1119 }
1120
Willy Tarreau7b524852020-08-11 10:26:36 +02001121 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001122 chunk_appendf(out, "<tr class=\"maintain\">");
1123 else
1124 chunk_appendf(out,
1125 "<tr class=\"%s_%s\">",
1126 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1127
1128
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001129 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001130 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001131 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1132 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001133 field_str(stats, ST_F_SVNAME));
1134
1135 chunk_appendf(out,
1136 "<td class=ac><a name=\"%s/%s\"></a>%s"
1137 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1138 "",
1139 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001140 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001141 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1142
Willy Tarreau708c4162019-10-09 10:19:16 +02001143 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001144 chunk_appendf(out, "<div class=tips>");
1145
Willy Tarreau90807112020-02-25 08:16:33 +01001146 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001147 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1148 else if (*field_str(stats, ST_F_ADDR) == '[')
1149 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1150 else if (*field_str(stats, ST_F_ADDR))
1151 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1152
1153 /* id */
1154 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1155
1156 /* cookie */
1157 if (stats[ST_F_COOKIE].type) {
1158 chunk_appendf(out, ", cookie: '");
1159 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1160 chunk_htmlencode(out, &src);
1161 chunk_appendf(out, "'");
1162 }
1163
1164 chunk_appendf(out, "</div>");
1165 }
1166
1167 chunk_appendf(out,
1168 /* queue : current, max, limit */
1169 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1170 /* sessions rate : current, max, limit */
1171 "<td>%s</td><td>%s</td><td></td>"
1172 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001173 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001174 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1175 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1176
1177 chunk_appendf(out,
1178 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001179 "<td><u>%s<div class=tips>"
1180 "<table class=det>"
1181 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001182 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001183 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001184 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1185 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001186 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001187 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1188 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1189 "</table></div></u>"
1190 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001191 "<td><u>%s<div class=tips><table class=det>"
1192 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1193 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001194 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001195 U2H(stats[ST_F_SCUR].u.u32),
1196 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1197 U2H(stats[ST_F_SRV_ICUR].u.u32),
1198 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1199 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001200 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001201
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001202 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1203 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1204 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001205 U2H(stats[ST_F_STOT].u.u64),
1206 U2H(stats[ST_F_STOT].u.u64));
1207
1208 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1209 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001210 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001211 "<tr><th>New connections:</th><td>%s</td></tr>"
1212 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001213 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001214 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1215 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1216 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1217 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1218 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1219 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001220 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001221 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001222 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001223 U2H(stats[ST_F_CONNECT].u.u64),
1224 U2H(stats[ST_F_REUSE].u.u64),
1225 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1226 (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 +02001227 U2H(stats[ST_F_REQ_TOT].u.u64),
1228 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1229 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1230 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1231 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1232 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1233 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1234 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1235 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1236 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1237 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1238 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1239 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001240 U2H(stats[ST_F_WREW].u.u64),
1241 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001242 }
1243
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001244 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1245 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1246 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1247 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1248 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001249 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001250 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1251 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1252 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1253 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001254
1255 chunk_appendf(out,
1256 "</table></div></u></td>"
1257 /* sessions: lbtot, last */
1258 "<td>%s</td><td>%s</td>",
1259 U2H(stats[ST_F_LBTOT].u.u64),
1260 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1261
1262 chunk_appendf(out,
1263 /* bytes : in, out */
1264 "<td>%s</td><td>%s</td>"
1265 /* denied: req, resp */
1266 "<td></td><td>%s</td>"
1267 /* errors : request, connect */
1268 "<td></td><td>%s</td>"
1269 /* errors : response */
1270 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1271 /* warnings: retries, redispatches */
1272 "<td>%lld</td><td>%lld</td>"
1273 "",
1274 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1275 U2H(stats[ST_F_DRESP].u.u64),
1276 U2H(stats[ST_F_ECON].u.u64),
1277 U2H(stats[ST_F_ERESP].u.u64),
1278 (long long)stats[ST_F_CLI_ABRT].u.u64,
1279 (long long)stats[ST_F_SRV_ABRT].u.u64,
1280 (long long)stats[ST_F_WRETR].u.u64,
1281 (long long)stats[ST_F_WREDIS].u.u64);
1282
1283 /* status, last change */
1284 chunk_appendf(out, "<td class=ac>");
1285
1286 /* FIXME!!!!
1287 * LASTCHG should contain the last change for *this* server and must be computed
1288 * properly above, as was done below, ie: this server if maint, otherwise ref server
1289 * if tracking. Note that ref is either local or remote depending on tracking.
1290 */
1291
1292
Willy Tarreau7b524852020-08-11 10:26:36 +02001293 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001294 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1295 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001296 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001297 chunk_strcat(out, "<i>no check</i>");
1298 }
1299 else {
1300 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 +02001301 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001302 if (stats[ST_F_CHECK_HEALTH].u.u32)
1303 chunk_strcat(out, " &uarr;");
1304 }
1305 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1306 chunk_strcat(out, " &darr;");
1307 }
1308
Willy Tarreau7b524852020-08-11 10:26:36 +02001309 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001310 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1311 chunk_appendf(out,
1312 "</td><td class=ac><u> %s",
1313 field_str(stats, ST_F_AGENT_STATUS));
1314
1315 if (stats[ST_F_AGENT_CODE].type)
1316 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1317
1318 if (stats[ST_F_AGENT_DURATION].type)
1319 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1320
1321 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1322
1323 if (*field_str(stats, ST_F_LAST_AGT)) {
1324 chunk_appendf(out, ": ");
1325 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1326 chunk_htmlencode(out, &src);
1327 }
1328 chunk_appendf(out, "</div></u>");
1329 }
1330 else if (stats[ST_F_CHECK_STATUS].type) {
1331 chunk_appendf(out,
1332 "</td><td class=ac><u> %s",
1333 field_str(stats, ST_F_CHECK_STATUS));
1334
1335 if (stats[ST_F_CHECK_CODE].type)
1336 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1337
1338 if (stats[ST_F_CHECK_DURATION].type)
1339 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1340
1341 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1342
1343 if (*field_str(stats, ST_F_LAST_CHK)) {
1344 chunk_appendf(out, ": ");
1345 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1346 chunk_htmlencode(out, &src);
1347 }
1348 chunk_appendf(out, "</div></u>");
1349 }
1350 else
1351 chunk_appendf(out, "</td><td>");
1352
1353 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001354 /* weight / uweight */
1355 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001356 /* act, bck */
1357 "<td class=ac>%s</td><td class=ac>%s</td>"
1358 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001359 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001360 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1361 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1362
1363 /* check failures: unique, fatal, down time */
1364 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1365 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1366 }
1367 else if (stats[ST_F_CHKFAIL].type) {
1368 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1369
1370 if (stats[ST_F_HANAFAIL].type)
1371 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1372
1373 chunk_appendf(out,
1374 "<div class=tips>Failed Health Checks%s</div></u></td>"
1375 "<td>%lld</td><td>%s</td>"
1376 "",
1377 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1378 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1379 }
1380 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1381 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1382 chunk_appendf(out,
1383 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1384 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1385 }
1386 else
1387 chunk_appendf(out, "<td colspan=3></td>");
1388
1389 /* throttle */
1390 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001391 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001392 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001393 chunk_appendf(out, "<td class=ac>-</td>");
1394
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001395 if (flags & STAT_SHMODULES) {
1396 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001397 chunk_appendf(out, "<td>");
1398
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001399 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001400 chunk_appendf(out,
1401 "<u>%s<div class=tips><table class=det>",
1402 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001403 for (j = 0; j < mod->stats_count; ++j) {
1404 chunk_appendf(out,
1405 "<tr><th>%s</th><td>%s</td></tr>",
1406 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1407 ++i;
1408 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001409 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001410 } else {
1411 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001412 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001413
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001414 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001415 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001416 }
1417
1418 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001419 }
1420 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1421 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001422 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001423 /* Column sub-heading for Enable or Disable server */
1424 chunk_appendf(out, "<td></td>");
1425 }
1426 chunk_appendf(out,
1427 "<td class=ac>"
1428 /* name */
1429 "%s<a name=\"%s/Backend\"></a>"
1430 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1431 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001432 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001433 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1434
Willy Tarreau708c4162019-10-09 10:19:16 +02001435 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001436 /* balancing */
1437 chunk_appendf(out, "<div class=tips>balancing: %s",
1438 field_str(stats, ST_F_ALGO));
1439
1440 /* cookie */
1441 if (stats[ST_F_COOKIE].type) {
1442 chunk_appendf(out, ", cookie: '");
1443 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1444 chunk_htmlencode(out, &src);
1445 chunk_appendf(out, "'");
1446 }
1447 chunk_appendf(out, "</div>");
1448 }
1449
1450 chunk_appendf(out,
1451 "%s</td>"
1452 /* queue : current, max */
1453 "<td>%s</td><td>%s</td><td></td>"
1454 /* sessions rate : current, max, limit */
1455 "<td>%s</td><td>%s</td><td></td>"
1456 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001457 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001458 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1459 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1460
1461 chunk_appendf(out,
1462 /* sessions: current, max, limit, total */
1463 "<td>%s</td><td>%s</td><td>%s</td>"
1464 "<td><u>%s<div class=tips><table class=det>"
1465 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1466 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001467 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 +01001468 U2H(stats[ST_F_STOT].u.u64),
1469 U2H(stats[ST_F_STOT].u.u64));
1470
1471 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1472 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1473 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001474 "<tr><th>New connections:</th><td>%s</td></tr>"
1475 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001476 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1477 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1478 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1479 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1480 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1481 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1482 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1483 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001484 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1485 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001486 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001487 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001488 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001489 U2H(stats[ST_F_CONNECT].u.u64),
1490 U2H(stats[ST_F_REUSE].u.u64),
1491 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1492 (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 +01001493 U2H(stats[ST_F_REQ_TOT].u.u64),
1494 U2H(stats[ST_F_HRSP_1XX].u.u64),
1495 U2H(stats[ST_F_HRSP_2XX].u.u64),
1496 U2H(stats[ST_F_COMP_RSP].u.u64),
1497 stats[ST_F_HRSP_2XX].u.u64 ?
1498 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1499 U2H(stats[ST_F_HRSP_3XX].u.u64),
1500 U2H(stats[ST_F_HRSP_4XX].u.u64),
1501 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001502 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001503 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1504 U2H(stats[ST_F_CACHE_HITS].u.u64),
1505 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1506 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001507 U2H(stats[ST_F_WREW].u.u64),
1508 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001509 }
1510
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001511 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1512 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1513 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1514 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1515 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001516 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001517 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1518 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1519 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1520 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001521
1522 chunk_appendf(out,
1523 "</table></div></u></td>"
1524 /* sessions: lbtot, last */
1525 "<td>%s</td><td>%s</td>"
1526 /* bytes: in */
1527 "<td>%s</td>"
1528 "",
1529 U2H(stats[ST_F_LBTOT].u.u64),
1530 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1531 U2H(stats[ST_F_BIN].u.u64));
1532
1533 chunk_appendf(out,
1534 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1535 "<td>%s%s<div class=tips><table class=det>"
1536 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1537 "<tr><th>Compression in:</th><td>%s</td></tr>"
1538 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1539 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1540 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1541 "</table></div>%s</td>",
1542 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1543 U2H(stats[ST_F_BOUT].u.u64),
1544 U2H(stats[ST_F_BOUT].u.u64),
1545 U2H(stats[ST_F_COMP_IN].u.u64),
1546 U2H(stats[ST_F_COMP_OUT].u.u64),
1547 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1548 U2H(stats[ST_F_COMP_BYP].u.u64),
1549 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1550 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,
1551 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1552
1553 chunk_appendf(out,
1554 /* denied: req, resp */
1555 "<td>%s</td><td>%s</td>"
1556 /* errors : request, connect */
1557 "<td></td><td>%s</td>"
1558 /* errors : response */
1559 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1560 /* warnings: retries, redispatches */
1561 "<td>%lld</td><td>%lld</td>"
1562 /* backend status: reflect backend status (up/down): we display UP
1563 * if the backend has known working servers or if it has no server at
1564 * all (eg: for stats). Then we display the total weight, number of
1565 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001566 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001567 "<td class=ac>%d</td><td class=ac>%d</td>"
1568 "",
1569 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1570 U2H(stats[ST_F_ECON].u.u64),
1571 U2H(stats[ST_F_ERESP].u.u64),
1572 (long long)stats[ST_F_CLI_ABRT].u.u64,
1573 (long long)stats[ST_F_SRV_ABRT].u.u64,
1574 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1575 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1576 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 +02001577 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001578 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1579
1580 chunk_appendf(out,
1581 /* rest of backend: nothing, down transitions, total downtime, throttle */
1582 "<td class=ac>&nbsp;</td><td>%d</td>"
1583 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001584 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001585 stats[ST_F_CHKDOWN].u.u32,
1586 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001587
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001588 if (flags & STAT_SHMODULES) {
1589 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001590 chunk_appendf(out, "<td>");
1591
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001592 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001593 chunk_appendf(out,
1594 "<u>%s<div class=tips><table class=det>",
1595 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001596 for (j = 0; j < mod->stats_count; ++j) {
1597 chunk_appendf(out,
1598 "<tr><th>%s</th><td>%s</td></tr>",
1599 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1600 ++i;
1601 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001602 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001603 } else {
1604 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001605 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001606
1607 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001608 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001609 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001610
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001611 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001612 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001613
William Lallemand74c24fb2016-11-21 17:18:36 +01001614 return 1;
1615}
1616
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001617int stats_dump_one_line(const struct field *stats, size_t stats_count,
1618 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001619{
Simon Horman05ee2132017-01-04 09:37:25 +01001620 int ret;
1621
William Lallemand74c24fb2016-11-21 17:18:36 +01001622 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001623 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001624 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001625 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001626 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001627 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001628 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001629 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001630
1631 if (ret)
1632 appctx->ctx.stats.flags |= STAT_STARTED;
1633
1634 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001635}
1636
William Dauchy0ef54392021-01-17 18:27:45 +01001637/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001638 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1639 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1640 * this value, or if the selected field is not implemented for frontends, the
1641 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001642 */
William Dauchy0ef54392021-01-17 18:27:45 +01001643int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1644 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001645{
William Dauchy0ef54392021-01-17 18:27:45 +01001646 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001647
William Lallemand74c24fb2016-11-21 17:18:36 +01001648 if (len < ST_F_TOTAL_FIELDS)
1649 return 0;
1650
William Dauchy0ef54392021-01-17 18:27:45 +01001651 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001652 struct field metric = { 0 };
1653
William Dauchy0ef54392021-01-17 18:27:45 +01001654 switch (current_field) {
1655 case ST_F_PXNAME:
1656 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1657 break;
1658 case ST_F_SVNAME:
1659 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1660 break;
1661 case ST_F_MODE:
1662 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1663 break;
1664 case ST_F_SCUR:
1665 metric = mkf_u32(0, px->feconn);
1666 break;
1667 case ST_F_SMAX:
1668 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1669 break;
1670 case ST_F_SLIM:
1671 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1672 break;
1673 case ST_F_STOT:
1674 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1675 break;
1676 case ST_F_BIN:
1677 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1678 break;
1679 case ST_F_BOUT:
1680 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1681 break;
1682 case ST_F_DREQ:
1683 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1684 break;
1685 case ST_F_DRESP:
1686 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1687 break;
1688 case ST_F_EREQ:
1689 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1690 break;
1691 case ST_F_DCON:
1692 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1693 break;
1694 case ST_F_DSES:
1695 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1696 break;
1697 case ST_F_STATUS:
1698 metric = mkf_str(FO_STATUS, px->disabled ? "STOP" : "OPEN");
1699 break;
1700 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001701 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001702 break;
1703 case ST_F_IID:
1704 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1705 break;
1706 case ST_F_SID:
1707 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1708 break;
1709 case ST_F_TYPE:
1710 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1711 break;
1712 case ST_F_RATE:
1713 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1714 break;
1715 case ST_F_RATE_LIM:
1716 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1717 break;
1718 case ST_F_RATE_MAX:
1719 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1720 break;
1721 case ST_F_WREW:
1722 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1723 break;
1724 case ST_F_EINT:
1725 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1726 break;
1727 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001728 if (px->mode == PR_MODE_HTTP)
1729 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001730 break;
1731 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001732 if (px->mode == PR_MODE_HTTP)
1733 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001734 break;
1735 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001736 if (px->mode == PR_MODE_HTTP)
1737 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001738 break;
1739 case ST_F_HRSP_4XX:
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[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001742 break;
1743 case ST_F_HRSP_5XX:
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[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001746 break;
1747 case ST_F_HRSP_OTHER:
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[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001750 break;
1751 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001752 if (px->mode == PR_MODE_HTTP)
1753 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001754 break;
1755 case ST_F_CACHE_LOOKUPS:
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.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001758 break;
1759 case ST_F_CACHE_HITS:
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.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001762 break;
1763 case ST_F_REQ_RATE:
1764 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1765 break;
1766 case ST_F_REQ_RATE_MAX:
1767 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1768 break;
1769 case ST_F_REQ_TOT:
1770 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1771 break;
1772 case ST_F_COMP_IN:
1773 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1774 break;
1775 case ST_F_COMP_OUT:
1776 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1777 break;
1778 case ST_F_COMP_BYP:
1779 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1780 break;
1781 case ST_F_COMP_RSP:
1782 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1783 break;
1784 case ST_F_CONN_RATE:
1785 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1786 break;
1787 case ST_F_CONN_RATE_MAX:
1788 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1789 break;
1790 case ST_F_CONN_TOT:
1791 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1792 break;
1793 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001794 /* not used for frontends. If a specific metric
1795 * is requested, return an error. Otherwise continue.
1796 */
1797 if (selected_field != NULL)
1798 return 0;
1799 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001800 }
1801 stats[current_field] = metric;
1802 if (selected_field != NULL)
1803 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001804 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001805 return 1;
1806}
1807
1808/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1809 * the state from stream interface <si>. The caller is responsible for clearing
1810 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1811 */
1812static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1813{
1814 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001815 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1816 struct stats_module *mod;
1817 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001818
1819 if (!(px->cap & PR_CAP_FE))
1820 return 0;
1821
1822 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1823 return 0;
1824
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001825 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1826
William Dauchy0ef54392021-01-17 18:27:45 +01001827 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001828 return 0;
1829
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001830 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1831 void *counters;
1832
1833 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1834 stats_count += mod->stats_count;
1835 continue;
1836 }
1837
1838 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1839 mod->fill_stats(counters, stats + stats_count);
1840 stats_count += mod->stats_count;
1841 }
1842
1843 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001844}
1845
William Dauchy655e14e2021-02-14 23:22:54 +01001846/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1847 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1848 * this length is less then this value, the function returns 0, otherwise, it
1849 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1850 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001851 */
1852int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001853 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001854{
William Dauchy655e14e2021-02-14 23:22:54 +01001855 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001856 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001857
1858 if (len < ST_F_TOTAL_FIELDS)
1859 return 0;
1860
1861 if (!l->counters)
1862 return 0;
1863
1864 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001865
William Dauchy655e14e2021-02-14 23:22:54 +01001866 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1867 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001868
William Dauchy655e14e2021-02-14 23:22:54 +01001869 switch (current_field) {
1870 case ST_F_PXNAME:
1871 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1872 break;
1873 case ST_F_SVNAME:
1874 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1875 break;
1876 case ST_F_MODE:
1877 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1878 break;
1879 case ST_F_SCUR:
1880 metric = mkf_u32(0, l->nbconn);
1881 break;
1882 case ST_F_SMAX:
1883 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1884 break;
1885 case ST_F_SLIM:
1886 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1887 break;
1888 case ST_F_STOT:
1889 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1890 break;
1891 case ST_F_BIN:
1892 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1893 break;
1894 case ST_F_BOUT:
1895 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1896 break;
1897 case ST_F_DREQ:
1898 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1899 break;
1900 case ST_F_DRESP:
1901 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1902 break;
1903 case ST_F_EREQ:
1904 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1905 break;
1906 case ST_F_DCON:
1907 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1908 break;
1909 case ST_F_DSES:
1910 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1911 break;
1912 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001913 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001914 break;
1915 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001916 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01001917 break;
1918 case ST_F_IID:
1919 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1920 break;
1921 case ST_F_SID:
1922 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1923 break;
1924 case ST_F_TYPE:
1925 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1926 break;
1927 case ST_F_WREW:
1928 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1929 break;
1930 case ST_F_EINT:
1931 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1932 break;
1933 case ST_F_ADDR:
1934 if (flags & STAT_SHLGNDS) {
1935 char str[INET6_ADDRSTRLEN];
1936 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001937
William Dauchy655e14e2021-02-14 23:22:54 +01001938 port = get_host_port(&l->rx.addr);
1939 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1940 case AF_INET:
1941 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1942 chunk_appendf(out, "%s:%d", str, port);
1943 break;
1944 case AF_INET6:
1945 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1946 chunk_appendf(out, "[%s]:%d", str, port);
1947 break;
1948 case AF_UNIX:
1949 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1950 break;
1951 case -1:
1952 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1953 chunk_strcat(out, strerror(errno));
1954 break;
1955 default: /* address family not supported */
1956 break;
1957 }
1958 }
1959 break;
1960 default:
1961 /* not used for listen. If a specific metric
1962 * is requested, return an error. Otherwise continue.
1963 */
1964 if (selected_field != NULL)
1965 return 0;
1966 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001967 }
William Dauchy655e14e2021-02-14 23:22:54 +01001968 stats[current_field] = metric;
1969 if (selected_field != NULL)
1970 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001971 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001972 return 1;
1973}
1974
1975/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001976 * from stream interface <si>. The caller is responsible for clearing the trash
1977 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001978 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001979static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001980{
1981 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001982 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1983 struct stats_module *mod;
1984 size_t stats_count = ST_F_TOTAL_FIELDS;
1985
1986 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001987
William Dauchy655e14e2021-02-14 23:22:54 +01001988 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats,
1989 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001990 return 0;
1991
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001992 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1993 void *counters;
1994
1995 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1996 stats_count += mod->stats_count;
1997 continue;
1998 }
1999
2000 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2001 mod->fill_stats(counters, stats + stats_count);
2002 stats_count += mod->stats_count;
2003 }
2004
2005 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002006}
2007
2008enum srv_stats_state {
2009 SRV_STATS_STATE_DOWN = 0,
2010 SRV_STATS_STATE_DOWN_AGENT,
2011 SRV_STATS_STATE_GOING_UP,
2012 SRV_STATS_STATE_UP_GOING_DOWN,
2013 SRV_STATS_STATE_UP,
2014 SRV_STATS_STATE_NOLB_GOING_DOWN,
2015 SRV_STATS_STATE_NOLB,
2016 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2017 SRV_STATS_STATE_DRAIN,
2018 SRV_STATS_STATE_DRAIN_AGENT,
2019 SRV_STATS_STATE_NO_CHECK,
2020
2021 SRV_STATS_STATE_COUNT, /* Must be last */
2022};
2023
2024static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2025 [SRV_STATS_STATE_DOWN] = "DOWN",
2026 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2027 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2028 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2029 [SRV_STATS_STATE_UP] = "UP",
2030 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2031 [SRV_STATS_STATE_NOLB] = "NOLB",
2032 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2033 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2034 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2035 [SRV_STATS_STATE_NO_CHECK] = "no check"
2036};
2037
William Dauchyd3a9a492021-01-25 17:29:03 +01002038/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002039 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002040static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2041 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002042{
Emeric Brun52a91d32017-08-31 14:41:55 +02002043 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002044 if ((ref->check.state & CHK_ST_ENABLED) &&
2045 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002046 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002047 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002048 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002049 }
2050
Emeric Brun52a91d32017-08-31 14:41:55 +02002051 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002052 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002053 *state = SRV_STATS_STATE_DRAIN_AGENT;
2054 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2055 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002056 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002057 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002058 }
2059
William Dauchyd3a9a492021-01-25 17:29:03 +01002060 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2061 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002062 }
2063 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002064 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002065 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2066 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002067 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002068 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002069 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002070 }
2071 }
2072 else { /* stopped */
2073 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002074 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002075 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002076 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002077 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002078 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002079 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002080 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002081 }
2082 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002083}
William Lallemand74c24fb2016-11-21 17:18:36 +01002084
William Dauchyd3a9a492021-01-25 17:29:03 +01002085/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2086 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2087 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2088 * this value, or if the selected field is not implemented for servers, the
2089 * function returns 0, otherwise, it returns 1. <flags> can take the value
2090 * STAT_SHLGNDS.
2091 */
2092int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2093 struct field *stats, int len,
2094 enum stat_field *selected_field)
2095{
2096 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2097 struct server *via = sv->track ? sv->track : sv;
2098 struct server *ref = via;
2099 enum srv_stats_state state = 0;
2100 char str[INET6_ADDRSTRLEN];
2101 struct buffer *out = get_trash_chunk();
2102 char *fld_status;
2103 long long srv_samples_counter;
2104 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002105
William Dauchyd3a9a492021-01-25 17:29:03 +01002106 if (len < ST_F_TOTAL_FIELDS)
2107 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002108
William Dauchyd3a9a492021-01-25 17:29:03 +01002109 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002110
William Dauchyd3a9a492021-01-25 17:29:03 +01002111 /* compute state for later use */
2112 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2113 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2114 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2115 /* we have "via" which is the tracked server as described in the configuration,
2116 * and "ref" which is the checked server and the end of the chain.
2117 */
2118 while (ref->track)
2119 ref = ref->track;
2120 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002121 }
2122
William Dauchyd3a9a492021-01-25 17:29:03 +01002123 /* compue time values for later use */
2124 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2125 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2126 *selected_field == ST_F_TTIME) {
2127 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2128 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2129 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002130 }
2131
William Dauchyd3a9a492021-01-25 17:29:03 +01002132 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2133 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002134
William Dauchyd3a9a492021-01-25 17:29:03 +01002135 switch (current_field) {
2136 case ST_F_PXNAME:
2137 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2138 break;
2139 case ST_F_SVNAME:
2140 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2141 break;
2142 case ST_F_MODE:
2143 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002144 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002145 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002146 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002147 break;
2148 case ST_F_QMAX:
2149 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2150 break;
2151 case ST_F_SCUR:
2152 metric = mkf_u32(0, sv->cur_sess);
2153 break;
2154 case ST_F_SMAX:
2155 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2156 break;
2157 case ST_F_SLIM:
2158 if (sv->maxconn)
2159 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2160 break;
2161 case ST_F_SRV_ICUR:
2162 metric = mkf_u32(0, sv->curr_idle_conns);
2163 break;
2164 case ST_F_SRV_ILIM:
2165 if (sv->max_idle_conns != -1)
2166 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2167 break;
2168 case ST_F_STOT:
2169 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2170 break;
2171 case ST_F_BIN:
2172 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2173 break;
2174 case ST_F_BOUT:
2175 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2176 break;
2177 case ST_F_DRESP:
2178 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2179 break;
2180 case ST_F_ECON:
2181 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2182 break;
2183 case ST_F_ERESP:
2184 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2185 break;
2186 case ST_F_WRETR:
2187 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2188 break;
2189 case ST_F_WREDIS:
2190 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2191 break;
2192 case ST_F_WREW:
2193 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2194 break;
2195 case ST_F_EINT:
2196 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2197 break;
2198 case ST_F_CONNECT:
2199 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2200 break;
2201 case ST_F_REUSE:
2202 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2203 break;
2204 case ST_F_IDLE_CONN_CUR:
2205 metric = mkf_u32(0, sv->curr_idle_nb);
2206 break;
2207 case ST_F_SAFE_CONN_CUR:
2208 metric = mkf_u32(0, sv->curr_safe_nb);
2209 break;
2210 case ST_F_USED_CONN_CUR:
2211 metric = mkf_u32(0, sv->curr_used_conns);
2212 break;
2213 case ST_F_NEED_CONN_EST:
2214 metric = mkf_u32(0, sv->est_need_conns);
2215 break;
2216 case ST_F_STATUS:
2217 fld_status = chunk_newstr(out);
2218 if (sv->cur_admin & SRV_ADMF_RMAINT)
2219 chunk_appendf(out, "MAINT (resolution)");
2220 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2221 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2222 else if (sv->cur_admin & SRV_ADMF_MAINT)
2223 chunk_appendf(out, "MAINT");
2224 else
2225 chunk_appendf(out,
2226 srv_hlt_st[state],
2227 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2228 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002229
William Dauchyd3a9a492021-01-25 17:29:03 +01002230 metric = mkf_str(FO_STATUS, fld_status);
2231 break;
2232 case ST_F_LASTCHG:
2233 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2234 break;
2235 case ST_F_WEIGHT:
2236 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2237 break;
2238 case ST_F_UWEIGHT:
2239 metric = mkf_u32(FN_AVG, sv->uweight);
2240 break;
2241 case ST_F_ACT:
2242 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2243 break;
2244 case ST_F_BCK:
2245 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2246 break;
2247 case ST_F_CHKFAIL:
2248 if (sv->check.state & CHK_ST_ENABLED)
2249 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2250 break;
2251 case ST_F_CHKDOWN:
2252 if (sv->check.state & CHK_ST_ENABLED)
2253 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2254 break;
2255 case ST_F_DOWNTIME:
2256 if (sv->check.state & CHK_ST_ENABLED)
2257 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2258 break;
2259 case ST_F_QLIMIT:
2260 if (sv->maxqueue)
2261 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2262 break;
2263 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002264 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002265 break;
2266 case ST_F_IID:
2267 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2268 break;
2269 case ST_F_SID:
2270 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2271 break;
2272 case ST_F_THROTTLE:
2273 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2274 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2275 break;
2276 case ST_F_LBTOT:
2277 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2278 break;
2279 case ST_F_TRACKED:
2280 if (sv->track) {
2281 char *fld_track = chunk_newstr(out);
2282 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2283 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2284 }
2285 break;
2286 case ST_F_TYPE:
2287 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2288 break;
2289 case ST_F_RATE:
2290 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2291 break;
2292 case ST_F_RATE_MAX:
2293 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2294 break;
2295 case ST_F_CHECK_STATUS:
2296 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2297 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002298
William Dauchyd3a9a492021-01-25 17:29:03 +01002299 fld_chksts = chunk_newstr(out);
2300 chunk_strcat(out, "* "); // for check in progress
2301 chunk_strcat(out, get_check_status_info(sv->check.status));
2302 if (!(sv->check.state & CHK_ST_INPROGRESS))
2303 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002304 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002305 }
2306 break;
2307 case ST_F_CHECK_CODE:
2308 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2309 sv->check.status >= HCHK_STATUS_L57DATA)
2310 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2311 break;
2312 case ST_F_CHECK_DURATION:
2313 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2314 sv->check.status >= HCHK_STATUS_CHECKED)
2315 metric = mkf_u64(FN_DURATION, sv->check.duration);
2316 break;
2317 case ST_F_CHECK_DESC:
2318 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2319 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2320 break;
2321 case ST_F_LAST_CHK:
2322 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2323 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2324 break;
2325 case ST_F_CHECK_RISE:
2326 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2327 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2328 break;
2329 case ST_F_CHECK_FALL:
2330 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2331 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2332 break;
2333 case ST_F_CHECK_HEALTH:
2334 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2335 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2336 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002337 case ST_F_AGENT_STATUS:
2338 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2339 const char *fld_chksts;
2340
2341 fld_chksts = chunk_newstr(out);
2342 chunk_strcat(out, "* "); // for check in progress
2343 chunk_strcat(out, get_check_status_info(sv->agent.status));
2344 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2345 fld_chksts += 2; // skip "* "
2346 metric = mkf_str(FN_OUTPUT, fld_chksts);
2347 }
2348 break;
2349 case ST_F_AGENT_CODE:
2350 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2351 (sv->agent.status >= HCHK_STATUS_L57DATA))
2352 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2353 break;
2354 case ST_F_AGENT_DURATION:
2355 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2356 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2357 break;
2358 case ST_F_AGENT_DESC:
2359 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2360 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2361 break;
2362 case ST_F_LAST_AGT:
2363 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2364 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2365 break;
2366 case ST_F_AGENT_RISE:
2367 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2368 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2369 break;
2370 case ST_F_AGENT_FALL:
2371 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2372 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2373 break;
2374 case ST_F_AGENT_HEALTH:
2375 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2376 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2377 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002378 case ST_F_REQ_TOT:
2379 if (px->mode == PR_MODE_HTTP)
2380 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2381 break;
2382 case ST_F_HRSP_1XX:
2383 if (px->mode == PR_MODE_HTTP)
2384 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2385 break;
2386 case ST_F_HRSP_2XX:
2387 if (px->mode == PR_MODE_HTTP)
2388 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2389 break;
2390 case ST_F_HRSP_3XX:
2391 if (px->mode == PR_MODE_HTTP)
2392 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2393 break;
2394 case ST_F_HRSP_4XX:
2395 if (px->mode == PR_MODE_HTTP)
2396 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2397 break;
2398 case ST_F_HRSP_5XX:
2399 if (px->mode == PR_MODE_HTTP)
2400 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2401 break;
2402 case ST_F_HRSP_OTHER:
2403 if (px->mode == PR_MODE_HTTP)
2404 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2405 break;
2406 case ST_F_HANAFAIL:
2407 if (ref->observe)
2408 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2409 break;
2410 case ST_F_CLI_ABRT:
2411 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2412 break;
2413 case ST_F_SRV_ABRT:
2414 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2415 break;
2416 case ST_F_LASTSESS:
2417 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2418 break;
2419 case ST_F_QTIME:
2420 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2421 break;
2422 case ST_F_CTIME:
2423 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2424 break;
2425 case ST_F_RTIME:
2426 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2427 break;
2428 case ST_F_TTIME:
2429 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2430 break;
2431 case ST_F_QT_MAX:
2432 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2433 break;
2434 case ST_F_CT_MAX:
2435 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2436 break;
2437 case ST_F_RT_MAX:
2438 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2439 break;
2440 case ST_F_TT_MAX:
2441 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2442 break;
2443 case ST_F_ADDR:
2444 if (flags & STAT_SHLGNDS) {
2445 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2446 case AF_INET:
2447 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2448 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2449 break;
2450 case AF_INET6:
2451 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2452 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2453 break;
2454 case AF_UNIX:
2455 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2456 break;
2457 case -1:
2458 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2459 chunk_strcat(out, strerror(errno));
2460 break;
2461 default: /* address family not supported */
2462 break;
2463 }
2464 }
2465 break;
2466 case ST_F_COOKIE:
2467 if (flags & STAT_SHLGNDS && sv->cookie)
2468 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2469 break;
2470 default:
2471 /* not used for servers. If a specific metric
2472 * is requested, return an error. Otherwise continue.
2473 */
2474 if (selected_field != NULL)
2475 return 0;
2476 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002477 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002478 stats[current_field] = metric;
2479 if (selected_field != NULL)
2480 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002481 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002482 return 1;
2483}
2484
2485/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02002486 * from stream interface <si>, and server state <state>. The caller is
2487 * responsible for clearing the trash if needed. Returns non-zero if it emits
2488 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002489 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002490static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002491{
2492 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002493 struct stats_module *mod;
2494 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2495 size_t stats_count = ST_F_TOTAL_FIELDS;
2496
2497 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002498
William Dauchyd3a9a492021-01-25 17:29:03 +01002499 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats,
2500 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002501 return 0;
2502
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002503 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2504 void *counters;
2505
2506 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2507 continue;
2508
2509 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2510 stats_count += mod->stats_count;
2511 continue;
2512 }
2513
2514 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2515 mod->fill_stats(counters, stats + stats_count);
2516 stats_count += mod->stats_count;
2517 }
2518
2519 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002520}
2521
William Dauchyda3b4662021-01-25 17:29:01 +01002522/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002523 */
William Dauchyda3b4662021-01-25 17:29:01 +01002524static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002525{
William Dauchyda3b4662021-01-25 17:29:01 +01002526 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002527 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002528
William Dauchyda3b4662021-01-25 17:29:01 +01002529 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002530 for (srv = px->srv; srv; srv = srv->next) {
2531 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002532 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002533 if (srv_currently_usable(srv) &&
2534 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002535 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002536 }
William Dauchyda3b4662021-01-25 17:29:01 +01002537 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002538 }
2539
Willy Tarreaubd715102020-10-23 22:44:30 +02002540 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2541 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002542 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002543 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2544
William Dauchyda3b4662021-01-25 17:29:01 +01002545 /* use tmp variable then assign result to make gcc happy */
2546 *nbup = nbup_tmp;
2547 *nbsrv = nbsrv_tmp;
2548 *totuw = totuw_tmp;
2549}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002550
William Dauchyda3b4662021-01-25 17:29:01 +01002551/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2552 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2553 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2554 * this value, or if the selected field is not implemented for backends, the
2555 * function returns 0, otherwise, it returns 1. <flags> can take the value
2556 * STAT_SHLGNDS.
2557 */
2558int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2559 enum stat_field *selected_field)
2560{
2561 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2562 long long be_samples_counter;
2563 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2564 struct buffer *out = get_trash_chunk();
2565 int nbup, nbsrv, totuw;
2566 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002567
William Dauchyda3b4662021-01-25 17:29:01 +01002568 if (len < ST_F_TOTAL_FIELDS)
2569 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002570
William Dauchyda3b4662021-01-25 17:29:01 +01002571 nbup = nbsrv = totuw = 0;
2572 /* some srv values compute for later if we either select all fields or
2573 * need them for one of the mentioned ones */
2574 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2575 *selected_field == ST_F_UWEIGHT)
2576 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002577
William Dauchyda3b4662021-01-25 17:29:01 +01002578 /* same here but specific to time fields */
2579 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2580 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2581 *selected_field == ST_F_TTIME) {
2582 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2583 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2584 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002585 }
2586
William Dauchyda3b4662021-01-25 17:29:01 +01002587 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2588 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002589
William Dauchyda3b4662021-01-25 17:29:01 +01002590 switch (current_field) {
2591 case ST_F_PXNAME:
2592 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2593 break;
2594 case ST_F_SVNAME:
2595 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2596 break;
2597 case ST_F_MODE:
2598 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2599 break;
2600 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002601 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002602 break;
2603 case ST_F_QMAX:
2604 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2605 break;
2606 case ST_F_SCUR:
2607 metric = mkf_u32(0, px->beconn);
2608 break;
2609 case ST_F_SMAX:
2610 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2611 break;
2612 case ST_F_SLIM:
2613 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2614 break;
2615 case ST_F_STOT:
2616 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2617 break;
2618 case ST_F_BIN:
2619 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2620 break;
2621 case ST_F_BOUT:
2622 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2623 break;
2624 case ST_F_DREQ:
2625 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2626 break;
2627 case ST_F_DRESP:
2628 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2629 break;
2630 case ST_F_ECON:
2631 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2632 break;
2633 case ST_F_ERESP:
2634 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2635 break;
2636 case ST_F_WRETR:
2637 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2638 break;
2639 case ST_F_WREDIS:
2640 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2641 break;
2642 case ST_F_WREW:
2643 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2644 break;
2645 case ST_F_EINT:
2646 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2647 break;
2648 case ST_F_CONNECT:
2649 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2650 break;
2651 case ST_F_REUSE:
2652 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2653 break;
2654 case ST_F_STATUS:
2655 fld = chunk_newstr(out);
2656 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2657 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2658 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2659 metric = mkf_str(FO_STATUS, fld);
2660 break;
2661 case ST_F_WEIGHT:
2662 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2663 break;
2664 case ST_F_UWEIGHT:
2665 metric = mkf_u32(FN_AVG, totuw);
2666 break;
2667 case ST_F_ACT:
2668 metric = mkf_u32(0, px->srv_act);
2669 break;
2670 case ST_F_BCK:
2671 metric = mkf_u32(0, px->srv_bck);
2672 break;
2673 case ST_F_CHKDOWN:
2674 metric = mkf_u64(FN_COUNTER, px->down_trans);
2675 break;
2676 case ST_F_LASTCHG:
2677 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2678 break;
2679 case ST_F_DOWNTIME:
2680 if (px->srv)
2681 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2682 break;
2683 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002684 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002685 break;
2686 case ST_F_IID:
2687 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2688 break;
2689 case ST_F_SID:
2690 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2691 break;
2692 case ST_F_LBTOT:
2693 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2694 break;
2695 case ST_F_TYPE:
2696 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2697 break;
2698 case ST_F_RATE:
2699 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2700 break;
2701 case ST_F_RATE_MAX:
2702 metric = mkf_u32(0, px->be_counters.sps_max);
2703 break;
2704 case ST_F_COOKIE:
2705 if (flags & STAT_SHLGNDS && px->cookie_name)
2706 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2707 break;
2708 case ST_F_ALGO:
2709 if (flags & STAT_SHLGNDS)
2710 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2711 break;
2712 case ST_F_REQ_TOT:
2713 if (px->mode == PR_MODE_HTTP)
2714 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2715 break;
2716 case ST_F_HRSP_1XX:
2717 if (px->mode == PR_MODE_HTTP)
2718 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2719 break;
2720 case ST_F_HRSP_2XX:
2721 if (px->mode == PR_MODE_HTTP)
2722 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2723 break;
2724 case ST_F_HRSP_3XX:
2725 if (px->mode == PR_MODE_HTTP)
2726 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2727 break;
2728 case ST_F_HRSP_4XX:
2729 if (px->mode == PR_MODE_HTTP)
2730 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2731 break;
2732 case ST_F_HRSP_5XX:
2733 if (px->mode == PR_MODE_HTTP)
2734 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2735 break;
2736 case ST_F_HRSP_OTHER:
2737 if (px->mode == PR_MODE_HTTP)
2738 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2739 break;
2740 case ST_F_CACHE_LOOKUPS:
2741 if (px->mode == PR_MODE_HTTP)
2742 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2743 break;
2744 case ST_F_CACHE_HITS:
2745 if (px->mode == PR_MODE_HTTP)
2746 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2747 break;
2748 case ST_F_CLI_ABRT:
2749 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2750 break;
2751 case ST_F_SRV_ABRT:
2752 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2753 break;
2754 case ST_F_COMP_IN:
2755 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2756 break;
2757 case ST_F_COMP_OUT:
2758 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2759 break;
2760 case ST_F_COMP_BYP:
2761 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2762 break;
2763 case ST_F_COMP_RSP:
2764 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2765 break;
2766 case ST_F_LASTSESS:
2767 metric = mkf_s32(FN_AGE, be_lastsession(px));
2768 break;
2769 case ST_F_QTIME:
2770 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2771 break;
2772 case ST_F_CTIME:
2773 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2774 break;
2775 case ST_F_RTIME:
2776 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2777 break;
2778 case ST_F_TTIME:
2779 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2780 break;
2781 case ST_F_QT_MAX:
2782 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2783 break;
2784 case ST_F_CT_MAX:
2785 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2786 break;
2787 case ST_F_RT_MAX:
2788 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2789 break;
2790 case ST_F_TT_MAX:
2791 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2792 break;
2793 default:
2794 /* not used for backends. If a specific metric
2795 * is requested, return an error. Otherwise continue.
2796 */
2797 if (selected_field != NULL)
2798 return 0;
2799 continue;
2800 }
2801 stats[current_field] = metric;
2802 if (selected_field != NULL)
2803 break;
2804 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002805 return 1;
2806}
2807
2808/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002809 * interface <si>. The caller is responsible for clearing the trash if needed.
2810 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002811 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002812static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002813{
2814 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002815 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2816 struct stats_module *mod;
2817 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002818
2819 if (!(px->cap & PR_CAP_BE))
2820 return 0;
2821
2822 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2823 return 0;
2824
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002825 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2826
William Dauchyda3b4662021-01-25 17:29:01 +01002827 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002828 return 0;
2829
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002830 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2831 struct extra_counters *counters;
2832
2833 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2834 continue;
2835
2836 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2837 stats_count += mod->stats_count;
2838 continue;
2839 }
2840
2841 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2842 mod->fill_stats(counters, stats + stats_count);
2843 stats_count += mod->stats_count;
2844 }
2845
2846 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002847}
2848
2849/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2850 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2851 * for clearing the trash if needed.
2852 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002853static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002854{
2855 struct appctx *appctx = __objt_appctx(si->end);
2856 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002857 struct stats_module *mod;
2858 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002859
2860 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2861 /* A form to enable/disable this proxy servers */
2862
2863 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2864 scope_txt[0] = 0;
2865 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002866 const char *scope_ptr = stats_scope_ptr(appctx, si);
2867
William Lallemand74c24fb2016-11-21 17:18:36 +01002868 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002869 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002870 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2871 }
2872
2873 chunk_appendf(&trash,
2874 "<form method=\"post\">");
2875 }
2876
2877 /* print a new table */
2878 chunk_appendf(&trash,
2879 "<table class=\"tbl\" width=\"100%%\">\n"
2880 "<tr class=\"titre\">"
2881 "<th class=\"pxname\" width=\"10%%\">");
2882
2883 chunk_appendf(&trash,
2884 "<a name=\"%s\"></a>%s"
2885 "<a class=px href=\"#%s\">%s</a>",
2886 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002887 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002888 px->id, px->id);
2889
Willy Tarreau676c29e2019-10-09 10:50:01 +02002890 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002891 /* cap, mode, id */
2892 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2893 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2894 px->uuid);
2895 chunk_appendf(&trash, "</div>");
2896 }
2897
2898 chunk_appendf(&trash,
2899 "%s</th>"
2900 "<th class=\"%s\" width=\"90%%\">%s</th>"
2901 "</tr>\n"
2902 "</table>\n"
2903 "<table class=\"tbl\" width=\"100%%\">\n"
2904 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002905 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002906 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2907
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002908 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002909 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002910 if ((px->cap & PR_CAP_BE) && px->srv)
2911 chunk_appendf(&trash,
2912 "<th rowspan=2 width=1><input type=\"checkbox\" "
2913 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2914 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2915 px->id,
2916 px->id);
2917 else
2918 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002919 }
2920
2921 chunk_appendf(&trash,
2922 "<th rowspan=2></th>"
2923 "<th colspan=3>Queue</th>"
2924 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2925 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2926 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002927 "<th colspan=9>Server</th>");
2928
2929 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2930 // calculate the count of module for colspan attribute
2931 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2932 ++stats_module_len;
2933 }
2934 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2935 stats_module_len);
2936 }
2937
2938 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002939 "</tr>\n"
2940 "<tr class=\"titre\">"
2941 "<th>Cur</th><th>Max</th><th>Limit</th>"
2942 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2943 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2944 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2945 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2946 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2947 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002948 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002949
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002950 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2951 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2952 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2953 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002954 }
2955
2956 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002957}
2958
2959/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2960 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2961 */
2962static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2963{
2964 struct appctx *appctx = __objt_appctx(si->end);
2965 chunk_appendf(&trash, "</table>");
2966
2967 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2968 /* close the form used to enable/disable this proxy servers */
2969 chunk_appendf(&trash,
2970 "Choose the action to perform on the checked servers : "
2971 "<select name=action>"
2972 "<option value=\"\"></option>"
2973 "<option value=\"ready\">Set state to READY</option>"
2974 "<option value=\"drain\">Set state to DRAIN</option>"
2975 "<option value=\"maint\">Set state to MAINT</option>"
2976 "<option value=\"dhlth\">Health: disable checks</option>"
2977 "<option value=\"ehlth\">Health: enable checks</option>"
2978 "<option value=\"hrunn\">Health: force UP</option>"
2979 "<option value=\"hnolb\">Health: force NOLB</option>"
2980 "<option value=\"hdown\">Health: force DOWN</option>"
2981 "<option value=\"dagent\">Agent: disable checks</option>"
2982 "<option value=\"eagent\">Agent: enable checks</option>"
2983 "<option value=\"arunn\">Agent: force UP</option>"
2984 "<option value=\"adown\">Agent: force DOWN</option>"
2985 "<option value=\"shutdown\">Kill Sessions</option>"
2986 "</select>"
2987 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2988 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2989 "</form>",
2990 px->uuid);
2991 }
2992
2993 chunk_appendf(&trash, "<p>\n");
2994}
2995
2996/*
2997 * Dumps statistics for a proxy. The output is sent to the stream interface's
2998 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2999 * buffer space, or non-zero if everything completed. This function is used
3000 * both by the CLI and the HTTP entry points, and is able to dump the output
3001 * in HTML or CSV formats. If the later, <uri> must be NULL.
3002 */
Christopher Fauletef779222018-10-31 08:47:01 +01003003int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
3004 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003005{
3006 struct appctx *appctx = __objt_appctx(si->end);
3007 struct stream *s = si_strm(si);
3008 struct channel *rep = si_ic(si);
3009 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3010 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01003011
3012 chunk_reset(&trash);
3013
3014 switch (appctx->ctx.stats.px_st) {
3015 case STAT_PX_ST_INIT:
3016 /* we are on a new proxy */
3017 if (uri && uri->scope) {
3018 /* we have a limited scope, we have to check the proxy name */
3019 struct stat_scope *scope;
3020 int len;
3021
3022 len = strlen(px->id);
3023 scope = uri->scope;
3024
3025 while (scope) {
3026 /* match exact proxy name */
3027 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3028 break;
3029
3030 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003031 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003032 break;
3033 scope = scope->next;
3034 }
3035
3036 /* proxy name not found : don't dump anything */
3037 if (scope == NULL)
3038 return 1;
3039 }
3040
3041 /* if the user has requested a limited output and the proxy
3042 * name does not match, skip it.
3043 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003044 if (appctx->ctx.stats.scope_len) {
3045 const char *scope_ptr = stats_scope_ptr(appctx, si);
3046
3047 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
3048 return 1;
3049 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003050
3051 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3052 (appctx->ctx.stats.iid != -1) &&
3053 (px->uuid != appctx->ctx.stats.iid))
3054 return 1;
3055
3056 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
3057 /* fall through */
3058
3059 case STAT_PX_ST_TH:
3060 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02003061 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003062 if (!stats_putchk(rep, htx, &trash))
3063 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003064 }
3065
3066 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
3067 /* fall through */
3068
3069 case STAT_PX_ST_FE:
3070 /* print the frontend */
3071 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003072 if (!stats_putchk(rep, htx, &trash))
3073 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003074 }
3075
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003076 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01003077 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
3078 /* fall through */
3079
3080 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003081 /* obj2 points to listeners list as initialized above */
3082 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003083 if (htx) {
3084 if (htx_almost_full(htx))
3085 goto full;
3086 }
3087 else {
3088 if (buffer_almost_full(&rep->buf))
3089 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003090 }
3091
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003092 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003093 if (!l->counters)
3094 continue;
3095
3096 if (appctx->ctx.stats.flags & STAT_BOUND) {
3097 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
3098 break;
3099
3100 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
3101 continue;
3102 }
3103
3104 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02003105 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003106 if (!stats_putchk(rep, htx, &trash))
3107 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003108 }
3109 }
3110
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003111 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01003112 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
3113 /* fall through */
3114
3115 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003116 /* obj2 points to servers list as initialized above.
3117 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003118 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003119 * Temporarily increment its refcount to prevent its
3120 * anticipated cleaning. Call free_server to release it.
3121 */
3122 for (; appctx->ctx.stats.obj2 != NULL;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003123 appctx->ctx.stats.obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003124
3125 sv = appctx->ctx.stats.obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003126 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003127
Christopher Fauletef779222018-10-31 08:47:01 +01003128 if (htx) {
3129 if (htx_almost_full(htx))
3130 goto full;
3131 }
3132 else {
3133 if (buffer_almost_full(&rep->buf))
3134 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003135 }
3136
William Lallemand74c24fb2016-11-21 17:18:36 +01003137 if (appctx->ctx.stats.flags & STAT_BOUND) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003138 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003139 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003140 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003141 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003142
3143 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
3144 continue;
3145 }
3146
Willy Tarreau3e320362020-10-23 17:28:57 +02003147 /* do not report disabled servers */
3148 if (appctx->ctx.stats.flags & STAT_HIDE_MAINT &&
3149 sv->cur_admin & SRV_ADMF_MAINT) {
3150 continue;
3151 }
3152
William Lallemand74c24fb2016-11-21 17:18:36 +01003153 svs = sv;
3154 while (svs->track)
3155 svs = svs->track;
3156
3157 /* do not report servers which are DOWN and not changing state */
3158 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003159 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3160 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003161 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3162 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3163 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3164 continue;
3165 }
3166
Willy Tarreau43241ff2019-10-09 11:27:51 +02003167 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003168 if (!stats_putchk(rep, htx, &trash))
3169 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003170 }
3171 } /* for sv */
3172
3173 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
3174 /* fall through */
3175
3176 case STAT_PX_ST_BE:
3177 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02003178 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003179 if (!stats_putchk(rep, htx, &trash))
3180 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003181 }
3182
3183 appctx->ctx.stats.px_st = STAT_PX_ST_END;
3184 /* fall through */
3185
3186 case STAT_PX_ST_END:
3187 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3188 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003189 if (!stats_putchk(rep, htx, &trash))
3190 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003191 }
3192
3193 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
3194 /* fall through */
3195
3196 case STAT_PX_ST_FIN:
3197 return 1;
3198
3199 default:
3200 /* unknown state, we should put an abort() here ! */
3201 return 1;
3202 }
Christopher Fauletef779222018-10-31 08:47:01 +01003203
3204 full:
3205 si_rx_room_blk(si);
3206 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003207}
3208
3209/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3210 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3211 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003212static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003213{
3214 /* WARNING! This must fit in the first buffer !!! */
3215 chunk_appendf(&trash,
3216 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3217 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3218 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003219 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003220 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3221 "<style type=\"text/css\"><!--\n"
3222 "body {"
3223 " font-family: arial, helvetica, sans-serif;"
3224 " font-size: 12px;"
3225 " font-weight: normal;"
3226 " color: black;"
3227 " background: white;"
3228 "}\n"
3229 "th,td {"
3230 " font-size: 10px;"
3231 "}\n"
3232 "h1 {"
3233 " font-size: x-large;"
3234 " margin-bottom: 0.5em;"
3235 "}\n"
3236 "h2 {"
3237 " font-family: helvetica, arial;"
3238 " font-size: x-large;"
3239 " font-weight: bold;"
3240 " font-style: italic;"
3241 " color: #6020a0;"
3242 " margin-top: 0em;"
3243 " margin-bottom: 0em;"
3244 "}\n"
3245 "h3 {"
3246 " font-family: helvetica, arial;"
3247 " font-size: 16px;"
3248 " font-weight: bold;"
3249 " color: #b00040;"
3250 " background: #e8e8d0;"
3251 " margin-top: 0em;"
3252 " margin-bottom: 0em;"
3253 "}\n"
3254 "li {"
3255 " margin-top: 0.25em;"
3256 " margin-right: 2em;"
3257 "}\n"
3258 ".hr {margin-top: 0.25em;"
3259 " border-color: black;"
3260 " border-bottom-style: solid;"
3261 "}\n"
3262 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3263 ".total {background: #20D0D0;color: #ffff80;}\n"
3264 ".frontend {background: #e8e8d0;}\n"
3265 ".socket {background: #d0d0d0;}\n"
3266 ".backend {background: #e8e8d0;}\n"
3267 ".active_down {background: #ff9090;}\n"
3268 ".active_going_up {background: #ffd020;}\n"
3269 ".active_going_down {background: #ffffa0;}\n"
3270 ".active_up {background: #c0ffc0;}\n"
3271 ".active_nolb {background: #20a0ff;}\n"
3272 ".active_draining {background: #20a0FF;}\n"
3273 ".active_no_check {background: #e0e0e0;}\n"
3274 ".backup_down {background: #ff9090;}\n"
3275 ".backup_going_up {background: #ff80ff;}\n"
3276 ".backup_going_down {background: #c060ff;}\n"
3277 ".backup_up {background: #b0d0ff;}\n"
3278 ".backup_nolb {background: #90b0e0;}\n"
3279 ".backup_draining {background: #cc9900;}\n"
3280 ".backup_no_check {background: #e0e0e0;}\n"
3281 ".maintain {background: #c07820;}\n"
3282 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3283 "\n"
3284 "a.px:link {color: #ffff40; text-decoration: none;}"
3285 "a.px:visited {color: #ffff40; text-decoration: none;}"
3286 "a.px:hover {color: #ffffff; text-decoration: none;}"
3287 "a.lfsb:link {color: #000000; text-decoration: none;}"
3288 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3289 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3290 "\n"
3291 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3292 "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"
3293 "table.tbl td.ac { text-align: center;}\n"
3294 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3295 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3296 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3297 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3298 "\n"
3299 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3300 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3301 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3302 "table.det { border-collapse: collapse; border-style: none; }\n"
3303 "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"
3304 "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"
3305 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003306 "div.tips {\n"
3307 " display:block;\n"
3308 " visibility:hidden;\n"
3309 " z-index:2147483647;\n"
3310 " position:absolute;\n"
3311 " padding:2px 4px 3px;\n"
3312 " background:#f0f060; color:#000000;\n"
3313 " border:1px solid #7040c0;\n"
3314 " white-space:nowrap;\n"
3315 " font-style:normal;font-size:11px;font-weight:normal;\n"
3316 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3317 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3318 "}\n"
3319 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003320 "@media (prefers-color-scheme: dark) {\n"
3321 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3322 " h1 { color: #a265e0!important; }\n"
3323 " h2 { color: #a265e0; }\n"
3324 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3325 " a { color: #3391ff; }\n"
3326 " input { background-color: #2f3437; }\n"
3327 " .hr { border-color: #8c8273; }\n"
3328 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3329 " .frontend {background: #2f3437;}\n"
3330 " .backend {background: #2f3437;}\n"
3331 " .active_down {background: #760000;}\n"
3332 " .active_going_up {background: #b99200;}\n"
3333 " .active_going_down {background: #6c6c00;}\n"
3334 " .active_up {background: #165900;}\n"
3335 " .active_nolb {background: #006ab9;}\n"
3336 " .active_draining {background: #006ab9;}\n"
3337 " .active_no_check {background: #2a2d2f;}\n"
3338 " .backup_down {background: #760000;}\n"
3339 " .backup_going_up {background: #7f007f;}\n"
3340 " .backup_going_down {background: #580092;}\n"
3341 " .backup_up {background: #2e3234;}\n"
3342 " .backup_nolb {background: #1e3c6a;}\n"
3343 " .backup_draining {background: #a37a00;}\n"
3344 " .backup_no_check {background: #2a2d2f;}\n"
3345 " .maintain {background: #9a601a;}\n"
3346 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3347 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3348 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3349 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3350 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3351 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3352 " table.tbl th.empty { background-color: #181a1b; }\n"
3353 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3354 " table.tbl th { border-color: #808080; }\n"
3355 " table.tbl td { border-color: #808080; }\n"
3356 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3357 " div.tips {\n"
3358 " background:#8e8e0d;\n"
3359 " color:#e8e6e3;\n"
3360 " border-color: #4e2c86;\n"
3361 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3362 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3363 " box-shadow: #60686c 2px 2px 3px;\n"
3364 " }\n"
3365 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003366 "-->\n"
3367 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003368 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01003369 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003370 );
3371}
3372
3373/* Dumps the HTML stats information block to the trash for and uses the state from
3374 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
3375 * for clearing the trash if needed.
3376 */
3377static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
3378{
3379 struct appctx *appctx = __objt_appctx(si->end);
3380 unsigned int up = (now.tv_sec - start_date.tv_sec);
3381 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01003382 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02003383 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3384
3385 /* Turn the bytes per second to bits per second and take care of the
3386 * usual ethernet overhead in order to help figure how far we are from
3387 * interface saturation since it's the only case which usually matters.
3388 * For this we count the total size of an Ethernet frame on the wire
3389 * including preamble and IFG (1538) for the largest TCP segment it
3390 * transports (1448 with TCP timestamps). This is not valid for smaller
3391 * packets (under-estimated), but it gives a reasonably accurate
3392 * estimation of how far we are from uplink saturation.
3393 */
3394 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003395
3396 /* WARNING! this has to fit the first packet too.
3397 * We are around 3.5 kB, add adding entries will
3398 * become tricky if we want to support 4kB buffers !
3399 */
3400 chunk_appendf(&trash,
3401 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3402 PRODUCT_NAME "%s</a></h1>\n"
3403 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3404 "<hr width=\"100%%\" class=\"hr\">\n"
3405 "<h3>&gt; General process information</h3>\n"
3406 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003407 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003408 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3409 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3410 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003411 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003412 "Running tasks: %d/%d; idle = %d %%<br>\n"
3413 "</td><td align=\"center\" nowrap>\n"
3414 "<table class=\"lgd\"><tr>\n"
3415 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3416 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3417 "</tr><tr>\n"
3418 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3419 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3420 "</tr><tr>\n"
3421 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3422 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3423 "</tr><tr>\n"
3424 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3425 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3426 "</tr><tr>\n"
3427 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3428 "</tr><tr>\n"
3429 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3430 "</tr></table>\n"
3431 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3432 "</td>"
3433 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3434 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3435 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003436 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
3437 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
3438 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3439 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
3440 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003441 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003442 up / 86400, (up % 86400) / 3600,
3443 (up % 3600) / 60, (up % 60),
3444 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3445 global.rlimit_memmax ? " MB" : "",
3446 global.rlimit_nofile,
3447 global.maxsock, global.maxconn, global.maxpipes,
3448 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003449 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3450 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau61369892021-09-30 08:52:11 +02003451 total_run_queues(), total_allocated_tasks(), sched_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003452 );
3453
3454 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003455 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003456 scope_txt[appctx->ctx.stats.scope_len] = '\0';
3457
3458 chunk_appendf(&trash,
3459 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
3460 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
3461 STAT_SCOPE_TXT_MAXLEN);
3462
3463 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3464 scope_txt[0] = 0;
3465 if (appctx->ctx.stats.scope_len) {
3466 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003467 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003468 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3469 }
3470
3471 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
3472 chunk_appendf(&trash,
3473 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3474 uri->uri_prefix,
3475 "",
3476 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3477 scope_txt);
3478 else
3479 chunk_appendf(&trash,
3480 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3481 uri->uri_prefix,
3482 ";up",
3483 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3484 scope_txt);
3485
3486 if (uri->refresh > 0) {
3487 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
3488 chunk_appendf(&trash,
3489 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3490 uri->uri_prefix,
3491 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3492 "",
3493 scope_txt);
3494 else
3495 chunk_appendf(&trash,
3496 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3497 uri->uri_prefix,
3498 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3499 ";norefresh",
3500 scope_txt);
3501 }
3502
3503 chunk_appendf(&trash,
3504 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3505 uri->uri_prefix,
3506 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3507 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3508 scope_txt);
3509
3510 chunk_appendf(&trash,
3511 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3512 uri->uri_prefix,
3513 (uri->refresh > 0) ? ";norefresh" : "",
3514 scope_txt);
3515
3516 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003517 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3518 uri->uri_prefix,
3519 (uri->refresh > 0) ? ";norefresh" : "",
3520 scope_txt, uri->uri_prefix);
3521
3522 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003523 "</ul></td>"
3524 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3525 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3526 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3527 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3528 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3529 "</ul>"
3530 "</td>"
3531 "</tr></table>\n"
3532 ""
3533 );
3534
3535 if (appctx->ctx.stats.st_code) {
3536 switch (appctx->ctx.stats.st_code) {
3537 case STAT_STATUS_DONE:
3538 chunk_appendf(&trash,
3539 "<p><div class=active_up>"
3540 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3541 "Action processed successfully."
3542 "</div>\n", uri->uri_prefix,
3543 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3544 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3545 scope_txt);
3546 break;
3547 case STAT_STATUS_NONE:
3548 chunk_appendf(&trash,
3549 "<p><div class=active_going_down>"
3550 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3551 "Nothing has changed."
3552 "</div>\n", uri->uri_prefix,
3553 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3554 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3555 scope_txt);
3556 break;
3557 case STAT_STATUS_PART:
3558 chunk_appendf(&trash,
3559 "<p><div class=active_going_down>"
3560 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3561 "Action partially processed.<br>"
3562 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3563 "</div>\n", uri->uri_prefix,
3564 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3565 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3566 scope_txt);
3567 break;
3568 case STAT_STATUS_ERRP:
3569 chunk_appendf(&trash,
3570 "<p><div class=active_down>"
3571 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3572 "Action not processed because of invalid parameters."
3573 "<ul>"
3574 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003575 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003576 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3577 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3578 "</ul>"
3579 "</div>\n", uri->uri_prefix,
3580 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3581 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3582 scope_txt);
3583 break;
3584 case STAT_STATUS_EXCD:
3585 chunk_appendf(&trash,
3586 "<p><div class=active_down>"
3587 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3588 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3589 "You should retry with less servers at a time.</b>"
3590 "</div>\n", uri->uri_prefix,
3591 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3592 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3593 scope_txt);
3594 break;
3595 case STAT_STATUS_DENY:
3596 chunk_appendf(&trash,
3597 "<p><div class=active_down>"
3598 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3599 "<b>Action denied.</b>"
3600 "</div>\n", uri->uri_prefix,
3601 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3602 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3603 scope_txt);
3604 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003605 case STAT_STATUS_IVAL:
3606 chunk_appendf(&trash,
3607 "<p><div class=active_down>"
3608 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3609 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3610 "</div>\n", uri->uri_prefix,
3611 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3612 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3613 scope_txt);
3614 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003615 default:
3616 chunk_appendf(&trash,
3617 "<p><div class=active_no_check>"
3618 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3619 "Unexpected result."
3620 "</div>\n", uri->uri_prefix,
3621 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3622 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3623 scope_txt);
3624 }
3625 chunk_appendf(&trash, "<p>\n");
3626 }
3627}
3628
3629/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3630 * for clearing the trash if needed.
3631 */
3632static void stats_dump_html_end()
3633{
3634 chunk_appendf(&trash, "</body></html>\n");
3635}
3636
Simon Horman05ee2132017-01-04 09:37:25 +01003637/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3638 * for clearing it if needed.
3639 */
3640static void stats_dump_json_header()
3641{
3642 chunk_strcat(&trash, "[");
3643}
3644
3645
3646/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3647 * for clearing the trash if needed.
3648 */
3649static void stats_dump_json_end()
3650{
3651 chunk_strcat(&trash, "]");
3652}
3653
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003654/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3655 * a pointer to the current server/listener.
3656 */
3657static int stats_dump_proxies(struct stream_interface *si,
3658 struct htx *htx,
3659 struct uri_auth *uri)
3660{
3661 struct appctx *appctx = __objt_appctx(si->end);
3662 struct channel *rep = si_ic(si);
3663 struct proxy *px;
3664
3665 /* dump proxies */
3666 while (appctx->ctx.stats.obj1) {
3667 if (htx) {
3668 if (htx_almost_full(htx))
3669 goto full;
3670 }
3671 else {
3672 if (buffer_almost_full(&rep->buf))
3673 goto full;
3674 }
3675
3676 px = appctx->ctx.stats.obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003677 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003678 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003679 * This change allows retrieving stats from "old" proxies after a reload.
3680 */
William Lallemand85a16b22021-08-03 13:18:11 +02003681 if (!(px->disabled & PR_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003682 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003683 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
3684 return 0;
3685 }
3686
3687 appctx->ctx.stats.obj1 = px->next;
3688 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3689 }
3690
3691 return 1;
3692
3693 full:
3694 si_rx_room_blk(si);
3695 return 0;
3696}
3697
William Lallemand74c24fb2016-11-21 17:18:36 +01003698/* This function dumps statistics onto the stream interface's read buffer in
3699 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3700 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3701 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3702 * and the stream must be closed, or -1 in case of any error. This function is
3703 * used by both the CLI and the HTTP handlers.
3704 */
Christopher Fauletef779222018-10-31 08:47:01 +01003705static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
3706 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003707{
3708 struct appctx *appctx = __objt_appctx(si->end);
3709 struct channel *rep = si_ic(si);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003710 enum stats_domain domain = appctx->ctx.stats.domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003711
3712 chunk_reset(&trash);
3713
3714 switch (appctx->st2) {
3715 case STAT_ST_INIT:
3716 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3717 /* fall through */
3718
3719 case STAT_ST_HEAD:
3720 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003721 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003722 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3723 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003724 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003725 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003726 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003727 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003728
Christopher Fauletef779222018-10-31 08:47:01 +01003729 if (!stats_putchk(rep, htx, &trash))
3730 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003731
Christopher Faulet6338a082019-09-09 15:50:54 +02003732 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3733 appctx->st2 = STAT_ST_FIN;
3734 return 1;
3735 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003736 appctx->st2 = STAT_ST_INFO;
3737 /* fall through */
3738
3739 case STAT_ST_INFO:
3740 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3741 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003742 if (!stats_putchk(rep, htx, &trash))
3743 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003744 }
3745
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003746 if (domain == STATS_DOMAIN_PROXY)
3747 appctx->ctx.stats.obj1 = proxies_list;
3748
William Lallemand74c24fb2016-11-21 17:18:36 +01003749 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3750 appctx->st2 = STAT_ST_LIST;
3751 /* fall through */
3752
3753 case STAT_ST_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003754 switch (domain) {
3755 case STATS_DOMAIN_DNS:
Emeric Brund3b44952021-01-06 16:01:02 +01003756 if (!stats_dump_resolvers(si, stat_l[domain],
3757 stat_count[domain],
3758 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003759 return 0;
3760 }
3761 break;
3762
3763 case STATS_DOMAIN_PROXY:
3764 default:
3765 /* dump proxies */
3766 if (!stats_dump_proxies(si, htx, uri))
3767 return 0;
3768 break;
3769 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003770
3771 appctx->st2 = STAT_ST_END;
3772 /* fall through */
3773
3774 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003775 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3776 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3777 stats_dump_html_end();
3778 else
3779 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003780 if (!stats_putchk(rep, htx, &trash))
3781 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003782 }
3783
3784 appctx->st2 = STAT_ST_FIN;
3785 /* fall through */
3786
3787 case STAT_ST_FIN:
3788 return 1;
3789
3790 default:
3791 /* unknown state ! */
3792 appctx->st2 = STAT_ST_FIN;
3793 return -1;
3794 }
Christopher Fauletef779222018-10-31 08:47:01 +01003795
3796 full:
3797 si_rx_room_blk(si);
3798 return 0;
3799
William Lallemand74c24fb2016-11-21 17:18:36 +01003800}
3801
3802/* We reached the stats page through a POST request. The appctx is
3803 * expected to have already been allocated by the caller.
3804 * Parse the posted data and enable/disable servers if necessary.
3805 * Returns 1 if request was parsed or zero if it needs more data.
3806 */
3807static int stats_process_http_post(struct stream_interface *si)
3808{
3809 struct stream *s = si_strm(si);
3810 struct appctx *appctx = objt_appctx(si->end);
3811
3812 struct proxy *px = NULL;
3813 struct server *sv = NULL;
3814
3815 char key[LINESIZE];
3816 int action = ST_ADM_ACTION_NONE;
3817 int reprocess = 0;
3818
3819 int total_servers = 0;
3820 int altered_servers = 0;
3821
3822 char *first_param, *cur_param, *next_param, *end_params;
3823 char *st_cur_param = NULL;
3824 char *st_next_param = NULL;
3825
Christopher Fauleta3618372018-12-13 21:59:56 +01003826 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003827
Christopher Fauletb7f88902019-07-15 21:56:43 +02003828 struct htx *htx = htxbuf(&s->req.buf);
3829 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003830
Christopher Fauletb7f88902019-07-15 21:56:43 +02003831 /* we need more data */
3832 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3833 /* check if we can receive more */
3834 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3835 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3836 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003837 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003838 goto wait;
3839 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003840
Christopher Fauletb7f88902019-07-15 21:56:43 +02003841 /* The request was fully received. Copy data */
3842 blk = htx_get_head_blk(htx);
3843 while (blk) {
3844 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003845
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003846 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003847 break;
3848 if (type == HTX_BLK_DATA) {
3849 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003850
Christopher Fauletb7f88902019-07-15 21:56:43 +02003851 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003852 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3853 goto out;
3854 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003855 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003856 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003857 }
3858
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003859 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003860 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003861 cur_param = next_param = end_params;
3862 *end_params = '\0';
3863
3864 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3865
3866 /*
3867 * Parse the parameters in reverse order to only store the last value.
3868 * From the html form, the backend and the action are at the end.
3869 */
3870 while (cur_param > first_param) {
3871 char *value;
3872 int poffset, plen;
3873
3874 cur_param--;
3875
3876 if ((*cur_param == '&') || (cur_param == first_param)) {
3877 reprocess_servers:
3878 /* Parse the key */
3879 poffset = (cur_param != first_param ? 1 : 0);
3880 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3881 if ((plen > 0) && (plen <= sizeof(key))) {
3882 strncpy(key, cur_param + poffset, plen);
3883 key[plen - 1] = '\0';
3884 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003885 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003886 goto out;
3887 }
3888
3889 /* Parse the value */
3890 value = key;
3891 while (*value != '\0' && *value != '=') {
3892 value++;
3893 }
3894 if (*value == '=') {
3895 /* Ok, a value is found, we can mark the end of the key */
3896 *value++ = '\0';
3897 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003898 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003899 break;
3900
3901 /* Now we can check the key to see what to do */
3902 if (!px && (strcmp(key, "b") == 0)) {
3903 if ((px = proxy_be_by_name(value)) == NULL) {
3904 /* the backend name is unknown or ambiguous (duplicate names) */
3905 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3906 goto out;
3907 }
3908 }
3909 else if (!action && (strcmp(key, "action") == 0)) {
3910 if (strcmp(value, "ready") == 0) {
3911 action = ST_ADM_ACTION_READY;
3912 }
3913 else if (strcmp(value, "drain") == 0) {
3914 action = ST_ADM_ACTION_DRAIN;
3915 }
3916 else if (strcmp(value, "maint") == 0) {
3917 action = ST_ADM_ACTION_MAINT;
3918 }
3919 else if (strcmp(value, "shutdown") == 0) {
3920 action = ST_ADM_ACTION_SHUTDOWN;
3921 }
3922 else if (strcmp(value, "dhlth") == 0) {
3923 action = ST_ADM_ACTION_DHLTH;
3924 }
3925 else if (strcmp(value, "ehlth") == 0) {
3926 action = ST_ADM_ACTION_EHLTH;
3927 }
3928 else if (strcmp(value, "hrunn") == 0) {
3929 action = ST_ADM_ACTION_HRUNN;
3930 }
3931 else if (strcmp(value, "hnolb") == 0) {
3932 action = ST_ADM_ACTION_HNOLB;
3933 }
3934 else if (strcmp(value, "hdown") == 0) {
3935 action = ST_ADM_ACTION_HDOWN;
3936 }
3937 else if (strcmp(value, "dagent") == 0) {
3938 action = ST_ADM_ACTION_DAGENT;
3939 }
3940 else if (strcmp(value, "eagent") == 0) {
3941 action = ST_ADM_ACTION_EAGENT;
3942 }
3943 else if (strcmp(value, "arunn") == 0) {
3944 action = ST_ADM_ACTION_ARUNN;
3945 }
3946 else if (strcmp(value, "adown") == 0) {
3947 action = ST_ADM_ACTION_ADOWN;
3948 }
3949 /* else these are the old supported methods */
3950 else if (strcmp(value, "disable") == 0) {
3951 action = ST_ADM_ACTION_DISABLE;
3952 }
3953 else if (strcmp(value, "enable") == 0) {
3954 action = ST_ADM_ACTION_ENABLE;
3955 }
3956 else if (strcmp(value, "stop") == 0) {
3957 action = ST_ADM_ACTION_STOP;
3958 }
3959 else if (strcmp(value, "start") == 0) {
3960 action = ST_ADM_ACTION_START;
3961 }
3962 else {
3963 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3964 goto out;
3965 }
3966 }
3967 else if (strcmp(key, "s") == 0) {
3968 if (!(px && action)) {
3969 /*
3970 * Indicates that we'll need to reprocess the parameters
3971 * as soon as backend and action are known
3972 */
3973 if (!reprocess) {
3974 st_cur_param = cur_param;
3975 st_next_param = next_param;
3976 }
3977 reprocess = 1;
3978 }
3979 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003980 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003981 switch (action) {
3982 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003983 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003984 altered_servers++;
3985 total_servers++;
3986 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3987 }
3988 break;
3989 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003990 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003991 altered_servers++;
3992 total_servers++;
3993 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3994 }
3995 break;
3996 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003997 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003998 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3999 altered_servers++;
4000 total_servers++;
4001 }
4002 break;
4003 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004004 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004005 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4006 altered_servers++;
4007 total_servers++;
4008 }
4009 break;
4010 case ST_ADM_ACTION_DHLTH:
4011 if (sv->check.state & CHK_ST_CONFIGURED) {
4012 sv->check.state &= ~CHK_ST_ENABLED;
4013 altered_servers++;
4014 total_servers++;
4015 }
4016 break;
4017 case ST_ADM_ACTION_EHLTH:
4018 if (sv->check.state & CHK_ST_CONFIGURED) {
4019 sv->check.state |= CHK_ST_ENABLED;
4020 altered_servers++;
4021 total_servers++;
4022 }
4023 break;
4024 case ST_ADM_ACTION_HRUNN:
4025 if (!(sv->track)) {
4026 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004027 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004028 altered_servers++;
4029 total_servers++;
4030 }
4031 break;
4032 case ST_ADM_ACTION_HNOLB:
4033 if (!(sv->track)) {
4034 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004035 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004036 altered_servers++;
4037 total_servers++;
4038 }
4039 break;
4040 case ST_ADM_ACTION_HDOWN:
4041 if (!(sv->track)) {
4042 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004043 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004044 altered_servers++;
4045 total_servers++;
4046 }
4047 break;
4048 case ST_ADM_ACTION_DAGENT:
4049 if (sv->agent.state & CHK_ST_CONFIGURED) {
4050 sv->agent.state &= ~CHK_ST_ENABLED;
4051 altered_servers++;
4052 total_servers++;
4053 }
4054 break;
4055 case ST_ADM_ACTION_EAGENT:
4056 if (sv->agent.state & CHK_ST_CONFIGURED) {
4057 sv->agent.state |= CHK_ST_ENABLED;
4058 altered_servers++;
4059 total_servers++;
4060 }
4061 break;
4062 case ST_ADM_ACTION_ARUNN:
4063 if (sv->agent.state & CHK_ST_ENABLED) {
4064 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004065 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004066 altered_servers++;
4067 total_servers++;
4068 }
4069 break;
4070 case ST_ADM_ACTION_ADOWN:
4071 if (sv->agent.state & CHK_ST_ENABLED) {
4072 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004073 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004074 altered_servers++;
4075 total_servers++;
4076 }
4077 break;
4078 case ST_ADM_ACTION_READY:
4079 srv_adm_set_ready(sv);
4080 altered_servers++;
4081 total_servers++;
4082 break;
4083 case ST_ADM_ACTION_DRAIN:
4084 srv_adm_set_drain(sv);
4085 altered_servers++;
4086 total_servers++;
4087 break;
4088 case ST_ADM_ACTION_MAINT:
4089 srv_adm_set_maint(sv);
4090 altered_servers++;
4091 total_servers++;
4092 break;
4093 case ST_ADM_ACTION_SHUTDOWN:
Willy Tarreauc3914d42020-09-24 08:39:22 +02004094 if (!px->disabled) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004095 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004096 altered_servers++;
4097 total_servers++;
4098 }
4099 break;
4100 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004101 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004102 } else {
4103 /* the server name is unknown or ambiguous (duplicate names) */
4104 total_servers++;
4105 }
4106 }
4107 if (reprocess && px && action) {
4108 /* Now, we know the backend and the action chosen by the user.
4109 * We can safely restart from the first server parameter
4110 * to reprocess them
4111 */
4112 cur_param = st_cur_param;
4113 next_param = st_next_param;
4114 reprocess = 0;
4115 goto reprocess_servers;
4116 }
4117
4118 next_param = cur_param;
4119 }
4120 }
4121
4122 if (total_servers == 0) {
4123 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4124 }
4125 else if (altered_servers == 0) {
4126 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
4127 }
4128 else if (altered_servers == total_servers) {
4129 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
4130 }
4131 else {
4132 appctx->ctx.stats.st_code = STAT_STATUS_PART;
4133 }
4134 out:
4135 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004136 wait:
4137 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4138 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004139}
4140
4141
Christopher Fauletb7f88902019-07-15 21:56:43 +02004142static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004143{
4144 struct stream *s = si_strm(si);
4145 struct uri_auth *uri = s->be->uri_auth;
4146 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004147 struct htx_sl *sl;
4148 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004149
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004150 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);
4151 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4152 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004153 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004154 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004155
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004156 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004157 goto full;
4158 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
4159 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4160 goto full;
4161 }
Christopher Faulet6338a082019-09-09 15:50:54 +02004162 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
4163 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4164 goto full;
4165 }
Christopher Fauletef779222018-10-31 08:47:01 +01004166 else {
4167 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4168 goto full;
4169 }
4170
4171 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
4172 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004173 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004174 goto full;
4175 }
4176
4177 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4178 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4179 goto full;
4180 }
4181
4182 if (!htx_add_endof(htx, HTX_BLK_EOH))
4183 goto full;
4184
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004185 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004186 return 1;
4187
4188 full:
4189 htx_reset(htx);
4190 si_rx_room_blk(si);
4191 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004192}
4193
Christopher Fauletef779222018-10-31 08:47:01 +01004194
Christopher Fauletb7f88902019-07-15 21:56:43 +02004195static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004196{
4197 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4198 struct stream *s = si_strm(si);
4199 struct uri_auth *uri = s->be->uri_auth;
4200 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004201 struct htx_sl *sl;
4202 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004203
4204 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4205 scope_txt[0] = 0;
4206 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01004207 const char *scope_ptr = stats_scope_ptr(appctx, si);
4208
Christopher Fauletef779222018-10-31 08:47:01 +01004209 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004210 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01004211 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
4212 }
4213
4214 /* We don't want to land on the posted stats page because a refresh will
4215 * repost the data. We don't want this to happen on accident so we redirect
4216 * the browse to the stats page with a GET.
4217 */
4218 chunk_printf(&trash, "%s;st=%s%s%s%s",
4219 uri->uri_prefix,
4220 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4221 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4222 stat_status_codes[appctx->ctx.stats.st_code]) ?
4223 stat_status_codes[appctx->ctx.stats.st_code] :
4224 stat_status_codes[STAT_STATUS_UNKN],
4225 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4226 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4227 scope_txt);
4228
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004229 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4230 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4231 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004232 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004233 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004234
4235 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004236 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4237 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4238 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4239 goto full;
4240
4241 if (!htx_add_endof(htx, HTX_BLK_EOH))
4242 goto full;
4243
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004244 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004245 return 1;
4246
4247full:
4248 htx_reset(htx);
4249 si_rx_room_blk(si);
4250 return 0;
4251}
William Lallemand74c24fb2016-11-21 17:18:36 +01004252
Simon Horman05ee2132017-01-04 09:37:25 +01004253
William Lallemand74c24fb2016-11-21 17:18:36 +01004254/* This I/O handler runs as an applet embedded in a stream interface. It is
4255 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4256 * appctx->st0 contains the operation in progress (dump, done). The handler
4257 * automatically unregisters itself once transfer is complete.
4258 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004259static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004260{
4261 struct stream_interface *si = appctx->owner;
4262 struct stream *s = si_strm(si);
4263 struct channel *req = si_oc(si);
4264 struct channel *res = si_ic(si);
4265 struct htx *req_htx, *res_htx;
4266
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004267 /* only proxy stats are available via http */
4268 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
4269
Christopher Fauletef779222018-10-31 08:47:01 +01004270 res_htx = htx_from_buf(&res->buf);
4271
4272 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4273 goto out;
4274
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004275 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004276 if (!b_size(&res->buf)) {
4277 si_rx_room_blk(si);
4278 goto out;
4279 }
4280
4281 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004282 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4283 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004284
4285 /* all states are processed in sequence */
4286 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02004287 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004288 if (s->txn->meth == HTTP_METH_HEAD)
4289 appctx->st0 = STAT_HTTP_DONE;
4290 else
4291 appctx->st0 = STAT_HTTP_DUMP;
4292 }
4293 }
4294
4295 if (appctx->st0 == STAT_HTTP_DUMP) {
4296 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
4297 appctx->st0 = STAT_HTTP_DONE;
4298 }
4299
4300 if (appctx->st0 == STAT_HTTP_POST) {
4301 if (stats_process_http_post(si))
4302 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004303 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004304 appctx->st0 = STAT_HTTP_DONE;
4305 }
4306
4307 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02004308 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004309 appctx->st0 = STAT_HTTP_DONE;
4310 }
4311
4312 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004313 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
4314 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004315 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004316 }
4317
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004318 if (appctx->st0 == STAT_HTTP_END) {
4319 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004320 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004321 si_shutr(si);
4322 }
4323
4324 /* eat the whole request */
4325 if (co_data(req)) {
4326 req_htx = htx_from_buf(&req->buf);
4327 co_htx_skip(req, req_htx, co_data(req));
4328 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004329 }
4330 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004331
Christopher Fauletef779222018-10-31 08:47:01 +01004332 out:
4333 /* we have left the request in the buffer for the case where we
4334 * process a POST, and this automatically re-enables activity on
4335 * read. It's better to indicate that we want to stop reading when
4336 * we're sending, so that we know there's at most one direction
4337 * deciding to wake the applet up. It saves it from looping when
4338 * emitting large blocks into small TCP windows.
4339 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004340 htx_to_buf(res_htx, &res->buf);
4341 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01004342 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01004343}
4344
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004345/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004346static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004347 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004348{
4349 int field;
4350
4351 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4352 if (!field_format(info, field))
4353 continue;
4354
Willy Tarreaueaa55372019-10-09 07:39:11 +02004355 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004356 return 0;
4357 if (!stats_emit_raw_data_field(out, &info[field]))
4358 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004359 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4360 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004361 if (!chunk_strcat(out, "\n"))
4362 return 0;
4363 }
4364 return 1;
4365}
4366
4367/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004368static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004369 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004370{
4371 int field;
4372
4373 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4374 if (!field_format(info, field))
4375 continue;
4376
Willy Tarreaueaa55372019-10-09 07:39:11 +02004377 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 +01004378 return 0;
4379 if (!stats_emit_field_tags(out, &info[field], ':'))
4380 return 0;
4381 if (!stats_emit_typed_data_field(out, &info[field]))
4382 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004383 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4384 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004385 if (!chunk_strcat(out, "\n"))
4386 return 0;
4387 }
4388 return 1;
4389}
4390
Willy Tarreau0b26b382021-05-08 07:43:53 +02004391/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4392 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4393 * less then this value, the function returns 0, otherwise, it returns 1. Some
4394 * fields' presence or precision may depend on some of the STAT_* flags present
4395 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004396 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004397int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004398{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004399 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004400 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004401
4402#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004403 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4404 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4405 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004406
Willy Tarreauc5977722021-05-08 08:14:04 +02004407 if (ssl_key_rate < ssl_sess_rate)
4408 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004409#endif
4410
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004411 tv_remain(&start_date, &now, &up);
4412
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004413 if (len < INF_TOTAL_FIELDS)
4414 return 0;
4415
4416 chunk_reset(out);
4417 memset(info, 0, sizeof(*info) * len);
4418
4419 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004420 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004421 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004422 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004423
Yves Lafon95317282018-02-26 11:10:37 +01004424 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004425 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004426 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004427 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4428
4429 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004430 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 +01004431
Willy Tarreaue8abc322021-05-08 07:56:56 +02004432 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);
4433 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 +01004434 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004435 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004436 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004437 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004438 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004439 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004440 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4441 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4442 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4443 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4444 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4445 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4446 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4447 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4448#ifdef USE_OPENSSL
4449 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004450 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4451 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004452#endif
4453 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4454 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4455 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004456 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 +01004457 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4458 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004459 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 +01004460 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4461 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4462
4463#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004464 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 +01004465 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4466 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004467 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 +01004468 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004469 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4470 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 +01004471 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4472 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4473 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4474#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004475 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));
4476 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 +01004477 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4478#ifdef USE_ZLIB
4479 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4480 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4481#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004482 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004483 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreau61369892021-09-30 08:52:11 +02004484 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, sched_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004485 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4486 if (global.desc)
4487 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004488 info[INF_STOPPING] = mkf_u32(0, stopping);
4489 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004490 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004491 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004492 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004493 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004494 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004495 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004496 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004497 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004498 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004499 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 +02004500 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004501 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004502
4503 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4504 chunk_appendf(out, "%#x", get_tainted());
4505
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004506 return 1;
4507}
4508
4509/* This function dumps information onto the stream interface's read buffer.
4510 * It returns 0 as long as it does not complete, non-zero upon completion.
4511 * No state is used.
4512 */
4513static int stats_dump_info_to_buffer(struct stream_interface *si)
4514{
4515 struct appctx *appctx = __objt_appctx(si->end);
4516
Willy Tarreau0b26b382021-05-08 07:43:53 +02004517 if (!stats_fill_info(info, INF_TOTAL_FIELDS, appctx->ctx.stats.flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004518 return 0;
4519
4520 chunk_reset(&trash);
4521
4522 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004523 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01004524 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004525 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004526 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02004527 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004528
Willy Tarreau06d80a92017-10-19 14:32:15 +02004529 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004530 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004531 return 0;
4532 }
4533
4534 return 1;
4535}
4536
Simon Horman6f6bb382017-01-04 09:37:26 +01004537/* This function dumps the schema onto the stream interface's read buffer.
4538 * It returns 0 as long as it does not complete, non-zero upon completion.
4539 * No state is used.
4540 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004541 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004542 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4543 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004544static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004545{
4546
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004547 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004548
4549 chunk_strcat(out,
4550 "{"
4551 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4552 "\"oneOf\":["
4553 "{"
4554 "\"title\":\"Info\","
4555 "\"type\":\"array\","
4556 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004557 "\"title\":\"InfoItem\","
4558 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004559 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004560 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4561 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4562 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4563 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4564 "},"
4565 "\"required\":[\"field\",\"processNum\",\"tags\","
4566 "\"value\"]"
4567 "}"
4568 "},"
4569 "{"
4570 "\"title\":\"Stat\","
4571 "\"type\":\"array\","
4572 "\"items\":{"
4573 "\"title\":\"InfoItem\","
4574 "\"type\":\"object\","
4575 "\"properties\":{"
4576 "\"objType\":{"
4577 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4578 "\"Server\",\"Unknown\"]"
4579 "},"
4580 "\"proxyId\":{"
4581 "\"type\":\"integer\","
4582 "\"minimum\":0"
4583 "},"
4584 "\"id\":{"
4585 "\"type\":\"integer\","
4586 "\"minimum\":0"
4587 "},"
4588 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4589 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4590 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4591 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4592 "},"
4593 "\"required\":[\"objType\",\"proxyId\",\"id\","
4594 "\"field\",\"processNum\",\"tags\","
4595 "\"value\"]"
4596 "}"
4597 "},"
4598 "{"
4599 "\"title\":\"Error\","
4600 "\"type\":\"object\","
4601 "\"properties\":{"
4602 "\"errorStr\":{"
4603 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004604 "}"
4605 "},"
4606 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004607 "}"
4608 "],"
4609 "\"definitions\":{"
4610 "\"field\":{"
4611 "\"type\":\"object\","
4612 "\"pos\":{"
4613 "\"type\":\"integer\","
4614 "\"minimum\":0"
4615 "},"
4616 "\"name\":{"
4617 "\"type\":\"string\""
4618 "},"
4619 "\"required\":[\"pos\",\"name\"]"
4620 "},"
4621 "\"processNum\":{"
4622 "\"type\":\"integer\","
4623 "\"minimum\":1"
4624 "},"
4625 "\"tags\":{"
4626 "\"type\":\"object\","
4627 "\"origin\":{"
4628 "\"type\":\"string\","
4629 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4630 "\"Config\",\"Product\",\"Unknown\"]"
4631 "},"
4632 "\"nature\":{"
4633 "\"type\":\"string\","
4634 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4635 "\"Rate\",\"Counter\",\"Duration\","
4636 "\"Age\",\"Time\",\"Name\",\"Output\","
4637 "\"Avg\", \"Unknown\"]"
4638 "},"
4639 "\"scope\":{"
4640 "\"type\":\"string\","
4641 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4642 "\"System\",\"Unknown\"]"
4643 "},"
4644 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4645 "},"
4646 "\"typedValue\":{"
4647 "\"type\":\"object\","
4648 "\"oneOf\":["
4649 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4650 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4651 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4652 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4653 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4654 "],"
4655 "\"definitions\":{"
4656 "\"s32Value\":{"
4657 "\"properties\":{"
4658 "\"type\":{"
4659 "\"type\":\"string\","
4660 "\"enum\":[\"s32\"]"
4661 "},"
4662 "\"value\":{"
4663 "\"type\":\"integer\","
4664 "\"minimum\":-2147483648,"
4665 "\"maximum\":2147483647"
4666 "}"
4667 "},"
4668 "\"required\":[\"type\",\"value\"]"
4669 "},"
4670 "\"s64Value\":{"
4671 "\"properties\":{"
4672 "\"type\":{"
4673 "\"type\":\"string\","
4674 "\"enum\":[\"s64\"]"
4675 "},"
4676 "\"value\":{"
4677 "\"type\":\"integer\","
4678 "\"minimum\":-9007199254740991,"
4679 "\"maximum\":9007199254740991"
4680 "}"
4681 "},"
4682 "\"required\":[\"type\",\"value\"]"
4683 "},"
4684 "\"u32Value\":{"
4685 "\"properties\":{"
4686 "\"type\":{"
4687 "\"type\":\"string\","
4688 "\"enum\":[\"u32\"]"
4689 "},"
4690 "\"value\":{"
4691 "\"type\":\"integer\","
4692 "\"minimum\":0,"
4693 "\"maximum\":4294967295"
4694 "}"
4695 "},"
4696 "\"required\":[\"type\",\"value\"]"
4697 "},"
4698 "\"u64Value\":{"
4699 "\"properties\":{"
4700 "\"type\":{"
4701 "\"type\":\"string\","
4702 "\"enum\":[\"u64\"]"
4703 "},"
4704 "\"value\":{"
4705 "\"type\":\"integer\","
4706 "\"minimum\":0,"
4707 "\"maximum\":9007199254740991"
4708 "}"
4709 "},"
4710 "\"required\":[\"type\",\"value\"]"
4711 "},"
4712 "\"strValue\":{"
4713 "\"properties\":{"
4714 "\"type\":{"
4715 "\"type\":\"string\","
4716 "\"enum\":[\"str\"]"
4717 "},"
4718 "\"value\":{\"type\":\"string\"}"
4719 "},"
4720 "\"required\":[\"type\",\"value\"]"
4721 "},"
4722 "\"unknownValue\":{"
4723 "\"properties\":{"
4724 "\"type\":{"
4725 "\"type\":\"integer\","
4726 "\"minimum\":0"
4727 "},"
4728 "\"value\":{"
4729 "\"type\":\"string\","
4730 "\"enum\":[\"unknown\"]"
4731 "}"
4732 "},"
4733 "\"required\":[\"type\",\"value\"]"
4734 "}"
4735 "}"
4736 "}"
4737 "}"
4738 "}");
4739
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004740 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004741 chunk_reset(out);
4742 chunk_appendf(out,
4743 "{\"errorStr\":\"output buffer too short\"}");
4744 }
4745}
4746
4747/* This function dumps the schema onto the stream interface's read buffer.
4748 * It returns 0 as long as it does not complete, non-zero upon completion.
4749 * No state is used.
4750 */
4751static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
4752{
4753 chunk_reset(&trash);
4754
4755 stats_dump_json_schema(&trash);
4756
Willy Tarreau06d80a92017-10-19 14:32:15 +02004757 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004758 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01004759 return 0;
4760 }
4761
4762 return 1;
4763}
4764
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004765static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004766{
4767 struct proxy *px;
4768 struct server *sv;
4769 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004770 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004771 int clrall = 0;
4772
4773 if (strcmp(args[2], "all") == 0)
4774 clrall = 1;
4775
4776 /* check permissions */
4777 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4778 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4779 return 1;
4780
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004781 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004782 if (clrall) {
4783 memset(&px->be_counters, 0, sizeof(px->be_counters));
4784 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4785 }
4786 else {
4787 px->be_counters.conn_max = 0;
4788 px->be_counters.p.http.rps_max = 0;
4789 px->be_counters.sps_max = 0;
4790 px->be_counters.cps_max = 0;
4791 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004792 px->be_counters.qtime_max = 0;
4793 px->be_counters.ctime_max = 0;
4794 px->be_counters.dtime_max = 0;
4795 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004796
4797 px->fe_counters.conn_max = 0;
4798 px->fe_counters.p.http.rps_max = 0;
4799 px->fe_counters.sps_max = 0;
4800 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004801 }
4802
4803 for (sv = px->srv; sv; sv = sv->next)
4804 if (clrall)
4805 memset(&sv->counters, 0, sizeof(sv->counters));
4806 else {
4807 sv->counters.cur_sess_max = 0;
4808 sv->counters.nbpend_max = 0;
4809 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004810 sv->counters.qtime_max = 0;
4811 sv->counters.ctime_max = 0;
4812 sv->counters.dtime_max = 0;
4813 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004814 }
4815
4816 list_for_each_entry(li, &px->conf.listeners, by_fe)
4817 if (li->counters) {
4818 if (clrall)
4819 memset(li->counters, 0, sizeof(*li->counters));
4820 else
4821 li->counters->conn_max = 0;
4822 }
4823 }
4824
4825 global.cps_max = 0;
4826 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004827 global.ssl_max = 0;
4828 global.ssl_fe_keys_max = 0;
4829 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004830
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004831 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4832 if (!mod->clearable && !clrall)
4833 continue;
4834
4835 for (px = proxies_list; px; px = px->next) {
4836 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4837
4838 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4839 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4840 mod,
4841 mod->counters,
4842 mod->counters_size);
4843 }
4844
4845 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4846 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4847 mod,
4848 mod->counters,
4849 mod->counters_size);
4850 }
4851
4852 if (mod_cap & STATS_PX_CAP_SRV) {
4853 for (sv = px->srv; sv; sv = sv->next) {
4854 EXTRA_COUNTERS_INIT(sv->extra_counters,
4855 mod,
4856 mod->counters,
4857 mod->counters_size);
4858 }
4859 }
4860
4861 if (mod_cap & STATS_PX_CAP_LI) {
4862 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4863 EXTRA_COUNTERS_INIT(li->extra_counters,
4864 mod,
4865 mod->counters,
4866 mod->counters_size);
4867 }
4868 }
4869 }
4870 }
4871
Emeric Brund3b44952021-01-06 16:01:02 +01004872 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_DNS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004873
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004874 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004875 return 1;
4876}
4877
4878
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004879static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004880{
Willy Tarreau2f397382019-10-09 11:43:59 +02004881 int arg = 2;
4882
Willy Tarreaud25fc792016-12-16 12:33:47 +01004883 appctx->ctx.stats.scope_str = 0;
4884 appctx->ctx.stats.scope_len = 0;
4885 appctx->ctx.stats.flags = 0;
4886
Willy Tarreau2f397382019-10-09 11:43:59 +02004887 while (*args[arg]) {
4888 if (strcmp(args[arg], "typed") == 0)
4889 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4890 else if (strcmp(args[arg], "json") == 0)
4891 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4892 else if (strcmp(args[arg], "desc") == 0)
4893 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02004894 else if (strcmp(args[arg], "float") == 0)
4895 appctx->ctx.stats.flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02004896 arg++;
4897 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004898 return 0;
4899}
4900
4901
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004902static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004903{
Willy Tarreau2f397382019-10-09 11:43:59 +02004904 int arg = 2;
4905
Willy Tarreaud25fc792016-12-16 12:33:47 +01004906 appctx->ctx.stats.scope_str = 0;
4907 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004908 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4909
4910 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
4911 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004912
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004913 /* proxy is the default domain */
4914 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004915 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004916 ++args;
4917
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004918 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004919 ++args;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004920 } else if (strcmp(args[arg], "dns") == 0) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004921 appctx->ctx.stats.domain = STATS_DOMAIN_DNS;
4922 ++args;
4923 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004924 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004925 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004926 }
4927
4928 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4929 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004930 struct proxy *px;
4931
Willy Tarreau2f397382019-10-09 11:43:59 +02004932 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004933 if (px)
4934 appctx->ctx.stats.iid = px->uuid;
4935 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004936 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004937
Willy Tarreau9d008692019-08-09 11:21:01 +02004938 if (!appctx->ctx.stats.iid)
4939 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004940
Willy Tarreau2b812e22016-11-22 16:18:05 +01004941 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004942 appctx->ctx.stats.type = atoi(args[arg+1]);
4943 appctx->ctx.stats.sid = atoi(args[arg+2]);
4944 arg += 3;
4945 }
4946
4947 while (*args[arg]) {
4948 if (strcmp(args[arg], "typed") == 0)
4949 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4950 else if (strcmp(args[arg], "json") == 0)
4951 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4952 else if (strcmp(args[arg], "desc") == 0)
4953 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02004954 else if (strcmp(args[arg], "no-maint") == 0)
4955 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02004956 else if (strcmp(args[arg], "up") == 0)
4957 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02004958 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004959 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004960
Willy Tarreau2b812e22016-11-22 16:18:05 +01004961 return 0;
4962}
4963
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004964static int cli_io_handler_dump_info(struct appctx *appctx)
4965{
4966 return stats_dump_info_to_buffer(appctx->owner);
4967}
4968
Willy Tarreau2b812e22016-11-22 16:18:05 +01004969/* This I/O handler runs as an applet embedded in a stream interface. It is
4970 * used to send raw stats over a socket.
4971 */
4972static int cli_io_handler_dump_stat(struct appctx *appctx)
4973{
Christopher Fauletef779222018-10-31 08:47:01 +01004974 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004975}
4976
Simon Horman6f6bb382017-01-04 09:37:26 +01004977static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4978{
4979 return stats_dump_json_schema_to_buffer(appctx->owner);
4980}
4981
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01004982int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
4983 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004984{
4985 struct stats_module *mod;
4986
4987 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
4988
4989 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4990 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4991 continue;
4992
4993 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
4994 }
4995
4996 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
4997
4998 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4999 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5000 continue;
5001
5002 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5003 }
5004
5005 return 1;
5006
5007 alloc_failed:
5008 return 0;
5009}
5010
5011/* Initialize and allocate all extra counters for a proxy and its attached
5012 * servers/listeners with all already registered stats module
5013 */
5014int stats_allocate_proxy_counters(struct proxy *px)
5015{
5016 struct server *sv;
5017 struct listener *li;
5018
5019 if (px->cap & PR_CAP_FE) {
5020 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5021 COUNTERS_FE,
5022 STATS_PX_CAP_FE)) {
5023 return 0;
5024 }
5025 }
5026
5027 if (px->cap & PR_CAP_BE) {
5028 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5029 COUNTERS_BE,
5030 STATS_PX_CAP_BE)) {
5031 return 0;
5032 }
5033 }
5034
5035 for (sv = px->srv; sv; sv = sv->next) {
5036 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5037 COUNTERS_SV,
5038 STATS_PX_CAP_SRV)) {
5039 return 0;
5040 }
5041 }
5042
5043 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5044 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5045 COUNTERS_LI,
5046 STATS_PX_CAP_LI)) {
5047 return 0;
5048 }
5049 }
5050
5051 return 1;
5052}
5053
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005054void stats_register_module(struct stats_module *m)
5055{
5056 const uint8_t domain = stats_get_domain(m->domain_flags);
5057
Willy Tarreau2b718102021-04-21 07:32:39 +02005058 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005059 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005060}
5061
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005062static int allocate_stats_px_postcheck(void)
5063{
5064 struct stats_module *mod;
5065 size_t i = ST_F_TOTAL_FIELDS;
5066 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005067 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005068
5069 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5070
5071 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5072 if (!stat_f[STATS_DOMAIN_PROXY]) {
5073 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5074 err_code |= ERR_ALERT | ERR_FATAL;
5075 return err_code;
5076 }
5077
5078 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5079 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5080
5081 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5082 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5083 mod->stats,
5084 mod->stats_count * sizeof(struct name_desc));
5085 i += mod->stats_count;
5086 }
5087
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005088 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005089 if (!stats_allocate_proxy_counters(px)) {
5090 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5091 err_code |= ERR_ALERT | ERR_FATAL;
5092 return err_code;
5093 }
5094 }
5095
Christopher Fauletde79cd22021-01-06 07:41:56 +01005096 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005097
5098 return err_code;
5099}
5100
5101REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5102
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005103static int allocate_stats_dns_postcheck(void)
5104{
5105 struct stats_module *mod;
5106 size_t i = 0;
5107 int err_code = 0;
5108
5109 stat_f[STATS_DOMAIN_DNS] = malloc(stat_count[STATS_DOMAIN_DNS] * sizeof(struct name_desc));
5110 if (!stat_f[STATS_DOMAIN_DNS]) {
5111 ha_alert("stats: cannot allocate all fields for dns statistics\n");
5112 err_code |= ERR_ALERT | ERR_FATAL;
5113 return err_code;
5114 }
5115
5116 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_DNS], list) {
5117 memcpy(stat_f[STATS_DOMAIN_DNS] + i,
5118 mod->stats,
5119 mod->stats_count * sizeof(struct name_desc));
5120 i += mod->stats_count;
5121 }
5122
Emeric Brund3b44952021-01-06 16:01:02 +01005123 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_DNS])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005124 ha_alert("stats: cannot allocate all counters for dns statistics\n");
5125 err_code |= ERR_ALERT | ERR_FATAL;
5126 return err_code;
5127 }
5128
Christopher Fauletde79cd22021-01-06 07:41:56 +01005129 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005130
5131 return err_code;
5132}
5133
5134REGISTER_CONFIG_POSTPARSER("allocate-stats-dns", allocate_stats_dns_postcheck);
5135
Christopher Fauletde79cd22021-01-06 07:41:56 +01005136static int allocate_stat_lines_per_thread(void)
5137{
5138 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5139
5140 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5141 const int domain = domains[i];
5142
5143 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5144 if (!stat_l[domain])
5145 return 0;
5146 }
5147 return 1;
5148}
5149
5150REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5151
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005152static int allocate_trash_counters(void)
5153{
5154 struct stats_module *mod;
5155 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5156 size_t max_counters_size = 0;
5157
5158 /* calculate the greatest counters used by any stats modules */
5159 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5160 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5161 max_counters_size = mod->counters_size > max_counters_size ?
5162 mod->counters_size : max_counters_size;
5163 }
5164 }
5165
5166 /* allocate the trash with the size of the greatest counters */
5167 if (max_counters_size) {
5168 trash_counters = malloc(max_counters_size);
5169 if (!trash_counters) {
5170 ha_alert("stats: cannot allocate trash counters for statistics\n");
5171 return 0;
5172 }
5173 }
5174
5175 return 1;
5176}
5177
5178REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5179
Christopher Fauletde79cd22021-01-06 07:41:56 +01005180static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005181{
5182 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5183
5184 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5185 const int domain = domains[i];
5186
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005187 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005188 }
5189}
5190
5191
5192REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5193
5194static void deinit_stats(void)
5195{
5196 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
5197
5198 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5199 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005200
5201 if (stat_f[domain])
5202 free(stat_f[domain]);
5203 }
5204}
5205
5206REGISTER_POST_DEINIT(deinit_stats);
5207
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005208static void free_trash_counters(void)
5209{
5210 if (trash_counters)
5211 free(trash_counters);
5212}
5213
5214REGISTER_PER_THREAD_FREE(free_trash_counters);
5215
Willy Tarreau2b812e22016-11-22 16:18:05 +01005216/* register cli keywords */
5217static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005218 { { "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 +02005219 { { "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 +02005220 { { "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 },
5221 { { "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 +01005222 {{},}
5223}};
5224
Willy Tarreau0108d902018-11-25 19:14:37 +01005225INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5226
William Lallemand74c24fb2016-11-21 17:18:36 +01005227struct applet http_stats_applet = {
5228 .obj_type = OBJ_TYPE_APPLET,
5229 .name = "<STATS>", /* used for logging */
5230 .fct = http_stats_io_handler,
5231 .release = NULL,
5232};
5233
William Lallemand74c24fb2016-11-21 17:18:36 +01005234/*
5235 * Local variables:
5236 * c-indent-level: 8
5237 * c-basic-offset: 8
5238 * End:
5239 */