blob: bad4be8ea9c837620804e8557602dc3424b809ad [file] [log] [blame]
Christopher Fauletf959d082019-02-07 15:38:42 +01001/*
2 * Promex is a Prometheus exporter for HAProxy
3 *
4 * It is highly inspired by the official Prometheus exporter.
5 * See: https://github.com/prometheus/haproxy_exporter
6 *
7 * Copyright 2019 Christopher Faulet <cfaulet@haproxy.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020018#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020019#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/compression.h>
Christopher Fauletc55a6262020-07-10 15:39:39 +020022#include <haproxy/dns.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020023#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020024#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020025#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020029#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020031#include <haproxy/pipe.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020034#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020036#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020039#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020040#include <haproxy/task.h>
William Dauchy5a982a72021-01-08 13:18:06 +010041#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010042
43/* Prometheus exporter applet states (appctx->st0) */
44enum {
45 PROMEX_ST_INIT = 0, /* initialized */
46 PROMEX_ST_HEAD, /* send headers before dump */
47 PROMEX_ST_DUMP, /* dumping stats */
48 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010049 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010050};
51
52/* Prometheus exporter dumper states (appctx->st1) */
53enum {
54 PROMEX_DUMPER_INIT = 0, /* initialized */
55 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
56 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
57 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
58 PROMEX_DUMPER_LI, /* dump metrics of listeners */
59 PROMEX_DUMPER_SRV, /* dump metrics of servers */
60 PROMEX_DUMPER_DONE, /* finished */
61};
62
63/* Prometheus exporter flags (appctx->ctx.stats.flags) */
64#define PROMEX_FL_METRIC_HDR 0x00000001
65#define PROMEX_FL_INFO_METRIC 0x00000002
Christopher Fauletb713c4f2021-01-20 15:02:50 +010066#define PROMEX_FL_FRONT_METRIC 0x00000004
67#define PROMEX_FL_BACK_METRIC 0x00000008
68#define PROMEX_FL_SRV_METRIC 0x00000010
69#define PROMEX_FL_SCOPE_GLOBAL 0x00000020
70#define PROMEX_FL_SCOPE_FRONT 0x00000040
71#define PROMEX_FL_SCOPE_BACK 0x00000080
72#define PROMEX_FL_SCOPE_SERVER 0x00000100
73#define PROMEX_FL_NO_MAINT_SRV 0x00000200
Christopher Faulet78407ce2019-11-18 14:47:08 +010074
75#define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL|PROMEX_FL_SCOPE_FRONT|PROMEX_FL_SCOPE_BACK|PROMEX_FL_SCOPE_SERVER)
Christopher Fauletf959d082019-02-07 15:38:42 +010076
77/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +050078 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +010079 */
80#define PROMEX_MAX_NAME_LEN 128
81
82/* The expected max length for a metric dump, including its header lines. It is
83 * just a soft limit to avoid extra work. We don't try to dump a metric if less
84 * than this size is available in the HTX.
85 */
86#define PROMEX_MAX_METRIC_LENGTH 512
87
William Dauchy5a982a72021-01-08 13:18:06 +010088/* Some labels for build_info */
89#define PROMEX_VERSION_LABEL "version=\"" HAPROXY_VERSION "\""
90#define PROMEX_BUILDINFO_LABEL PROMEX_VERSION_LABEL
91
Christopher Fauletf959d082019-02-07 15:38:42 +010092/* Matrix used to dump global metrics. Each metric points to the next one to be
93 * processed or 0 to stop the dump. */
94const int promex_global_metrics[INF_TOTAL_FIELDS] = {
William Dauchy5a982a72021-01-08 13:18:06 +010095 [INF_NAME] = INF_BUILD_INFO,
Christopher Fauletf959d082019-02-07 15:38:42 +010096 [INF_VERSION] = 0,
97 [INF_RELEASE_DATE] = 0,
William Dauchy5a982a72021-01-08 13:18:06 +010098 [INF_BUILD_INFO] = INF_NBTHREAD,
Christopher Fauletf959d082019-02-07 15:38:42 +010099 [INF_NBTHREAD] = INF_NBPROC,
100 [INF_NBPROC] = INF_PROCESS_NUM,
101 [INF_PROCESS_NUM] = INF_UPTIME_SEC,
102 [INF_PID] = 0,
103 [INF_UPTIME] = 0,
William Dauchydefd1562021-01-15 22:41:38 +0100104 [INF_UPTIME_SEC] = INF_START_TIME_SEC,
105 [INF_START_TIME_SEC] = INF_MEMMAX_BYTES,
William Dauchya8766cf2021-01-15 22:41:37 +0100106 [INF_MEMMAX_BYTES] = INF_POOL_ALLOC_BYTES,
107 [INF_POOL_ALLOC_BYTES] = INF_POOL_USED_BYTES,
108 [INF_POOL_USED_BYTES] = INF_POOL_FAILED,
Christopher Fauletf959d082019-02-07 15:38:42 +0100109 [INF_POOL_FAILED] = INF_ULIMIT_N,
110 [INF_ULIMIT_N] = INF_MAXSOCK,
111 [INF_MAXSOCK] = INF_MAXCONN,
112 [INF_MAXCONN] = INF_HARD_MAXCONN,
113 [INF_HARD_MAXCONN] = INF_CURR_CONN,
114 [INF_CURR_CONN] = INF_CUM_CONN,
115 [INF_CUM_CONN] = INF_CUM_REQ,
116 [INF_CUM_REQ] = INF_MAX_SSL_CONNS,
117 [INF_MAX_SSL_CONNS] = INF_CURR_SSL_CONNS,
118 [INF_CURR_SSL_CONNS] = INF_CUM_SSL_CONNS,
119 [INF_CUM_SSL_CONNS] = INF_MAXPIPES,
120 [INF_MAXPIPES] = INF_PIPES_USED,
121 [INF_PIPES_USED] = INF_PIPES_FREE,
122 [INF_PIPES_FREE] = INF_CONN_RATE,
123 [INF_CONN_RATE] = INF_CONN_RATE_LIMIT,
124 [INF_CONN_RATE_LIMIT] = INF_MAX_CONN_RATE,
125 [INF_MAX_CONN_RATE] = INF_SESS_RATE,
126 [INF_SESS_RATE] = INF_SESS_RATE_LIMIT,
127 [INF_SESS_RATE_LIMIT] = INF_MAX_SESS_RATE,
128 [INF_MAX_SESS_RATE] = INF_SSL_RATE,
129 [INF_SSL_RATE] = INF_SSL_RATE_LIMIT,
130 [INF_SSL_RATE_LIMIT] = INF_MAX_SSL_RATE,
131 [INF_MAX_SSL_RATE] = INF_SSL_FRONTEND_KEY_RATE,
132 [INF_SSL_FRONTEND_KEY_RATE] = INF_SSL_FRONTEND_MAX_KEY_RATE,
133 [INF_SSL_FRONTEND_MAX_KEY_RATE] = INF_SSL_FRONTEND_SESSION_REUSE_PCT,
134 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = INF_SSL_BACKEND_KEY_RATE,
135 [INF_SSL_BACKEND_KEY_RATE] = INF_SSL_BACKEND_MAX_KEY_RATE,
136 [INF_SSL_BACKEND_MAX_KEY_RATE] = INF_SSL_CACHE_LOOKUPS,
137 [INF_SSL_CACHE_LOOKUPS] = INF_SSL_CACHE_MISSES,
138 [INF_SSL_CACHE_MISSES] = INF_COMPRESS_BPS_IN,
139 [INF_COMPRESS_BPS_IN] = INF_COMPRESS_BPS_OUT,
140 [INF_COMPRESS_BPS_OUT] = INF_COMPRESS_BPS_RATE_LIM,
141 [INF_COMPRESS_BPS_RATE_LIM] = INF_ZLIB_MEM_USAGE,
142 [INF_ZLIB_MEM_USAGE] = INF_MAX_ZLIB_MEM_USAGE,
143 [INF_MAX_ZLIB_MEM_USAGE] = INF_TASKS,
144 [INF_TASKS] = INF_RUN_QUEUE,
145 [INF_RUN_QUEUE] = INF_IDLE_PCT,
146 [INF_IDLE_PCT] = INF_STOPPING,
147 [INF_NODE] = 0,
148 [INF_DESCRIPTION] = 0,
149 [INF_STOPPING] = INF_JOBS,
150 [INF_JOBS] = INF_UNSTOPPABLE_JOBS,
151 [INF_UNSTOPPABLE_JOBS] = INF_LISTENERS,
152 [INF_LISTENERS] = INF_ACTIVE_PEERS,
153 [INF_ACTIVE_PEERS] = INF_CONNECTED_PEERS,
154 [INF_CONNECTED_PEERS] = INF_DROPPED_LOGS,
155 [INF_DROPPED_LOGS] = INF_BUSY_POLLING,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200156 [INF_BUSY_POLLING] = INF_FAILED_RESOLUTIONS,
157 [INF_FAILED_RESOLUTIONS] = INF_TOTAL_BYTES_OUT,
158 [INF_TOTAL_BYTES_OUT] = INF_TOTAL_SPLICED_BYTES_OUT,
159 [INF_TOTAL_SPLICED_BYTES_OUT] = INF_BYTES_OUT_RATE,
160 [INF_BYTES_OUT_RATE] = 0,
161 [INF_DEBUG_COMMANDS_ISSUED] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100162};
163
164/* Matrix used to dump frontend metrics. Each metric points to the next one to be
165 * processed or 0 to stop the dump. */
166const int promex_front_metrics[ST_F_TOTAL_FIELDS] = {
167 [ST_F_PXNAME] = ST_F_STATUS,
168 [ST_F_SVNAME] = 0,
169 [ST_F_QCUR] = 0,
170 [ST_F_QMAX] = 0,
171 [ST_F_SCUR] = ST_F_SMAX,
172 [ST_F_SMAX] = ST_F_SLIM,
173 [ST_F_SLIM] = ST_F_STOT,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200174 [ST_F_STOT] = ST_F_RATE_LIM,
Christopher Fauletf959d082019-02-07 15:38:42 +0100175 [ST_F_BIN] = ST_F_BOUT,
176 [ST_F_BOUT] = ST_F_DREQ,
177 [ST_F_DREQ] = ST_F_DRESP,
178 [ST_F_DRESP] = ST_F_EREQ,
179 [ST_F_EREQ] = ST_F_DCON,
180 [ST_F_ECON] = 0,
181 [ST_F_ERESP] = 0,
182 [ST_F_WRETR] = 0,
183 [ST_F_WREDIS] = 0,
184 [ST_F_STATUS] = ST_F_SCUR,
185 [ST_F_WEIGHT] = 0,
186 [ST_F_ACT] = 0,
187 [ST_F_BCK] = 0,
188 [ST_F_CHKFAIL] = 0,
189 [ST_F_CHKDOWN] = 0,
190 [ST_F_LASTCHG] = 0,
191 [ST_F_DOWNTIME] = 0,
192 [ST_F_QLIMIT] = 0,
193 [ST_F_PID] = 0,
194 [ST_F_IID] = 0,
195 [ST_F_SID] = 0,
196 [ST_F_THROTTLE] = 0,
197 [ST_F_LBTOT] = 0,
198 [ST_F_TRACKED] = 0,
199 [ST_F_TYPE] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200200 [ST_F_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100201 [ST_F_RATE_LIM] = ST_F_RATE_MAX,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200202 [ST_F_RATE_MAX] = ST_F_CONN_RATE_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100203 [ST_F_CHECK_STATUS] = 0,
204 [ST_F_CHECK_CODE] = 0,
205 [ST_F_CHECK_DURATION] = 0,
206 [ST_F_HRSP_1XX] = ST_F_HRSP_2XX,
207 [ST_F_HRSP_2XX] = ST_F_HRSP_3XX,
208 [ST_F_HRSP_3XX] = ST_F_HRSP_4XX,
209 [ST_F_HRSP_4XX] = ST_F_HRSP_5XX,
210 [ST_F_HRSP_5XX] = ST_F_HRSP_OTHER,
211 [ST_F_HRSP_OTHER] = ST_F_INTERCEPTED,
212 [ST_F_HANAFAIL] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200213 [ST_F_REQ_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100214 [ST_F_REQ_RATE_MAX] = ST_F_REQ_TOT,
215 [ST_F_REQ_TOT] = ST_F_HRSP_1XX,
216 [ST_F_CLI_ABRT] = 0,
217 [ST_F_SRV_ABRT] = 0,
218 [ST_F_COMP_IN] = ST_F_COMP_OUT,
219 [ST_F_COMP_OUT] = ST_F_COMP_BYP,
220 [ST_F_COMP_BYP] = ST_F_COMP_RSP,
221 [ST_F_COMP_RSP] = 0,
222 [ST_F_LASTSESS] = 0,
223 [ST_F_LAST_CHK] = 0,
224 [ST_F_LAST_AGT] = 0,
225 [ST_F_QTIME] = 0,
226 [ST_F_CTIME] = 0,
227 [ST_F_RTIME] = 0,
228 [ST_F_TTIME] = 0,
229 [ST_F_AGENT_STATUS] = 0,
230 [ST_F_AGENT_CODE] = 0,
231 [ST_F_AGENT_DURATION] = 0,
232 [ST_F_CHECK_DESC] = 0,
233 [ST_F_AGENT_DESC] = 0,
234 [ST_F_CHECK_RISE] = 0,
235 [ST_F_CHECK_FALL] = 0,
236 [ST_F_CHECK_HEALTH] = 0,
237 [ST_F_AGENT_RISE] = 0,
238 [ST_F_AGENT_FALL] = 0,
239 [ST_F_AGENT_HEALTH] = 0,
240 [ST_F_ADDR] = 0,
241 [ST_F_COOKIE] = 0,
242 [ST_F_MODE] = 0,
243 [ST_F_ALGO] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200244 [ST_F_CONN_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100245 [ST_F_CONN_RATE_MAX] = ST_F_CONN_TOT,
246 [ST_F_CONN_TOT] = ST_F_BIN,
247 [ST_F_INTERCEPTED] = ST_F_CACHE_LOOKUPS,
248 [ST_F_DCON] = ST_F_DSES,
249 [ST_F_DSES] = ST_F_WREW,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100250 [ST_F_WREW] = ST_F_EINT,
Christopher Fauletf959d082019-02-07 15:38:42 +0100251 [ST_F_CONNECT] = 0,
252 [ST_F_REUSE] = 0,
253 [ST_F_CACHE_LOOKUPS] = ST_F_CACHE_HITS,
254 [ST_F_CACHE_HITS] = ST_F_COMP_IN,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100255 [ST_F_SRV_ICUR] = 0,
256 [ST_F_SRV_ILIM] = 0,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100257 [ST_F_QT_MAX] = 0,
258 [ST_F_CT_MAX] = 0,
259 [ST_F_RT_MAX] = 0,
260 [ST_F_TT_MAX] = 0,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100261 [ST_F_EINT] = ST_F_REQ_RATE_MAX,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200262 [ST_F_IDLE_CONN_CUR] = 0,
263 [ST_F_SAFE_CONN_CUR] = 0,
264 [ST_F_USED_CONN_CUR] = 0,
265 [ST_F_NEED_CONN_EST] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100266};
267
268/* Matrix used to dump backend metrics. Each metric points to the next one to be
269 * processed or 0 to stop the dump. */
270const int promex_back_metrics[ST_F_TOTAL_FIELDS] = {
271 [ST_F_PXNAME] = ST_F_STATUS,
272 [ST_F_SVNAME] = 0,
273 [ST_F_QCUR] = ST_F_QMAX,
274 [ST_F_QMAX] = ST_F_CONNECT,
275 [ST_F_SCUR] = ST_F_SMAX,
276 [ST_F_SMAX] = ST_F_SLIM,
277 [ST_F_SLIM] = ST_F_STOT,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200278 [ST_F_STOT] = ST_F_RATE_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100279 [ST_F_BIN] = ST_F_BOUT,
280 [ST_F_BOUT] = ST_F_QTIME,
281 [ST_F_DREQ] = ST_F_DRESP,
282 [ST_F_DRESP] = ST_F_ECON,
283 [ST_F_EREQ] = 0,
284 [ST_F_ECON] = ST_F_ERESP,
285 [ST_F_ERESP] = ST_F_WRETR,
286 [ST_F_WRETR] = ST_F_WREDIS,
287 [ST_F_WREDIS] = ST_F_WREW,
288 [ST_F_STATUS] = ST_F_SCUR,
289 [ST_F_WEIGHT] = ST_F_ACT,
290 [ST_F_ACT] = ST_F_BCK,
291 [ST_F_BCK] = ST_F_CHKDOWN,
292 [ST_F_CHKFAIL] = 0,
293 [ST_F_CHKDOWN] = ST_F_LASTCHG,
294 [ST_F_LASTCHG] = ST_F_DOWNTIME,
295 [ST_F_DOWNTIME] = ST_F_LBTOT,
296 [ST_F_QLIMIT] = 0,
297 [ST_F_PID] = 0,
298 [ST_F_IID] = 0,
299 [ST_F_SID] = 0,
300 [ST_F_THROTTLE] = 0,
301 [ST_F_LBTOT] = ST_F_REQ_TOT,
302 [ST_F_TRACKED] = 9,
303 [ST_F_TYPE] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200304 [ST_F_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100305 [ST_F_RATE_LIM] = 0,
306 [ST_F_RATE_MAX] = ST_F_LASTSESS,
307 [ST_F_CHECK_STATUS] = 0,
308 [ST_F_CHECK_CODE] = 0,
309 [ST_F_CHECK_DURATION] = 0,
310 [ST_F_HRSP_1XX] = ST_F_HRSP_2XX,
311 [ST_F_HRSP_2XX] = ST_F_HRSP_3XX,
312 [ST_F_HRSP_3XX] = ST_F_HRSP_4XX,
313 [ST_F_HRSP_4XX] = ST_F_HRSP_5XX,
314 [ST_F_HRSP_5XX] = ST_F_HRSP_OTHER,
315 [ST_F_HRSP_OTHER] = ST_F_CACHE_LOOKUPS,
316 [ST_F_HANAFAIL] = 0,
317 [ST_F_REQ_RATE] = 0,
318 [ST_F_REQ_RATE_MAX] = 0,
319 [ST_F_REQ_TOT] = ST_F_HRSP_1XX,
320 [ST_F_CLI_ABRT] = ST_F_SRV_ABRT,
321 [ST_F_SRV_ABRT] = ST_F_WEIGHT,
322 [ST_F_COMP_IN] = ST_F_COMP_OUT,
323 [ST_F_COMP_OUT] = ST_F_COMP_BYP,
324 [ST_F_COMP_BYP] = ST_F_COMP_RSP,
325 [ST_F_COMP_RSP] = 0,
326 [ST_F_LASTSESS] = ST_F_QCUR,
327 [ST_F_LAST_CHK] = 0,
328 [ST_F_LAST_AGT] = 0,
329 [ST_F_QTIME] = ST_F_CTIME,
330 [ST_F_CTIME] = ST_F_RTIME,
331 [ST_F_RTIME] = ST_F_TTIME,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100332 [ST_F_TTIME] = ST_F_QT_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100333 [ST_F_AGENT_STATUS] = 0,
334 [ST_F_AGENT_CODE] = 0,
335 [ST_F_AGENT_DURATION] = 0,
336 [ST_F_CHECK_DESC] = 0,
337 [ST_F_AGENT_DESC] = 0,
338 [ST_F_CHECK_RISE] = 0,
339 [ST_F_CHECK_FALL] = 0,
340 [ST_F_CHECK_HEALTH] = 0,
341 [ST_F_AGENT_RISE] = 0,
342 [ST_F_AGENT_FALL] = 0,
343 [ST_F_AGENT_HEALTH] = 0,
344 [ST_F_ADDR] = 0,
345 [ST_F_COOKIE] = 0,
346 [ST_F_MODE] = 0,
347 [ST_F_ALGO] = 0,
348 [ST_F_CONN_RATE] = 0,
349 [ST_F_CONN_RATE_MAX] = 0,
350 [ST_F_CONN_TOT] = 0,
351 [ST_F_INTERCEPTED] = 0,
352 [ST_F_DCON] = 0,
353 [ST_F_DSES] = 0,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100354 [ST_F_WREW] = ST_F_EINT,
Christopher Fauletf959d082019-02-07 15:38:42 +0100355 [ST_F_CONNECT] = ST_F_REUSE,
356 [ST_F_REUSE] = ST_F_BIN,
357 [ST_F_CACHE_LOOKUPS] = ST_F_CACHE_HITS,
358 [ST_F_CACHE_HITS] = ST_F_COMP_IN,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100359 [ST_F_SRV_ICUR] = 0,
360 [ST_F_SRV_ILIM] = 0,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100361 [ST_F_QT_MAX] = ST_F_CT_MAX,
362 [ST_F_CT_MAX] = ST_F_RT_MAX,
363 [ST_F_RT_MAX] = ST_F_TT_MAX,
364 [ST_F_TT_MAX] = ST_F_DREQ,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100365 [ST_F_EINT] = ST_F_CLI_ABRT,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200366 [ST_F_IDLE_CONN_CUR] = 0,
367 [ST_F_SAFE_CONN_CUR] = 0,
368 [ST_F_USED_CONN_CUR] = 0,
369 [ST_F_NEED_CONN_EST] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100370};
371
372/* Matrix used to dump server metrics. Each metric points to the next one to be
373 * processed or 0 to stop the dump. */
374const int promex_srv_metrics[ST_F_TOTAL_FIELDS] = {
375 [ST_F_PXNAME] = ST_F_STATUS,
376 [ST_F_SVNAME] = 0,
377 [ST_F_QCUR] = ST_F_QMAX,
378 [ST_F_QMAX] = ST_F_QLIMIT,
379 [ST_F_SCUR] = ST_F_SMAX,
380 [ST_F_SMAX] = ST_F_SLIM,
381 [ST_F_SLIM] = ST_F_STOT,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200382 [ST_F_STOT] = ST_F_RATE_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100383 [ST_F_BIN] = ST_F_BOUT,
384 [ST_F_BOUT] = ST_F_QTIME,
385 [ST_F_DREQ] = 0,
386 [ST_F_DRESP] = ST_F_ECON,
387 [ST_F_EREQ] = 0,
388 [ST_F_ECON] = ST_F_ERESP,
389 [ST_F_ERESP] = ST_F_WRETR,
390 [ST_F_WRETR] = ST_F_WREDIS,
391 [ST_F_WREDIS] = ST_F_WREW,
392 [ST_F_STATUS] = ST_F_SCUR,
Christopher Fauletcf403f32019-11-21 14:35:46 +0100393 [ST_F_WEIGHT] = ST_F_CHECK_STATUS,
Christopher Fauletf959d082019-02-07 15:38:42 +0100394 [ST_F_ACT] = 0,
395 [ST_F_BCK] = 0,
396 [ST_F_CHKFAIL] = ST_F_CHKDOWN,
397 [ST_F_CHKDOWN] = ST_F_DOWNTIME,
398 [ST_F_LASTCHG] = ST_F_THROTTLE,
399 [ST_F_DOWNTIME] = ST_F_LASTCHG,
400 [ST_F_QLIMIT] = ST_F_BIN,
401 [ST_F_PID] = 0,
402 [ST_F_IID] = 0,
403 [ST_F_SID] = 0,
404 [ST_F_THROTTLE] = ST_F_LBTOT,
405 [ST_F_LBTOT] = ST_F_HRSP_1XX,
406 [ST_F_TRACKED] = 0,
407 [ST_F_TYPE] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200408 [ST_F_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100409 [ST_F_RATE_LIM] = 0,
410 [ST_F_RATE_MAX] = ST_F_LASTSESS,
Christopher Fauletcf403f32019-11-21 14:35:46 +0100411 [ST_F_CHECK_STATUS] = ST_F_CHECK_CODE,
Christopher Faulet2711e512020-02-27 16:12:07 +0100412 [ST_F_CHECK_CODE] = ST_F_CHECK_DURATION,
413 [ST_F_CHECK_DURATION] = ST_F_CHKFAIL,
Christopher Fauletf959d082019-02-07 15:38:42 +0100414 [ST_F_HRSP_1XX] = ST_F_HRSP_2XX,
415 [ST_F_HRSP_2XX] = ST_F_HRSP_3XX,
416 [ST_F_HRSP_3XX] = ST_F_HRSP_4XX,
417 [ST_F_HRSP_4XX] = ST_F_HRSP_5XX,
418 [ST_F_HRSP_5XX] = ST_F_HRSP_OTHER,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100419 [ST_F_HRSP_OTHER] = ST_F_SRV_ICUR,
Christopher Fauletf959d082019-02-07 15:38:42 +0100420 [ST_F_HANAFAIL] = 0,
421 [ST_F_REQ_RATE] = 0,
422 [ST_F_REQ_RATE_MAX] = 0,
423 [ST_F_REQ_TOT] = 0,
424 [ST_F_CLI_ABRT] = ST_F_SRV_ABRT,
425 [ST_F_SRV_ABRT] = ST_F_WEIGHT,
426 [ST_F_COMP_IN] = 0,
427 [ST_F_COMP_OUT] = 0,
428 [ST_F_COMP_BYP] = 0,
429 [ST_F_COMP_RSP] = 0,
430 [ST_F_LASTSESS] = ST_F_QCUR,
431 [ST_F_LAST_CHK] = 0,
432 [ST_F_LAST_AGT] = 0,
433 [ST_F_QTIME] = ST_F_CTIME,
434 [ST_F_CTIME] = ST_F_RTIME,
435 [ST_F_RTIME] = ST_F_TTIME,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100436 [ST_F_TTIME] = ST_F_QT_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100437 [ST_F_AGENT_STATUS] = 0,
438 [ST_F_AGENT_CODE] = 0,
439 [ST_F_AGENT_DURATION] = 0,
440 [ST_F_CHECK_DESC] = 0,
441 [ST_F_AGENT_DESC] = 0,
442 [ST_F_CHECK_RISE] = 0,
443 [ST_F_CHECK_FALL] = 0,
444 [ST_F_CHECK_HEALTH] = 0,
445 [ST_F_AGENT_RISE] = 0,
446 [ST_F_AGENT_FALL] = 0,
447 [ST_F_AGENT_HEALTH] = 0,
448 [ST_F_ADDR] = 0,
449 [ST_F_COOKIE] = 0,
450 [ST_F_MODE] = 0,
451 [ST_F_ALGO] = 0,
452 [ST_F_CONN_RATE] = 0,
453 [ST_F_CONN_RATE_MAX] = 0,
454 [ST_F_CONN_TOT] = 0,
455 [ST_F_INTERCEPTED] = 0,
456 [ST_F_DCON] = 0,
457 [ST_F_DSES] = 0,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100458 [ST_F_WREW] = ST_F_EINT,
Christopher Fauletf959d082019-02-07 15:38:42 +0100459 [ST_F_CONNECT] = ST_F_REUSE,
460 [ST_F_REUSE] = ST_F_DRESP,
461 [ST_F_CACHE_LOOKUPS] = 0,
462 [ST_F_CACHE_HITS] = 0,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100463 [ST_F_SRV_ICUR] = ST_F_SRV_ILIM,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200464 [ST_F_SRV_ILIM] = ST_F_IDLE_CONN_CUR,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100465 [ST_F_QT_MAX] = ST_F_CT_MAX,
466 [ST_F_CT_MAX] = ST_F_RT_MAX,
467 [ST_F_RT_MAX] = ST_F_TT_MAX,
468 [ST_F_TT_MAX] = ST_F_CONNECT,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100469 [ST_F_EINT] = ST_F_CLI_ABRT,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200470 [ST_F_IDLE_CONN_CUR] = ST_F_SAFE_CONN_CUR,
471 [ST_F_SAFE_CONN_CUR] = ST_F_USED_CONN_CUR,
472 [ST_F_USED_CONN_CUR] = ST_F_NEED_CONN_EST,
473 [ST_F_NEED_CONN_EST] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100474};
475
476/* Name of all info fields */
477const struct ist promex_inf_metric_names[INF_TOTAL_FIELDS] = {
478 [INF_NAME] = IST("name"),
479 [INF_VERSION] = IST("version"),
480 [INF_RELEASE_DATE] = IST("release_date"),
William Dauchy5a982a72021-01-08 13:18:06 +0100481 [INF_BUILD_INFO] = IST("build_info"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100482 [INF_NBTHREAD] = IST("nbthread"),
483 [INF_NBPROC] = IST("nbproc"),
484 [INF_PROCESS_NUM] = IST("relative_process_id"),
485 [INF_PID] = IST("pid"),
486 [INF_UPTIME] = IST("uptime"),
William Dauchydefd1562021-01-15 22:41:38 +0100487 [INF_UPTIME_SEC] = IST("uptime_seconds"),
488 [INF_START_TIME_SEC] = IST("start_time_seconds"),
William Dauchya8766cf2021-01-15 22:41:37 +0100489 [INF_MEMMAX_BYTES] = IST("max_memory_bytes"),
490 [INF_POOL_ALLOC_BYTES] = IST("pool_allocated_bytes"),
491 [INF_POOL_USED_BYTES] = IST("pool_used_bytes"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100492 [INF_POOL_FAILED] = IST("pool_failures_total"),
493 [INF_ULIMIT_N] = IST("max_fds"),
494 [INF_MAXSOCK] = IST("max_sockets"),
495 [INF_MAXCONN] = IST("max_connections"),
496 [INF_HARD_MAXCONN] = IST("hard_max_connections"),
497 [INF_CURR_CONN] = IST("current_connections"),
498 [INF_CUM_CONN] = IST("connections_total"),
499 [INF_CUM_REQ] = IST("requests_total"),
500 [INF_MAX_SSL_CONNS] = IST("max_ssl_connections"),
501 [INF_CURR_SSL_CONNS] = IST("current_ssl_connections"),
502 [INF_CUM_SSL_CONNS] = IST("ssl_connections_total"),
503 [INF_MAXPIPES] = IST("max_pipes"),
504 [INF_PIPES_USED] = IST("pipes_used_total"),
505 [INF_PIPES_FREE] = IST("pipes_free_total"),
506 [INF_CONN_RATE] = IST("current_connection_rate"),
507 [INF_CONN_RATE_LIMIT] = IST("limit_connection_rate"),
508 [INF_MAX_CONN_RATE] = IST("max_connection_rate"),
509 [INF_SESS_RATE] = IST("current_session_rate"),
510 [INF_SESS_RATE_LIMIT] = IST("limit_session_rate"),
511 [INF_MAX_SESS_RATE] = IST("max_session_rate"),
512 [INF_SSL_RATE] = IST("current_ssl_rate"),
513 [INF_SSL_RATE_LIMIT] = IST("limit_ssl_rate"),
514 [INF_MAX_SSL_RATE] = IST("max_ssl_rate"),
515 [INF_SSL_FRONTEND_KEY_RATE] = IST("current_frontend_ssl_key_rate"),
516 [INF_SSL_FRONTEND_MAX_KEY_RATE] = IST("max_frontend_ssl_key_rate"),
Pierre Cheynier1e369762020-07-07 19:14:08 +0200517 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = IST("frontend_ssl_reuse"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100518 [INF_SSL_BACKEND_KEY_RATE] = IST("current_backend_ssl_key_rate"),
519 [INF_SSL_BACKEND_MAX_KEY_RATE] = IST("max_backend_ssl_key_rate"),
Christopher Faulet8c8e4b12019-04-18 10:15:15 +0200520 [INF_SSL_CACHE_LOOKUPS] = IST("ssl_cache_lookups_total"),
521 [INF_SSL_CACHE_MISSES] = IST("ssl_cache_misses_total"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100522 [INF_COMPRESS_BPS_IN] = IST("http_comp_bytes_in_total"),
523 [INF_COMPRESS_BPS_OUT] = IST("http_comp_bytes_out_total"),
524 [INF_COMPRESS_BPS_RATE_LIM] = IST("limit_http_comp"),
525 [INF_ZLIB_MEM_USAGE] = IST("current_zlib_memory"),
526 [INF_MAX_ZLIB_MEM_USAGE] = IST("max_zlib_memory"),
527 [INF_TASKS] = IST("current_tasks"),
528 [INF_RUN_QUEUE] = IST("current_run_queue"),
529 [INF_IDLE_PCT] = IST("idle_time_percent"),
530 [INF_NODE] = IST("node"),
531 [INF_DESCRIPTION] = IST("description"),
532 [INF_STOPPING] = IST("stopping"),
533 [INF_JOBS] = IST("jobs"),
534 [INF_UNSTOPPABLE_JOBS] = IST("unstoppable_jobs"),
535 [INF_LISTENERS] = IST("listeners"),
536 [INF_ACTIVE_PEERS] = IST("active_peers"),
537 [INF_CONNECTED_PEERS] = IST("connected_peers"),
538 [INF_DROPPED_LOGS] = IST("dropped_logs_total"),
539 [INF_BUSY_POLLING] = IST("busy_polling_enabled"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200540 [INF_FAILED_RESOLUTIONS] = IST("failed_resolutions"),
541 [INF_TOTAL_BYTES_OUT] = IST("bytes_out_total"),
542 [INF_TOTAL_SPLICED_BYTES_OUT] = IST("spliced_bytes_out_total"),
543 [INF_BYTES_OUT_RATE] = IST("bytes_out_rate"),
544 [INF_DEBUG_COMMANDS_ISSUED] = IST("debug_commands_issued"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100545};
546
547/* Name of all stats fields */
548const struct ist promex_st_metric_names[ST_F_TOTAL_FIELDS] = {
549 [ST_F_PXNAME] = IST("proxy_name"),
550 [ST_F_SVNAME] = IST("service_name"),
551 [ST_F_QCUR] = IST("current_queue"),
552 [ST_F_QMAX] = IST("max_queue"),
553 [ST_F_SCUR] = IST("current_sessions"),
554 [ST_F_SMAX] = IST("max_sessions"),
555 [ST_F_SLIM] = IST("limit_sessions"),
556 [ST_F_STOT] = IST("sessions_total"),
557 [ST_F_BIN] = IST("bytes_in_total"),
558 [ST_F_BOUT] = IST("bytes_out_total"),
559 [ST_F_DREQ] = IST("requests_denied_total"),
560 [ST_F_DRESP] = IST("responses_denied_total"),
561 [ST_F_EREQ] = IST("request_errors_total"),
562 [ST_F_ECON] = IST("connection_errors_total"),
563 [ST_F_ERESP] = IST("response_errors_total"),
564 [ST_F_WRETR] = IST("retry_warnings_total"),
565 [ST_F_WREDIS] = IST("redispatch_warnings_total"),
566 [ST_F_STATUS] = IST("status"),
567 [ST_F_WEIGHT] = IST("weight"),
568 [ST_F_ACT] = IST("active_servers"),
569 [ST_F_BCK] = IST("backup_servers"),
570 [ST_F_CHKFAIL] = IST("check_failures_total"),
571 [ST_F_CHKDOWN] = IST("check_up_down_total"),
572 [ST_F_LASTCHG] = IST("check_last_change_seconds"),
573 [ST_F_DOWNTIME] = IST("downtime_seconds_total"),
574 [ST_F_QLIMIT] = IST("queue_limit"),
575 [ST_F_PID] = IST("pid"),
576 [ST_F_IID] = IST("proxy_id"),
577 [ST_F_SID] = IST("server_id"),
578 [ST_F_THROTTLE] = IST("current_throttle"),
579 [ST_F_LBTOT] = IST("loadbalanced_total"),
580 [ST_F_TRACKED] = IST("tracked"),
581 [ST_F_TYPE] = IST("type"),
582 [ST_F_RATE] = IST("current_session_rate"),
583 [ST_F_RATE_LIM] = IST("limit_session_rate"),
584 [ST_F_RATE_MAX] = IST("max_session_rate"),
585 [ST_F_CHECK_STATUS] = IST("check_status"),
586 [ST_F_CHECK_CODE] = IST("check_code"),
Christopher Faulet2711e512020-02-27 16:12:07 +0100587 [ST_F_CHECK_DURATION] = IST("check_duration_seconds"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100588 [ST_F_HRSP_1XX] = IST("http_responses_total"),
589 [ST_F_HRSP_2XX] = IST("http_responses_total"),
590 [ST_F_HRSP_3XX] = IST("http_responses_total"),
591 [ST_F_HRSP_4XX] = IST("http_responses_total"),
592 [ST_F_HRSP_5XX] = IST("http_responses_total"),
593 [ST_F_HRSP_OTHER] = IST("http_responses_total"),
594 [ST_F_HANAFAIL] = IST("check_analyses_failures_total"),
595 [ST_F_REQ_RATE] = IST("http_requests_rate_current"),
596 [ST_F_REQ_RATE_MAX] = IST("http_requests_rate_max"),
597 [ST_F_REQ_TOT] = IST("http_requests_total"),
598 [ST_F_CLI_ABRT] = IST("client_aborts_total"),
599 [ST_F_SRV_ABRT] = IST("server_aborts_total"),
600 [ST_F_COMP_IN] = IST("http_comp_bytes_in_total"),
601 [ST_F_COMP_OUT] = IST("http_comp_bytes_out_total"),
602 [ST_F_COMP_BYP] = IST("http_comp_bytes_bypassed_total"),
603 [ST_F_COMP_RSP] = IST("http_comp_responses_total"),
604 [ST_F_LASTSESS] = IST("last_session_seconds"),
605 [ST_F_LAST_CHK] = IST("check_last_content"),
606 [ST_F_LAST_AGT] = IST("agentcheck_last_content"),
Christopher Faulet68b69682019-11-08 15:12:29 +0100607 [ST_F_QTIME] = IST("queue_time_average_seconds"),
608 [ST_F_CTIME] = IST("connect_time_average_seconds"),
609 [ST_F_RTIME] = IST("response_time_average_seconds"),
610 [ST_F_TTIME] = IST("total_time_average_seconds"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100611 [ST_F_AGENT_STATUS] = IST("agentcheck_status"),
612 [ST_F_AGENT_CODE] = IST("agentcheck_code"),
613 [ST_F_AGENT_DURATION] = IST("agentcheck_duration_milliseconds"),
614 [ST_F_CHECK_DESC] = IST("check_description"),
615 [ST_F_AGENT_DESC] = IST("agentcheck_description"),
616 [ST_F_CHECK_RISE] = IST("check_rise"),
617 [ST_F_CHECK_FALL] = IST("check_fall"),
618 [ST_F_CHECK_HEALTH] = IST("check_value"),
619 [ST_F_AGENT_RISE] = IST("agentcheck_rise"),
620 [ST_F_AGENT_FALL] = IST("agentcheck_fall"),
621 [ST_F_AGENT_HEALTH] = IST("agentcheck_value"),
622 [ST_F_ADDR] = IST("address"),
623 [ST_F_COOKIE] = IST("cookie"),
624 [ST_F_MODE] = IST("mode"),
625 [ST_F_ALGO] = IST("loadbalance_algorithm"),
626 [ST_F_CONN_RATE] = IST("connections_rate_current"),
627 [ST_F_CONN_RATE_MAX] = IST("connections_rate_max"),
628 [ST_F_CONN_TOT] = IST("connections_total"),
629 [ST_F_INTERCEPTED] = IST("intercepted_requests_total"),
630 [ST_F_DCON] = IST("denied_connections_total"),
631 [ST_F_DSES] = IST("denied_sessions_total"),
632 [ST_F_WREW] = IST("failed_header_rewriting_total"),
633 [ST_F_CONNECT] = IST("connection_attempts_total"),
634 [ST_F_REUSE] = IST("connection_reuses_total"),
635 [ST_F_CACHE_LOOKUPS] = IST("http_cache_lookups_total"),
636 [ST_F_CACHE_HITS] = IST("http_cache_hits_total"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200637 [ST_F_SRV_ICUR] = IST("idle_connections_current"),
638 [ST_F_SRV_ILIM] = IST("idle_connections_limit"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100639 [ST_F_QT_MAX] = IST("max_queue_time_seconds"),
640 [ST_F_CT_MAX] = IST("max_connect_time_seconds"),
641 [ST_F_RT_MAX] = IST("max_response_time_seconds"),
642 [ST_F_TT_MAX] = IST("max_total_time_seconds"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100643 [ST_F_EINT] = IST("internal_errors_total"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200644 [ST_F_IDLE_CONN_CUR] = IST("unsafe_idle_connections_current"),
645 [ST_F_SAFE_CONN_CUR] = IST("safe_idle_connections_current"),
646 [ST_F_USED_CONN_CUR] = IST("used_connections_current"),
647 [ST_F_NEED_CONN_EST] = IST("need_connections_current"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100648};
649
Christopher Fauletf959d082019-02-07 15:38:42 +0100650/* Description of all stats fields */
651const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
652 [ST_F_PXNAME] = IST("The proxy name."),
653 [ST_F_SVNAME] = IST("The service name (FRONTEND for frontend, BACKEND for backend, any name for server/listener)."),
654 [ST_F_QCUR] = IST("Current number of queued requests."),
655 [ST_F_QMAX] = IST("Maximum observed number of queued requests."),
656 [ST_F_SCUR] = IST("Current number of active sessions."),
657 [ST_F_SMAX] = IST("Maximum observed number of active sessions."),
658 [ST_F_SLIM] = IST("Configured session limit."),
659 [ST_F_STOT] = IST("Total number of sessions."),
660 [ST_F_BIN] = IST("Current total of incoming bytes."),
661 [ST_F_BOUT] = IST("Current total of outgoing bytes."),
662 [ST_F_DREQ] = IST("Total number of denied requests."),
663 [ST_F_DRESP] = IST("Total number of denied responses."),
664 [ST_F_EREQ] = IST("Total number of request errors."),
665 [ST_F_ECON] = IST("Total number of connection errors."),
666 [ST_F_ERESP] = IST("Total number of response errors."),
667 [ST_F_WRETR] = IST("Total number of retry warnings."),
668 [ST_F_WREDIS] = IST("Total number of redispatch warnings."),
Willy Tarreau6b3bf732020-09-24 07:35:46 +0200669 [ST_F_STATUS] = IST("Current status of the service (frontend: 0=STOP, 1=UP - backend: 0=DOWN, 1=UP - server: 0=DOWN, 1=UP, 2=MAINT, 3=DRAIN, 4=NOLB)."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100670 [ST_F_WEIGHT] = IST("Service weight."),
671 [ST_F_ACT] = IST("Current number of active servers."),
672 [ST_F_BCK] = IST("Current number of backup servers."),
673 [ST_F_CHKFAIL] = IST("Total number of failed check (Only counts checks failed when the server is up)."),
674 [ST_F_CHKDOWN] = IST("Total number of UP->DOWN transitions."),
675 [ST_F_LASTCHG] = IST("Number of seconds since the last UP<->DOWN transition."),
676 [ST_F_DOWNTIME] = IST("Total downtime (in seconds) for the service."),
677 [ST_F_QLIMIT] = IST("Configured maxqueue for the server (0 meaning no limit)."),
678 [ST_F_PID] = IST("Process id (0 for first instance, 1 for second, ...)"),
679 [ST_F_IID] = IST("Unique proxy id."),
680 [ST_F_SID] = IST("Server id (unique inside a proxy)."),
681 [ST_F_THROTTLE] = IST("Current throttle percentage for the server, when slowstart is active, or no value if not in slowstart."),
682 [ST_F_LBTOT] = IST("Total number of times a service was selected, either for new sessions, or when redispatching."),
683 [ST_F_TRACKED] = IST("Id of proxy/server if tracking is enabled."),
684 [ST_F_TYPE] = IST("Service type (0=frontend, 1=backend, 2=server, 3=socket/listener)."),
685 [ST_F_RATE] = IST("Current number of sessions per second over last elapsed second."),
686 [ST_F_RATE_LIM] = IST("Configured limit on new sessions per second."),
687 [ST_F_RATE_MAX] = IST("Maximum observed number of sessions per second."),
Christopher Fauletcf403f32019-11-21 14:35:46 +0100688 [ST_F_CHECK_STATUS] = IST("Status of last health check (HCHK_STATUS_* values)."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100689 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100690 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100691 [ST_F_HRSP_1XX] = IST("Total number of HTTP responses."),
692 [ST_F_HRSP_2XX] = IST("Total number of HTTP responses."),
693 [ST_F_HRSP_3XX] = IST("Total number of HTTP responses."),
694 [ST_F_HRSP_4XX] = IST("Total number of HTTP responses."),
695 [ST_F_HRSP_5XX] = IST("Total number of HTTP responses."),
696 [ST_F_HRSP_OTHER] = IST("Total number of HTTP responses."),
697 [ST_F_HANAFAIL] = IST("Total number of failed health checks."),
698 [ST_F_REQ_RATE] = IST("Current number of HTTP requests per second over last elapsed second."),
699 [ST_F_REQ_RATE_MAX] = IST("Maximum observed number of HTTP requests per second."),
700 [ST_F_REQ_TOT] = IST("Total number of HTTP requests received."),
701 [ST_F_CLI_ABRT] = IST("Total number of data transfers aborted by the client."),
702 [ST_F_SRV_ABRT] = IST("Total number of data transfers aborted by the server."),
703 [ST_F_COMP_IN] = IST("Total number of HTTP response bytes fed to the compressor."),
704 [ST_F_COMP_OUT] = IST("Total number of HTTP response bytes emitted by the compressor."),
705 [ST_F_COMP_BYP] = IST("Total number of bytes that bypassed the HTTP compressor (CPU/BW limit)."),
706 [ST_F_COMP_RSP] = IST("Total number of HTTP responses that were compressed."),
707 [ST_F_LASTSESS] = IST("Number of seconds since last session assigned to server/backend."),
708 [ST_F_LAST_CHK] = IST("Last health check contents or textual error"),
709 [ST_F_LAST_AGT] = IST("Last agent check contents or textual error"),
710 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
711 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
712 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
713 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
714 [ST_F_AGENT_STATUS] = IST("Status of last agent check."),
715 [ST_F_AGENT_CODE] = IST("Numeric code reported by agent if any (unused for now)."),
716 [ST_F_AGENT_DURATION] = IST("Time in ms taken to finish last agent check."),
717 [ST_F_CHECK_DESC] = IST("Short human-readable description of the last health status."),
718 [ST_F_AGENT_DESC] = IST("Short human-readable description of the last agent status."),
719 [ST_F_CHECK_RISE] = IST("Server's \"rise\" parameter used by health checks"),
720 [ST_F_CHECK_FALL] = IST("Server's \"fall\" parameter used by health checks"),
721 [ST_F_CHECK_HEALTH] = IST("Server's health check value between 0 and rise+fall-1"),
722 [ST_F_AGENT_RISE] = IST("Agent's \"rise\" parameter, normally 1."),
723 [ST_F_AGENT_FALL] = IST("Agent's \"fall\" parameter, normally 1."),
724 [ST_F_AGENT_HEALTH] = IST("Agent's health parameter, between 0 and rise+fall-1"),
725 [ST_F_ADDR] = IST("address:port or \"unix\". IPv6 has brackets around the address."),
726 [ST_F_COOKIE] = IST("Server's cookie value or backend's cookie name."),
727 [ST_F_MODE] = IST("Proxy mode (tcp, http, health, unknown)."),
728 [ST_F_ALGO] = IST("Load balancing algorithm."),
729 [ST_F_CONN_RATE] = IST("Current number of connections per second over the last elapsed second."),
730 [ST_F_CONN_RATE_MAX] = IST("Maximum observed number of connections per second."),
731 [ST_F_CONN_TOT] = IST("Total number of connections."),
732 [ST_F_INTERCEPTED] = IST("Total number of intercepted HTTP requests."),
733 [ST_F_DCON] = IST("Total number of requests denied by \"tcp-request connection\" rules."),
734 [ST_F_DSES] = IST("Total number of requests denied by \"tcp-request session\" rules."),
735 [ST_F_WREW] = IST("Total number of failed header rewriting warnings."),
736 [ST_F_CONNECT] = IST("Total number of connection establishment attempts."),
737 [ST_F_REUSE] = IST("Total number of connection reuses."),
738 [ST_F_CACHE_LOOKUPS] = IST("Total number of HTTP cache lookups."),
739 [ST_F_CACHE_HITS] = IST("Total number of HTTP cache hits."),
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100740 [ST_F_SRV_ICUR] = IST("Current number of idle connections available for reuse"),
741 [ST_F_SRV_ILIM] = IST("Limit on the number of available idle connections"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100742 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
743 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
744 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
745 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100746 [ST_F_EINT] = IST("Total number of internal errors."),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200747 [ST_F_IDLE_CONN_CUR] = IST("Current number of unsafe idle connections."),
748 [ST_F_SAFE_CONN_CUR] = IST("Current number of safe idle connections."),
749 [ST_F_USED_CONN_CUR] = IST("Current number of connections in use."),
750 [ST_F_NEED_CONN_EST] = IST("Estimated needed number of connections."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100751};
752
753/* Specific labels for all info fields. Empty by default. */
754const struct ist promex_inf_metric_labels[INF_TOTAL_FIELDS] = {
Christopher Faulet0175b1f2021-01-20 15:28:22 +0100755 [INF_BUILD_INFO] = IST(PROMEX_BUILDINFO_LABEL),
Christopher Fauletf959d082019-02-07 15:38:42 +0100756};
757
758/* Specific labels for all stats fields. Empty by default. */
759const struct ist promex_st_metric_labels[ST_F_TOTAL_FIELDS] = {
Christopher Faulet0175b1f2021-01-20 15:28:22 +0100760 [ST_F_HRSP_1XX] = IST("code=\"1xx\""),
761 [ST_F_HRSP_2XX] = IST("code=\"2xx\""),
762 [ST_F_HRSP_3XX] = IST("code=\"3xx\""),
763 [ST_F_HRSP_4XX] = IST("code=\"4xx\""),
764 [ST_F_HRSP_5XX] = IST("code=\"5xx\""),
765 [ST_F_HRSP_OTHER] = IST("code=\"other\""),
Christopher Fauletf959d082019-02-07 15:38:42 +0100766};
767
768/* Type for all info fields. "untyped" is used for unsupported field. */
769const struct ist promex_inf_metric_types[INF_TOTAL_FIELDS] = {
770 [INF_NAME] = IST("untyped"),
771 [INF_VERSION] = IST("untyped"),
772 [INF_RELEASE_DATE] = IST("untyped"),
William Dauchy5a982a72021-01-08 13:18:06 +0100773 [INF_BUILD_INFO] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200774 [INF_NBTHREAD] = IST("gauge"),
775 [INF_NBPROC] = IST("gauge"),
776 [INF_PROCESS_NUM] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100777 [INF_PID] = IST("untyped"),
778 [INF_UPTIME] = IST("untyped"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200779 [INF_UPTIME_SEC] = IST("gauge"),
William Dauchydefd1562021-01-15 22:41:38 +0100780 [INF_START_TIME_SEC] = IST("gauge"),
William Dauchya8766cf2021-01-15 22:41:37 +0100781 [INF_MEMMAX_BYTES] = IST("gauge"),
782 [INF_POOL_ALLOC_BYTES] = IST("gauge"),
783 [INF_POOL_USED_BYTES] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100784 [INF_POOL_FAILED] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200785 [INF_ULIMIT_N] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100786 [INF_MAXSOCK] = IST("gauge"),
787 [INF_MAXCONN] = IST("gauge"),
788 [INF_HARD_MAXCONN] = IST("gauge"),
789 [INF_CURR_CONN] = IST("gauge"),
790 [INF_CUM_CONN] = IST("counter"),
791 [INF_CUM_REQ] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200792 [INF_MAX_SSL_CONNS] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100793 [INF_CURR_SSL_CONNS] = IST("gauge"),
794 [INF_CUM_SSL_CONNS] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200795 [INF_MAXPIPES] = IST("gauge"),
796 [INF_PIPES_USED] = IST("counter"),
797 [INF_PIPES_FREE] = IST("counter"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100798 [INF_CONN_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200799 [INF_CONN_RATE_LIMIT] = IST("gauge"),
800 [INF_MAX_CONN_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100801 [INF_SESS_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200802 [INF_SESS_RATE_LIMIT] = IST("gauge"),
803 [INF_MAX_SESS_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100804 [INF_SSL_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200805 [INF_SSL_RATE_LIMIT] = IST("gauge"),
806 [INF_MAX_SSL_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100807 [INF_SSL_FRONTEND_KEY_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200808 [INF_SSL_FRONTEND_MAX_KEY_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100809 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = IST("gauge"),
810 [INF_SSL_BACKEND_KEY_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200811 [INF_SSL_BACKEND_MAX_KEY_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100812 [INF_SSL_CACHE_LOOKUPS] = IST("counter"),
813 [INF_SSL_CACHE_MISSES] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200814 [INF_COMPRESS_BPS_IN] = IST("counter"),
815 [INF_COMPRESS_BPS_OUT] = IST("counter"),
816 [INF_COMPRESS_BPS_RATE_LIM] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100817 [INF_ZLIB_MEM_USAGE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200818 [INF_MAX_ZLIB_MEM_USAGE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100819 [INF_TASKS] = IST("gauge"),
Christopher Fauletf782c232019-04-17 16:04:44 +0200820 [INF_RUN_QUEUE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100821 [INF_IDLE_PCT] = IST("gauge"),
822 [INF_NODE] = IST("untyped"),
823 [INF_DESCRIPTION] = IST("untyped"),
824 [INF_STOPPING] = IST("gauge"),
825 [INF_JOBS] = IST("gauge"),
826 [INF_UNSTOPPABLE_JOBS] = IST("gauge"),
827 [INF_LISTENERS] = IST("gauge"),
828 [INF_ACTIVE_PEERS] = IST("gauge"),
829 [INF_CONNECTED_PEERS] = IST("gauge"),
830 [INF_DROPPED_LOGS] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200831 [INF_BUSY_POLLING] = IST("gauge"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200832 [INF_FAILED_RESOLUTIONS] = IST("counter"),
833 [INF_TOTAL_BYTES_OUT] = IST("counter"),
834 [INF_TOTAL_SPLICED_BYTES_OUT] = IST("counter"),
835 [INF_BYTES_OUT_RATE] = IST("gauge"),
836 [INF_DEBUG_COMMANDS_ISSUED] = IST(""),
Christopher Fauletf959d082019-02-07 15:38:42 +0100837};
838
839/* Type for all stats fields. "untyped" is used for unsupported field. */
840const struct ist promex_st_metric_types[ST_F_TOTAL_FIELDS] = {
841 [ST_F_PXNAME] = IST("untyped"),
842 [ST_F_SVNAME] = IST("untyped"),
843 [ST_F_QCUR] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200844 [ST_F_QMAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100845 [ST_F_SCUR] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200846 [ST_F_SMAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100847 [ST_F_SLIM] = IST("gauge"),
848 [ST_F_STOT] = IST("counter"),
849 [ST_F_BIN] = IST("counter"),
850 [ST_F_BOUT] = IST("counter"),
851 [ST_F_DREQ] = IST("counter"),
852 [ST_F_DRESP] = IST("counter"),
853 [ST_F_EREQ] = IST("counter"),
854 [ST_F_ECON] = IST("counter"),
855 [ST_F_ERESP] = IST("counter"),
856 [ST_F_WRETR] = IST("counter"),
857 [ST_F_WREDIS] = IST("counter"),
858 [ST_F_STATUS] = IST("gauge"),
859 [ST_F_WEIGHT] = IST("gauge"),
860 [ST_F_ACT] = IST("gauge"),
861 [ST_F_BCK] = IST("gauge"),
862 [ST_F_CHKFAIL] = IST("counter"),
863 [ST_F_CHKDOWN] = IST("counter"),
864 [ST_F_LASTCHG] = IST("gauge"),
865 [ST_F_DOWNTIME] = IST("counter"),
866 [ST_F_QLIMIT] = IST("gauge"),
867 [ST_F_PID] = IST("untyped"),
868 [ST_F_IID] = IST("untyped"),
869 [ST_F_SID] = IST("untyped"),
870 [ST_F_THROTTLE] = IST("gauge"),
871 [ST_F_LBTOT] = IST("counter"),
872 [ST_F_TRACKED] = IST("untyped"),
873 [ST_F_TYPE] = IST("untyped"),
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200874 [ST_F_RATE] = IST("untyped"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100875 [ST_F_RATE_LIM] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200876 [ST_F_RATE_MAX] = IST("gauge"),
Christopher Fauletcf403f32019-11-21 14:35:46 +0100877 [ST_F_CHECK_STATUS] = IST("gauge"),
878 [ST_F_CHECK_CODE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100879 [ST_F_CHECK_DURATION] = IST("gauge"),
880 [ST_F_HRSP_1XX] = IST("counter"),
881 [ST_F_HRSP_2XX] = IST("counter"),
882 [ST_F_HRSP_3XX] = IST("counter"),
883 [ST_F_HRSP_4XX] = IST("counter"),
884 [ST_F_HRSP_5XX] = IST("counter"),
885 [ST_F_HRSP_OTHER] = IST("counter"),
886 [ST_F_HANAFAIL] = IST("counter"),
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200887 [ST_F_REQ_RATE] = IST("untyped"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200888 [ST_F_REQ_RATE_MAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100889 [ST_F_REQ_TOT] = IST("counter"),
890 [ST_F_CLI_ABRT] = IST("counter"),
891 [ST_F_SRV_ABRT] = IST("counter"),
892 [ST_F_COMP_IN] = IST("counter"),
893 [ST_F_COMP_OUT] = IST("counter"),
894 [ST_F_COMP_BYP] = IST("counter"),
895 [ST_F_COMP_RSP] = IST("counter"),
896 [ST_F_LASTSESS] = IST("gauge"),
897 [ST_F_LAST_CHK] = IST("untyped"),
898 [ST_F_LAST_AGT] = IST("untyped"),
899 [ST_F_QTIME] = IST("gauge"),
900 [ST_F_CTIME] = IST("gauge"),
901 [ST_F_RTIME] = IST("gauge"),
902 [ST_F_TTIME] = IST("gauge"),
903 [ST_F_AGENT_STATUS] = IST("untyped"),
904 [ST_F_AGENT_CODE] = IST("untyped"),
905 [ST_F_AGENT_DURATION] = IST("gauge"),
906 [ST_F_CHECK_DESC] = IST("untyped"),
907 [ST_F_AGENT_DESC] = IST("untyped"),
908 [ST_F_CHECK_RISE] = IST("gauge"),
909 [ST_F_CHECK_FALL] = IST("gauge"),
910 [ST_F_CHECK_HEALTH] = IST("gauge"),
911 [ST_F_AGENT_RISE] = IST("gauge"),
912 [ST_F_AGENT_FALL] = IST("gauge"),
913 [ST_F_AGENT_HEALTH] = IST("gauge"),
914 [ST_F_ADDR] = IST("untyped"),
915 [ST_F_COOKIE] = IST("untyped"),
916 [ST_F_MODE] = IST("untyped"),
917 [ST_F_ALGO] = IST("untyped"),
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200918 [ST_F_CONN_RATE] = IST("untyped"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200919 [ST_F_CONN_RATE_MAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100920 [ST_F_CONN_TOT] = IST("counter"),
921 [ST_F_INTERCEPTED] = IST("counter"),
922 [ST_F_DCON] = IST("counter"),
923 [ST_F_DSES] = IST("counter"),
924 [ST_F_WREW] = IST("counter"),
925 [ST_F_CONNECT] = IST("counter"),
926 [ST_F_REUSE] = IST("counter"),
927 [ST_F_CACHE_LOOKUPS] = IST("counter"),
928 [ST_F_CACHE_HITS] = IST("counter"),
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100929 [ST_F_SRV_ICUR] = IST("gauge"),
930 [ST_F_SRV_ILIM] = IST("gauge"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100931 [ST_F_QT_MAX] = IST("gauge"),
932 [ST_F_CT_MAX] = IST("gauge"),
933 [ST_F_RT_MAX] = IST("gauge"),
934 [ST_F_TT_MAX] = IST("gauge"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100935 [ST_F_EINT] = IST("counter"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200936 [ST_F_IDLE_CONN_CUR] = IST("gauge"),
937 [ST_F_SAFE_CONN_CUR] = IST("gauge"),
938 [ST_F_USED_CONN_CUR] = IST("gauge"),
939 [ST_F_NEED_CONN_EST] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100940};
941
Christopher Fauletd45d1052019-09-06 16:10:19 +0200942/* Return the server status: 0=DOWN, 1=UP, 2=MAINT, 3=DRAIN, 4=NOLB. */
Christopher Fauletf959d082019-02-07 15:38:42 +0100943static int promex_srv_status(struct server *sv)
944{
Christopher Fauletf959d082019-02-07 15:38:42 +0100945 int state = 0;
946
Christopher Fauletf959d082019-02-07 15:38:42 +0100947 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
948 state = 1;
949 if (sv->cur_admin & SRV_ADMF_DRAIN)
Christopher Fauletd45d1052019-09-06 16:10:19 +0200950 state = 3;
Christopher Fauletf959d082019-02-07 15:38:42 +0100951 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200952 else if (sv->cur_state == SRV_ST_STOPPING)
953 state = 4;
954
955 if (sv->cur_admin & SRV_ADMF_MAINT)
956 state = 2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100957
958 return state;
959}
960
961/* Convert a field to its string representation and write it in <out>, followed
962 * by a newline, if there is enough space. non-numeric value are converted in
963 * "Nan" because Prometheus only support numerical values (but it is unexepceted
964 * to process this kind of value). It returns 1 on success. Otherwise, it
965 * returns 0. The buffer's length must not exceed <max> value.
966 */
967static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
968{
969 int ret = 0;
970
971 switch (field_format(f, 0)) {
972 case FF_EMPTY: ret = chunk_strcat(out, "Nan\n"); break;
973 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
974 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
975 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
976 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200977 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100978 case FF_STR: ret = chunk_strcat(out, "Nan\n"); break;
979 default: ret = chunk_strcat(out, "Nan\n"); break;
980 }
981 if (!ret || out->data > max)
982 return 0;
983 return 1;
984}
985
986/* Concatenate the <prefix> with the field name using the array
987 * <promex_st_metric_names> and store it in <name>. The field type is in
988 * <appctx->st2>. This function never fails but relies on
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500989 * <PROMEX_MAX_NAME_LEN>. So by sure the result is small enough to be copied in
Christopher Fauletf959d082019-02-07 15:38:42 +0100990 * <name>
991 */
992static void promex_metric_name(struct appctx *appctx, struct ist *name, const struct ist prefix)
993{
994 const struct ist *names;
995
996 names = ((appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC)
997 ? promex_inf_metric_names
998 : promex_st_metric_names);
999
1000 istcat(name, prefix, PROMEX_MAX_NAME_LEN);
1001 istcat(name, names[appctx->st2], PROMEX_MAX_NAME_LEN);
1002}
1003
1004/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
1005 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
1006 */
1007static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
1008 const struct ist name, struct ist *out, size_t max)
1009{
1010 const struct ist *desc, *types;
1011
William Dauchya191b772021-01-15 22:41:39 +01001012 if (istcat(out, ist("# HELP "), max) == -1 ||
1013 istcat(out, name, max) == -1 ||
1014 istcat(out, ist(" "), max) == -1)
1015 goto full;
1016
Christopher Fauletf959d082019-02-07 15:38:42 +01001017 if (appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC) {
Christopher Fauletf959d082019-02-07 15:38:42 +01001018 types = promex_inf_metric_types;
William Dauchya191b772021-01-15 22:41:39 +01001019
1020 if (istcat(out, ist(info_fields[appctx->st2].desc), max) == -1)
1021 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +01001022 }
1023 else {
1024 desc = promex_st_metric_desc;
1025 types = promex_st_metric_types;
William Dauchya191b772021-01-15 22:41:39 +01001026
1027 if (istcat(out, desc[appctx->st2], max) == -1)
1028 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +01001029 }
1030
William Dauchya191b772021-01-15 22:41:39 +01001031 if (istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001032 istcat(out, name, max) == -1 ||
1033 istcat(out, ist(" "), max) == -1 ||
1034 istcat(out, types[appctx->st2], max) == -1 ||
1035 istcat(out, ist("\n"), max) == -1)
1036 goto full;
1037
1038 return 1;
1039
1040 full:
1041 return 0;
1042}
1043
1044/* Dump the line for <metric>. It starts by the metric name followed by its
1045 * labels (proxy name, server name...) between braces and finally its value. If
1046 * not already done, the header lines are dumped first. It returns 1 on
1047 * success. Otherwise if <out> length exceeds <max>, it returns 0.
1048 */
1049static int promex_dump_metric(struct appctx *appctx, struct htx *htx,
1050 const struct ist prefix, struct field *metric,
1051 struct ist *out, size_t max)
1052{
1053 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
1054 size_t len = out->len;
1055
1056 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
1057 return 0;
1058
1059 promex_metric_name(appctx, &name, prefix);
1060 if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) &&
1061 !promex_dump_metric_header(appctx, htx, name, out, max))
1062 goto full;
1063
1064 if (appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC) {
1065 const struct ist label = promex_inf_metric_labels[appctx->st2];
1066
1067 if (istcat(out, name, max) == -1 ||
1068 (label.len && istcat(out, ist("{"), max) == -1) ||
1069 (label.len && istcat(out, label, max) == -1) ||
1070 (label.len && istcat(out, ist("}"), max) == -1) ||
1071 istcat(out, ist(" "), max) == -1)
1072 goto full;
1073 }
1074 else {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001075 struct proxy *px = appctx->ctx.stats.obj1;
1076 struct server *srv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +01001077 const struct ist label = promex_st_metric_labels[appctx->st2];
1078
1079 if (istcat(out, name, max) == -1 ||
1080 istcat(out, ist("{proxy=\""), max) == -1 ||
1081 istcat(out, ist2(px->id, strlen(px->id)), max) == -1 ||
1082 istcat(out, ist("\""), max) == -1 ||
1083 (srv && istcat(out, ist(",server=\""), max) == -1) ||
1084 (srv && istcat(out, ist2(srv->id, strlen(srv->id)), max) == -1) ||
1085 (srv && istcat(out, ist("\""), max) == -1) ||
1086 (label.len && istcat(out, ist(","), max) == -1) ||
1087 (label.len && istcat(out, label, max) == -1) ||
1088 istcat(out, ist("} "), max) == -1)
1089 goto full;
1090 }
1091
1092 trash.data = out->len;
1093 if (!promex_metric_to_str(&trash, metric, max))
1094 goto full;
1095 out->len = trash.data;
1096
1097 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1098 return 1;
1099 full:
1100 // Restore previous length
1101 out->len = len;
1102 return 0;
1103
1104}
1105
1106
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001107/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001108 * 0 if <htx> is full and -1 in case of any error. */
1109static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
1110{
1111 static struct ist prefix = IST("haproxy_process_");
1112 struct field metric;
1113 struct channel *chn = si_ic(appctx->owner);
1114 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001115 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001116 int ret = 1;
1117
William Dauchy5d9b8f32021-01-11 20:07:49 +01001118 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
1119 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001120
Christopher Fauletf959d082019-02-07 15:38:42 +01001121 while (appctx->st2 && appctx->st2 < INF_TOTAL_FIELDS) {
1122 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +01001123 case INF_BUILD_INFO:
1124 metric = mkf_u32(FN_GAUGE, 1);
1125 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001126
1127 default:
William Dauchy5d9b8f32021-01-11 20:07:49 +01001128 metric = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001129 }
1130
1131 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1132 goto full;
1133
Christopher Fauletf959d082019-02-07 15:38:42 +01001134 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
1135 appctx->st2 = promex_global_metrics[appctx->st2];
1136 }
1137
1138 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001139 if (out.len) {
1140 if (!htx_add_data_atonce(htx, out))
1141 return -1; /* Unexpected and unrecoverable error */
1142 channel_add_input(chn, out.len);
1143 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001144 return ret;
1145 full:
1146 ret = 0;
1147 goto end;
1148}
1149
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001150/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001151 * 0 if <htx> is full and -1 in case of any error. */
1152static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
1153{
1154 static struct ist prefix = IST("haproxy_frontend_");
1155 struct proxy *px;
1156 struct field metric;
1157 struct channel *chn = si_ic(appctx->owner);
1158 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001159 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +01001160 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +01001161 int ret = 1;
1162
1163 while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001164 while (appctx->ctx.stats.obj1) {
1165 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001166
1167 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001168 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +01001169 goto next_px;
1170
William Dauchyb9577452021-01-17 18:27:46 +01001171 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
1172 return -1;
1173
Christopher Fauletf959d082019-02-07 15:38:42 +01001174 switch (appctx->st2) {
1175 case ST_F_STATUS:
Willy Tarreauc3914d42020-09-24 08:39:22 +02001176 metric = mkf_u32(FO_STATUS, !px->disabled);
Christopher Fauletf959d082019-02-07 15:38:42 +01001177 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001178 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001179 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001180 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001181 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +01001182 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +01001183 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +01001184 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +01001185 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001186 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +01001187 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +01001188 if (px->mode != PR_MODE_HTTP)
1189 goto next_px;
William Dauchyb9577452021-01-17 18:27:46 +01001190 metric = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001191 break;
William Dauchyb9577452021-01-17 18:27:46 +01001192 case ST_F_HRSP_2XX:
1193 case ST_F_HRSP_3XX:
1194 case ST_F_HRSP_4XX:
1195 case ST_F_HRSP_5XX:
1196 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +01001197 if (px->mode != PR_MODE_HTTP)
1198 goto next_px;
William Dauchyb9577452021-01-17 18:27:46 +01001199 metric = stats[appctx->st2];
1200 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +01001201 break;
1202
1203 default:
William Dauchyb9577452021-01-17 18:27:46 +01001204 metric = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001205 }
1206
1207 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1208 goto full;
1209 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001210 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001211 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001212 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001213 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +01001214 appctx->st2 = promex_front_metrics[appctx->st2];
1215 }
1216
1217 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001218 if (out.len) {
1219 if (!htx_add_data_atonce(htx, out))
1220 return -1; /* Unexpected and unrecoverable error */
1221 channel_add_input(chn, out.len);
1222 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001223 return ret;
1224 full:
1225 ret = 0;
1226 goto end;
1227}
1228
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001229/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001230 * 0 if <htx> is full and -1 in case of any error. */
1231static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
1232{
1233 static struct ist prefix = IST("haproxy_backend_");
1234 struct proxy *px;
1235 struct field metric;
1236 struct channel *chn = si_ic(appctx->owner);
1237 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001238 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001239 int ret = 1;
1240 uint32_t weight;
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001241 double secs;
Christopher Fauletf959d082019-02-07 15:38:42 +01001242
1243 while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001244 while (appctx->ctx.stats.obj1) {
1245 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001246
1247 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001248 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +01001249 goto next_px;
1250
1251 switch (appctx->st2) {
1252 case ST_F_STATUS:
1253 metric = mkf_u32(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
1254 break;
1255 case ST_F_SCUR:
1256 metric = mkf_u32(0, px->beconn);
1257 break;
1258 case ST_F_SMAX:
1259 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
1260 break;
1261 case ST_F_SLIM:
1262 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1263 break;
1264 case ST_F_STOT:
1265 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1266 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001267 case ST_F_RATE_MAX:
1268 metric = mkf_u32(0, px->be_counters.sps_max);
1269 break;
1270 case ST_F_LASTSESS:
1271 metric = mkf_s32(FN_AGE, be_lastsession(px));
1272 break;
1273 case ST_F_QCUR:
1274 metric = mkf_u32(0, px->nbpend);
1275 break;
1276 case ST_F_QMAX:
1277 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
1278 break;
1279 case ST_F_CONNECT:
1280 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
1281 break;
1282 case ST_F_REUSE:
1283 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
1284 break;
1285 case ST_F_BIN:
1286 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1287 break;
1288 case ST_F_BOUT:
1289 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1290 break;
1291 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001292 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
1293 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001294 break;
1295 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001296 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
1297 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001298 break;
1299 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001300 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
1301 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001302 break;
1303 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001304 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
1305 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001306 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001307 case ST_F_QT_MAX:
1308 secs = (double)px->be_counters.qtime_max / 1000.0;
1309 metric = mkf_flt(FN_MAX, secs);
1310 break;
1311 case ST_F_CT_MAX:
1312 secs = (double)px->be_counters.ctime_max / 1000.0;
1313 metric = mkf_flt(FN_MAX, secs);
1314 break;
1315 case ST_F_RT_MAX:
1316 secs = (double)px->be_counters.dtime_max / 1000.0;
1317 metric = mkf_flt(FN_MAX, secs);
1318 break;
1319 case ST_F_TT_MAX:
1320 secs = (double)px->be_counters.ttime_max / 1000.0;
1321 metric = mkf_flt(FN_MAX, secs);
1322 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001323 case ST_F_DREQ:
1324 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1325 break;
1326 case ST_F_DRESP:
1327 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1328 break;
1329 case ST_F_ECON:
1330 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1331 break;
1332 case ST_F_ERESP:
1333 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1334 break;
1335 case ST_F_WRETR:
1336 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
1337 break;
1338 case ST_F_WREDIS:
1339 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
1340 break;
1341 case ST_F_WREW:
1342 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
1343 break;
Christopher Faulete4a2c8d2019-12-16 14:44:01 +01001344 case ST_F_EINT:
1345 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
1346 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001347 case ST_F_CLI_ABRT:
1348 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1349 break;
1350 case ST_F_SRV_ABRT:
1351 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1352 break;
1353 case ST_F_WEIGHT:
1354 weight = (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
1355 metric = mkf_u32(FN_AVG, weight);
1356 break;
1357 case ST_F_ACT:
1358 metric = mkf_u32(0, px->srv_act);
1359 break;
1360 case ST_F_BCK:
1361 metric = mkf_u32(0, px->srv_bck);
1362 break;
1363 case ST_F_CHKDOWN:
1364 metric = mkf_u64(FN_COUNTER, px->down_trans);
1365 break;
1366 case ST_F_LASTCHG:
1367 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1368 break;
1369 case ST_F_DOWNTIME:
1370 metric = mkf_u32(FN_COUNTER, be_downtime(px));
1371 break;
1372 case ST_F_LBTOT:
1373 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1374 break;
1375 case ST_F_REQ_TOT:
1376 if (px->mode != PR_MODE_HTTP)
1377 goto next_px;
1378 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1379 break;
1380 case ST_F_HRSP_1XX:
1381 if (px->mode != PR_MODE_HTTP)
1382 goto next_px;
1383 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1384 break;
1385 case ST_F_HRSP_2XX:
1386 if (px->mode != PR_MODE_HTTP)
1387 goto next_px;
1388 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1389 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1390 break;
1391 case ST_F_HRSP_3XX:
1392 if (px->mode != PR_MODE_HTTP)
1393 goto next_px;
1394 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1395 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1396 break;
1397 case ST_F_HRSP_4XX:
1398 if (px->mode != PR_MODE_HTTP)
1399 goto next_px;
1400 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1401 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1402 break;
1403 case ST_F_HRSP_5XX:
1404 if (px->mode != PR_MODE_HTTP)
1405 goto next_px;
1406 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1407 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1408 break;
1409 case ST_F_HRSP_OTHER:
1410 if (px->mode != PR_MODE_HTTP)
1411 goto next_px;
1412 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1413 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
1414 break;
1415 case ST_F_CACHE_LOOKUPS:
1416 if (px->mode != PR_MODE_HTTP)
1417 goto next_px;
1418 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1419 break;
1420 case ST_F_CACHE_HITS:
1421 if (px->mode != PR_MODE_HTTP)
1422 goto next_px;
1423 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
1424 break;
1425 case ST_F_COMP_IN:
1426 if (px->mode != PR_MODE_HTTP)
1427 goto next_px;
1428 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1429 break;
1430 case ST_F_COMP_OUT:
1431 if (px->mode != PR_MODE_HTTP)
1432 goto next_px;
1433 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1434 break;
1435 case ST_F_COMP_BYP:
1436 if (px->mode != PR_MODE_HTTP)
1437 goto next_px;
1438 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1439 break;
1440 case ST_F_COMP_RSP:
1441 if (px->mode != PR_MODE_HTTP)
1442 goto next_px;
1443 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1444 break;
1445
1446 default:
1447 goto next_metric;
1448 }
1449
1450 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1451 goto full;
1452 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001453 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001454 }
1455 next_metric:
1456 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001457 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +01001458 appctx->st2 = promex_back_metrics[appctx->st2];
1459 }
1460
1461 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001462 if (out.len) {
1463 if (!htx_add_data_atonce(htx, out))
1464 return -1; /* Unexpected and unrecoverable error */
1465 channel_add_input(chn, out.len);
1466 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001467 return ret;
1468 full:
1469 ret = 0;
1470 goto end;
1471}
1472
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001473/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001474 * 0 if <htx> is full and -1 in case of any error. */
1475static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
1476{
1477 static struct ist prefix = IST("haproxy_server_");
1478 struct proxy *px;
1479 struct server *sv;
1480 struct field metric;
1481 struct channel *chn = si_ic(appctx->owner);
1482 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001483 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001484 int ret = 1;
1485 uint32_t weight;
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001486 double secs;
Christopher Fauletf959d082019-02-07 15:38:42 +01001487
1488 while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001489 while (appctx->ctx.stats.obj1) {
1490 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001491
1492 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001493 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +01001494 goto next_px;
1495
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001496 while (appctx->ctx.stats.obj2) {
1497 sv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +01001498
Christopher Fauleteba22942019-11-19 14:18:24 +01001499 if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
1500 goto next_sv;
1501
Christopher Fauletf959d082019-02-07 15:38:42 +01001502 switch (appctx->st2) {
1503 case ST_F_STATUS:
1504 metric = mkf_u32(FO_STATUS, promex_srv_status(sv));
1505 break;
1506 case ST_F_SCUR:
1507 metric = mkf_u32(0, sv->cur_sess);
1508 break;
1509 case ST_F_SMAX:
1510 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1511 break;
1512 case ST_F_SLIM:
1513 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1514 break;
1515 case ST_F_STOT:
1516 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1517 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001518 case ST_F_RATE_MAX:
1519 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
1520 break;
1521 case ST_F_LASTSESS:
1522 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
1523 break;
1524 case ST_F_QCUR:
1525 metric = mkf_u32(0, sv->nbpend);
1526 break;
1527 case ST_F_QMAX:
1528 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1529 break;
1530 case ST_F_QLIMIT:
1531 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1532 break;
1533 case ST_F_BIN:
1534 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1535 break;
1536 case ST_F_BOUT:
1537 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1538 break;
1539 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001540 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
1541 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001542 break;
1543 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001544 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
1545 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001546 break;
1547 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001548 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
1549 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001550 break;
1551 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001552 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
1553 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001554 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001555 case ST_F_QT_MAX:
1556 secs = (double)sv->counters.qtime_max / 1000.0;
1557 metric = mkf_flt(FN_MAX, secs);
1558 break;
1559 case ST_F_CT_MAX:
1560 secs = (double)sv->counters.ctime_max / 1000.0;
1561 metric = mkf_flt(FN_MAX, secs);
1562 break;
1563 case ST_F_RT_MAX:
1564 secs = (double)sv->counters.dtime_max / 1000.0;
1565 metric = mkf_flt(FN_MAX, secs);
1566 break;
1567 case ST_F_TT_MAX:
1568 secs = (double)sv->counters.ttime_max / 1000.0;
1569 metric = mkf_flt(FN_MAX, secs);
1570 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001571 case ST_F_CONNECT:
1572 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
1573 break;
1574 case ST_F_REUSE:
1575 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
1576 break;
1577 case ST_F_DRESP:
Christopher Fauleta08546b2019-12-16 16:07:34 +01001578 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
Christopher Fauletf959d082019-02-07 15:38:42 +01001579 break;
1580 case ST_F_ECON:
1581 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1582 break;
1583 case ST_F_ERESP:
1584 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1585 break;
1586 case ST_F_WRETR:
1587 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
1588 break;
1589 case ST_F_WREDIS:
1590 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
1591 break;
1592 case ST_F_WREW:
1593 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
1594 break;
Christopher Faulete4a2c8d2019-12-16 14:44:01 +01001595 case ST_F_EINT:
1596 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
1597 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001598 case ST_F_CLI_ABRT:
1599 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1600 break;
1601 case ST_F_SRV_ABRT:
1602 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1603 break;
1604 case ST_F_WEIGHT:
1605 weight = (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
1606 metric = mkf_u32(FN_AVG, weight);
1607 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001608 case ST_F_CHECK_STATUS:
1609 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1610 goto next_sv;
1611 metric = mkf_u32(FN_OUTPUT, sv->check.status);
1612 break;
1613 case ST_F_CHECK_CODE:
1614 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1615 goto next_sv;
1616 metric = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
1617 break;
Christopher Faulet2711e512020-02-27 16:12:07 +01001618 case ST_F_CHECK_DURATION:
1619 if (sv->check.status < HCHK_STATUS_CHECKED)
1620 goto next_sv;
1621 secs = (double)sv->check.duration / 1000.0;
1622 metric = mkf_flt(FN_DURATION, secs);
1623 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001624 case ST_F_CHKFAIL:
1625 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1626 break;
1627 case ST_F_CHKDOWN:
1628 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1629 break;
1630 case ST_F_DOWNTIME:
1631 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
1632 break;
1633 case ST_F_LASTCHG:
1634 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
1635 break;
1636 case ST_F_THROTTLE:
1637 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
1638 break;
1639 case ST_F_LBTOT:
1640 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1641 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001642 case ST_F_REQ_TOT:
1643 if (px->mode != PR_MODE_HTTP)
1644 goto next_px;
1645 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
1646 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001647 case ST_F_HRSP_1XX:
1648 if (px->mode != PR_MODE_HTTP)
1649 goto next_px;
1650 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1651 break;
1652 case ST_F_HRSP_2XX:
1653 if (px->mode != PR_MODE_HTTP)
1654 goto next_px;
1655 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1656 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1657 break;
1658 case ST_F_HRSP_3XX:
1659 if (px->mode != PR_MODE_HTTP)
1660 goto next_px;
1661 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1662 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1663 break;
1664 case ST_F_HRSP_4XX:
1665 if (px->mode != PR_MODE_HTTP)
1666 goto next_px;
1667 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1668 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1669 break;
1670 case ST_F_HRSP_5XX:
1671 if (px->mode != PR_MODE_HTTP)
1672 goto next_px;
1673 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1674 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1675 break;
1676 case ST_F_HRSP_OTHER:
1677 if (px->mode != PR_MODE_HTTP)
1678 goto next_px;
1679 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1680 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1681 break;
Christopher Faulet20ab80c2019-11-08 15:24:32 +01001682 case ST_F_SRV_ICUR:
1683 metric = mkf_u32(0, sv->curr_idle_conns);
1684 break;
1685 case ST_F_SRV_ILIM:
1686 metric = mkf_u32(FO_CONFIG|FN_LIMIT, (sv->max_idle_conns == -1) ? 0 : sv->max_idle_conns);
1687 break;
Christopher Fauletc55a6262020-07-10 15:39:39 +02001688 case ST_F_IDLE_CONN_CUR:
1689 metric = mkf_u32(0, sv->curr_idle_nb);
1690 break;
1691 case ST_F_SAFE_CONN_CUR:
1692 metric = mkf_u32(0, sv->curr_safe_nb);
1693 break;
1694 case ST_F_USED_CONN_CUR:
1695 metric = mkf_u32(0, sv->curr_used_conns);
1696 break;
1697 case ST_F_NEED_CONN_EST:
1698 metric = mkf_u32(0, sv->est_need_conns);
1699 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001700
1701 default:
1702 goto next_metric;
1703 }
1704
1705 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1706 goto full;
1707
Christopher Fauleteba22942019-11-19 14:18:24 +01001708 next_sv:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001709 appctx->ctx.stats.obj2 = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001710 }
1711
1712 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001713 appctx->ctx.stats.obj1 = px->next;
1714 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001715 }
1716 next_metric:
1717 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001718 appctx->ctx.stats.obj1 = proxies_list;
1719 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001720 appctx->st2 = promex_srv_metrics[appctx->st2];
1721 }
1722
1723
1724 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001725 if (out.len) {
1726 if (!htx_add_data_atonce(htx, out))
1727 return -1; /* Unexpected and unrecoverable error */
1728 channel_add_input(chn, out.len);
1729 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001730 return ret;
1731 full:
1732 ret = 0;
1733 goto end;
1734}
1735
1736/* Dump all metrics (global, frontends, backends and servers) depending on the
1737 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001738 * -1 in case of any error.
1739 * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
1740 * a pointer to the current server/listener. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001741static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1742{
1743 int ret;
1744
1745 switch (appctx->st1) {
1746 case PROMEX_DUMPER_INIT:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001747 appctx->ctx.stats.obj1 = NULL;
1748 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001749 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001750 appctx->st2 = promex_global_metrics[INF_NAME];
1751 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1752 /* fall through */
1753
1754 case PROMEX_DUMPER_GLOBAL:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001755 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) {
1756 ret = promex_dump_global_metrics(appctx, htx);
1757 if (ret <= 0) {
1758 if (ret == -1)
1759 goto error;
1760 goto full;
1761 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001762 }
1763
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001764 appctx->ctx.stats.obj1 = proxies_list;
1765 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001766 appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001767 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001768 appctx->st2 = promex_front_metrics[ST_F_PXNAME];
1769 appctx->st1 = PROMEX_DUMPER_FRONT;
1770 /* fall through */
1771
1772 case PROMEX_DUMPER_FRONT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001773 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) {
1774 ret = promex_dump_front_metrics(appctx, htx);
1775 if (ret <= 0) {
1776 if (ret == -1)
1777 goto error;
1778 goto full;
1779 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001780 }
1781
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001782 appctx->ctx.stats.obj1 = proxies_list;
1783 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001784 appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC;
1785 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001786 appctx->st2 = promex_back_metrics[ST_F_PXNAME];
1787 appctx->st1 = PROMEX_DUMPER_BACK;
1788 /* fall through */
1789
1790 case PROMEX_DUMPER_BACK:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001791 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) {
1792 ret = promex_dump_back_metrics(appctx, htx);
1793 if (ret <= 0) {
1794 if (ret == -1)
1795 goto error;
1796 goto full;
1797 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001798 }
1799
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001800 appctx->ctx.stats.obj1 = proxies_list;
1801 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001802 appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC;
1803 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001804 appctx->st2 = promex_srv_metrics[ST_F_PXNAME];
1805 appctx->st1 = PROMEX_DUMPER_SRV;
1806 /* fall through */
1807
1808 case PROMEX_DUMPER_SRV:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001809 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) {
1810 ret = promex_dump_srv_metrics(appctx, htx);
1811 if (ret <= 0) {
1812 if (ret == -1)
1813 goto error;
1814 goto full;
1815 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001816 }
1817
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001818 appctx->ctx.stats.obj1 = NULL;
1819 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001820 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001821 appctx->st2 = 0;
1822 appctx->st1 = PROMEX_DUMPER_DONE;
1823 /* fall through */
1824
1825 case PROMEX_DUMPER_DONE:
1826 default:
1827 break;
1828 }
1829
1830 return 1;
1831
1832 full:
1833 si_rx_room_blk(si);
1834 return 0;
1835 error:
1836 /* unrecoverable error */
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001837 appctx->ctx.stats.obj1 = NULL;
1838 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletf959d082019-02-07 15:38:42 +01001839 appctx->ctx.stats.flags = 0;
1840 appctx->st2 = 0;
1841 appctx->st1 = PROMEX_DUMPER_DONE;
1842 return -1;
1843}
1844
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001845/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001846 * success and -1 on error. */
1847static int promex_parse_uri(struct appctx *appctx, struct stream_interface *si)
1848{
1849 struct channel *req = si_oc(si);
1850 struct channel *res = si_ic(si);
1851 struct htx *req_htx, *res_htx;
1852 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001853 char *p, *key, *value;
1854 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001855 struct buffer *err;
1856 int default_scopes = PROMEX_FL_SCOPE_ALL;
1857 int len;
1858
1859 /* Get the query-string */
1860 req_htx = htxbuf(&req->buf);
1861 sl = http_get_stline(req_htx);
1862 if (!sl)
1863 goto error;
1864 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1865 if (!p)
1866 goto end;
1867 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001868
William Dauchyc65f6562019-11-26 12:56:26 +01001869 /* copy the query-string */
1870 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001871 chunk_reset(&trash);
1872 memcpy(trash.area, p, len);
1873 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001874 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001875 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001876
1877 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001878 while (p < end && *p && *p != '#') {
1879 value = NULL;
1880
1881 /* decode parameter name */
1882 key = p;
1883 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001884 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001885 /* found a value */
1886 if (*p == '=') {
1887 *(p++) = 0;
1888 value = p;
1889 }
1890 else if (*p == '&')
1891 *(p++) = 0;
1892 else if (*p == '#')
1893 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001894 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001895 if (len == -1)
1896 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001897
William Dauchyc65f6562019-11-26 12:56:26 +01001898 /* decode value */
1899 if (value) {
1900 while (p < end && *p != '=' && *p != '&' && *p != '#')
1901 ++p;
1902 if (*p == '=')
1903 goto error;
1904 if (*p == '&')
1905 *(p++) = 0;
1906 else if (*p == '#')
1907 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001908 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001909 if (len == -1)
1910 goto error;
1911 }
1912
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001913 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001914 default_scopes = 0; /* at least a scope defined, unset default scopes */
1915 if (!value)
1916 goto error;
1917 else if (*value == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001918 appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001919 else if (*value == '*')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001920 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001921 else if (strcmp(value, "global") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001922 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001923 else if (strcmp(value, "server") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001924 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001925 else if (strcmp(value, "backend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001926 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001927 else if (strcmp(value, "frontend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001928 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT;
1929 else
1930 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001931 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001932 else if (strcmp(key, "no-maint") == 0)
Christopher Fauleteba22942019-11-19 14:18:24 +01001933 appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001934 }
1935
1936 end:
1937 appctx->ctx.stats.flags |= default_scopes;
1938 return 1;
1939
1940 error:
1941 err = &http_err_chunks[HTTP_ERR_400];
1942 channel_erase(res);
1943 res->buf.data = b_data(err);
1944 memcpy(res->buf.area, b_head(err), b_data(err));
1945 res_htx = htx_from_buf(&res->buf);
1946 channel_add_input(res, res_htx->data);
1947 appctx->st0 = PROMEX_ST_END;
1948 return -1;
1949}
1950
Christopher Fauletf959d082019-02-07 15:38:42 +01001951/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1952 * full. */
1953static int promex_send_headers(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1954{
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001955 struct channel *chn = si_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +01001956 struct htx_sl *sl;
1957 unsigned int flags;
1958
1959 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);
1960 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1961 if (!sl)
1962 goto full;
1963 sl->info.res.status = 200;
1964 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001965 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1966 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1967 !htx_add_endof(htx, HTX_BLK_EOH))
1968 goto full;
1969
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001970 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001971 return 1;
1972 full:
1973 htx_reset(htx);
1974 si_rx_room_blk(si);
1975 return 0;
1976}
1977
1978/* The function returns 1 if the initialisation is complete, 0 if
1979 * an errors occurs and -1 if more data are required for initializing
1980 * the applet.
1981 */
1982static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm)
1983{
1984 appctx->st0 = PROMEX_ST_INIT;
1985 return 1;
1986}
1987
1988/* The main I/O handler for the promex applet. */
1989static void promex_appctx_handle_io(struct appctx *appctx)
1990{
1991 struct stream_interface *si = appctx->owner;
1992 struct stream *s = si_strm(si);
1993 struct channel *req = si_oc(si);
1994 struct channel *res = si_ic(si);
1995 struct htx *req_htx, *res_htx;
1996 int ret;
1997
1998 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf959d082019-02-07 15:38:42 +01001999 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2000 goto out;
2001
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05002002 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01002003 if (!b_size(&res->buf)) {
2004 si_rx_room_blk(si);
2005 goto out;
2006 }
2007
2008 switch (appctx->st0) {
2009 case PROMEX_ST_INIT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01002010 ret = promex_parse_uri(appctx, si);
2011 if (ret <= 0) {
2012 if (ret == -1)
2013 goto error;
2014 goto out;
2015 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002016 appctx->st0 = PROMEX_ST_HEAD;
2017 appctx->st1 = PROMEX_DUMPER_INIT;
2018 /* fall through */
2019
2020 case PROMEX_ST_HEAD:
2021 if (!promex_send_headers(appctx, si, res_htx))
2022 goto out;
2023 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
2024 /* fall through */
2025
2026 case PROMEX_ST_DUMP:
2027 ret = promex_dump_metrics(appctx, si, res_htx);
2028 if (ret <= 0) {
2029 if (ret == -1)
2030 goto error;
2031 goto out;
2032 }
2033 appctx->st0 = PROMEX_ST_DONE;
2034 /* fall through */
2035
2036 case PROMEX_ST_DONE:
Christopher Faulet54b5e212019-06-04 10:08:28 +02002037 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02002038 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletf959d082019-02-07 15:38:42 +01002039 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
2040 si_rx_room_blk(si);
2041 goto out;
2042 }
Christopher Faulet9744f7c2019-03-27 15:48:53 +01002043 channel_add_input(res, 1);
2044 appctx->st0 = PROMEX_ST_END;
2045 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01002046
Christopher Faulet9744f7c2019-03-27 15:48:53 +01002047 case PROMEX_ST_END:
2048 if (!(res->flags & CF_SHUTR)) {
2049 res->flags |= CF_READ_NULL;
2050 si_shutr(si);
2051 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002052 }
2053
Christopher Fauletf959d082019-02-07 15:38:42 +01002054 out:
2055 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01002056
2057 /* eat the whole request */
2058 if (co_data(req)) {
2059 req_htx = htx_from_buf(&req->buf);
2060 co_htx_skip(req, req_htx, co_data(req));
2061 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002062 return;
2063
2064 error:
2065 res->flags |= CF_READ_NULL;
2066 si_shutr(si);
2067 si_shutw(si);
2068}
2069
2070struct applet promex_applet = {
2071 .obj_type = OBJ_TYPE_APPLET,
2072 .name = "<PROMEX>", /* used for logging */
2073 .init = promex_appctx_init,
2074 .fct = promex_appctx_handle_io,
2075};
2076
2077static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
2078 struct act_rule *rule, char **err)
2079{
2080 /* Prometheus exporter service is only available on "http-request" rulesets */
2081 if (rule->from != ACT_F_HTTP_REQ) {
2082 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
2083 return ACT_RET_PRS_ERR;
2084 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002085
2086 /* Add applet pointer in the rule. */
2087 rule->applet = promex_applet;
2088
2089 return ACT_RET_PRS_OK;
2090}
2091static void promex_register_build_options(void)
2092{
2093 char *ptr = NULL;
2094
2095 memprintf(&ptr, "Built with the Prometheus exporter as a service");
2096 hap_register_build_opts(ptr, 1);
2097}
2098
2099
2100static struct action_kw_list service_actions = { ILH, {
2101 { "prometheus-exporter", service_parse_prometheus_exporter },
2102 { /* END */ }
2103}};
2104
2105INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
2106INITCALL0(STG_REGISTER, promex_register_build_options);