blob: 81f5ba0dbc01ad6a25a41823ac1a474de1b4c095 [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
66#define PROMEX_FL_STATS_METRIC 0x00000004
Christopher Faulet78407ce2019-11-18 14:47:08 +010067#define PROMEX_FL_SCOPE_GLOBAL 0x00000008
68#define PROMEX_FL_SCOPE_FRONT 0x00000010
69#define PROMEX_FL_SCOPE_BACK 0x00000020
70#define PROMEX_FL_SCOPE_SERVER 0x00000040
Christopher Fauleteba22942019-11-19 14:18:24 +010071#define PROMEX_FL_NO_MAINT_SRV 0x00000080
Christopher Faulet78407ce2019-11-18 14:47:08 +010072
73#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 +010074
75/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +050076 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +010077 */
78#define PROMEX_MAX_NAME_LEN 128
79
80/* The expected max length for a metric dump, including its header lines. It is
81 * just a soft limit to avoid extra work. We don't try to dump a metric if less
82 * than this size is available in the HTX.
83 */
84#define PROMEX_MAX_METRIC_LENGTH 512
85
William Dauchy5a982a72021-01-08 13:18:06 +010086/* Some labels for build_info */
87#define PROMEX_VERSION_LABEL "version=\"" HAPROXY_VERSION "\""
88#define PROMEX_BUILDINFO_LABEL PROMEX_VERSION_LABEL
89
Christopher Fauletf959d082019-02-07 15:38:42 +010090/* Matrix used to dump global metrics. Each metric points to the next one to be
91 * processed or 0 to stop the dump. */
92const int promex_global_metrics[INF_TOTAL_FIELDS] = {
William Dauchy5a982a72021-01-08 13:18:06 +010093 [INF_NAME] = INF_BUILD_INFO,
Christopher Fauletf959d082019-02-07 15:38:42 +010094 [INF_VERSION] = 0,
95 [INF_RELEASE_DATE] = 0,
William Dauchy5a982a72021-01-08 13:18:06 +010096 [INF_BUILD_INFO] = INF_NBTHREAD,
Christopher Fauletf959d082019-02-07 15:38:42 +010097 [INF_NBTHREAD] = INF_NBPROC,
98 [INF_NBPROC] = INF_PROCESS_NUM,
99 [INF_PROCESS_NUM] = INF_UPTIME_SEC,
100 [INF_PID] = 0,
101 [INF_UPTIME] = 0,
William Dauchydefd1562021-01-15 22:41:38 +0100102 [INF_UPTIME_SEC] = INF_START_TIME_SEC,
103 [INF_START_TIME_SEC] = INF_MEMMAX_BYTES,
William Dauchya8766cf2021-01-15 22:41:37 +0100104 [INF_MEMMAX_BYTES] = INF_POOL_ALLOC_BYTES,
105 [INF_POOL_ALLOC_BYTES] = INF_POOL_USED_BYTES,
106 [INF_POOL_USED_BYTES] = INF_POOL_FAILED,
Christopher Fauletf959d082019-02-07 15:38:42 +0100107 [INF_POOL_FAILED] = INF_ULIMIT_N,
108 [INF_ULIMIT_N] = INF_MAXSOCK,
109 [INF_MAXSOCK] = INF_MAXCONN,
110 [INF_MAXCONN] = INF_HARD_MAXCONN,
111 [INF_HARD_MAXCONN] = INF_CURR_CONN,
112 [INF_CURR_CONN] = INF_CUM_CONN,
113 [INF_CUM_CONN] = INF_CUM_REQ,
114 [INF_CUM_REQ] = INF_MAX_SSL_CONNS,
115 [INF_MAX_SSL_CONNS] = INF_CURR_SSL_CONNS,
116 [INF_CURR_SSL_CONNS] = INF_CUM_SSL_CONNS,
117 [INF_CUM_SSL_CONNS] = INF_MAXPIPES,
118 [INF_MAXPIPES] = INF_PIPES_USED,
119 [INF_PIPES_USED] = INF_PIPES_FREE,
120 [INF_PIPES_FREE] = INF_CONN_RATE,
121 [INF_CONN_RATE] = INF_CONN_RATE_LIMIT,
122 [INF_CONN_RATE_LIMIT] = INF_MAX_CONN_RATE,
123 [INF_MAX_CONN_RATE] = INF_SESS_RATE,
124 [INF_SESS_RATE] = INF_SESS_RATE_LIMIT,
125 [INF_SESS_RATE_LIMIT] = INF_MAX_SESS_RATE,
126 [INF_MAX_SESS_RATE] = INF_SSL_RATE,
127 [INF_SSL_RATE] = INF_SSL_RATE_LIMIT,
128 [INF_SSL_RATE_LIMIT] = INF_MAX_SSL_RATE,
129 [INF_MAX_SSL_RATE] = INF_SSL_FRONTEND_KEY_RATE,
130 [INF_SSL_FRONTEND_KEY_RATE] = INF_SSL_FRONTEND_MAX_KEY_RATE,
131 [INF_SSL_FRONTEND_MAX_KEY_RATE] = INF_SSL_FRONTEND_SESSION_REUSE_PCT,
132 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = INF_SSL_BACKEND_KEY_RATE,
133 [INF_SSL_BACKEND_KEY_RATE] = INF_SSL_BACKEND_MAX_KEY_RATE,
134 [INF_SSL_BACKEND_MAX_KEY_RATE] = INF_SSL_CACHE_LOOKUPS,
135 [INF_SSL_CACHE_LOOKUPS] = INF_SSL_CACHE_MISSES,
136 [INF_SSL_CACHE_MISSES] = INF_COMPRESS_BPS_IN,
137 [INF_COMPRESS_BPS_IN] = INF_COMPRESS_BPS_OUT,
138 [INF_COMPRESS_BPS_OUT] = INF_COMPRESS_BPS_RATE_LIM,
139 [INF_COMPRESS_BPS_RATE_LIM] = INF_ZLIB_MEM_USAGE,
140 [INF_ZLIB_MEM_USAGE] = INF_MAX_ZLIB_MEM_USAGE,
141 [INF_MAX_ZLIB_MEM_USAGE] = INF_TASKS,
142 [INF_TASKS] = INF_RUN_QUEUE,
143 [INF_RUN_QUEUE] = INF_IDLE_PCT,
144 [INF_IDLE_PCT] = INF_STOPPING,
145 [INF_NODE] = 0,
146 [INF_DESCRIPTION] = 0,
147 [INF_STOPPING] = INF_JOBS,
148 [INF_JOBS] = INF_UNSTOPPABLE_JOBS,
149 [INF_UNSTOPPABLE_JOBS] = INF_LISTENERS,
150 [INF_LISTENERS] = INF_ACTIVE_PEERS,
151 [INF_ACTIVE_PEERS] = INF_CONNECTED_PEERS,
152 [INF_CONNECTED_PEERS] = INF_DROPPED_LOGS,
153 [INF_DROPPED_LOGS] = INF_BUSY_POLLING,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200154 [INF_BUSY_POLLING] = INF_FAILED_RESOLUTIONS,
155 [INF_FAILED_RESOLUTIONS] = INF_TOTAL_BYTES_OUT,
156 [INF_TOTAL_BYTES_OUT] = INF_TOTAL_SPLICED_BYTES_OUT,
157 [INF_TOTAL_SPLICED_BYTES_OUT] = INF_BYTES_OUT_RATE,
158 [INF_BYTES_OUT_RATE] = 0,
159 [INF_DEBUG_COMMANDS_ISSUED] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100160};
161
162/* Matrix used to dump frontend metrics. Each metric points to the next one to be
163 * processed or 0 to stop the dump. */
164const int promex_front_metrics[ST_F_TOTAL_FIELDS] = {
165 [ST_F_PXNAME] = ST_F_STATUS,
166 [ST_F_SVNAME] = 0,
167 [ST_F_QCUR] = 0,
168 [ST_F_QMAX] = 0,
169 [ST_F_SCUR] = ST_F_SMAX,
170 [ST_F_SMAX] = ST_F_SLIM,
171 [ST_F_SLIM] = ST_F_STOT,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200172 [ST_F_STOT] = ST_F_RATE_LIM,
Christopher Fauletf959d082019-02-07 15:38:42 +0100173 [ST_F_BIN] = ST_F_BOUT,
174 [ST_F_BOUT] = ST_F_DREQ,
175 [ST_F_DREQ] = ST_F_DRESP,
176 [ST_F_DRESP] = ST_F_EREQ,
177 [ST_F_EREQ] = ST_F_DCON,
178 [ST_F_ECON] = 0,
179 [ST_F_ERESP] = 0,
180 [ST_F_WRETR] = 0,
181 [ST_F_WREDIS] = 0,
182 [ST_F_STATUS] = ST_F_SCUR,
183 [ST_F_WEIGHT] = 0,
184 [ST_F_ACT] = 0,
185 [ST_F_BCK] = 0,
186 [ST_F_CHKFAIL] = 0,
187 [ST_F_CHKDOWN] = 0,
188 [ST_F_LASTCHG] = 0,
189 [ST_F_DOWNTIME] = 0,
190 [ST_F_QLIMIT] = 0,
191 [ST_F_PID] = 0,
192 [ST_F_IID] = 0,
193 [ST_F_SID] = 0,
194 [ST_F_THROTTLE] = 0,
195 [ST_F_LBTOT] = 0,
196 [ST_F_TRACKED] = 0,
197 [ST_F_TYPE] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200198 [ST_F_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100199 [ST_F_RATE_LIM] = ST_F_RATE_MAX,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200200 [ST_F_RATE_MAX] = ST_F_CONN_RATE_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100201 [ST_F_CHECK_STATUS] = 0,
202 [ST_F_CHECK_CODE] = 0,
203 [ST_F_CHECK_DURATION] = 0,
204 [ST_F_HRSP_1XX] = ST_F_HRSP_2XX,
205 [ST_F_HRSP_2XX] = ST_F_HRSP_3XX,
206 [ST_F_HRSP_3XX] = ST_F_HRSP_4XX,
207 [ST_F_HRSP_4XX] = ST_F_HRSP_5XX,
208 [ST_F_HRSP_5XX] = ST_F_HRSP_OTHER,
209 [ST_F_HRSP_OTHER] = ST_F_INTERCEPTED,
210 [ST_F_HANAFAIL] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200211 [ST_F_REQ_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100212 [ST_F_REQ_RATE_MAX] = ST_F_REQ_TOT,
213 [ST_F_REQ_TOT] = ST_F_HRSP_1XX,
214 [ST_F_CLI_ABRT] = 0,
215 [ST_F_SRV_ABRT] = 0,
216 [ST_F_COMP_IN] = ST_F_COMP_OUT,
217 [ST_F_COMP_OUT] = ST_F_COMP_BYP,
218 [ST_F_COMP_BYP] = ST_F_COMP_RSP,
219 [ST_F_COMP_RSP] = 0,
220 [ST_F_LASTSESS] = 0,
221 [ST_F_LAST_CHK] = 0,
222 [ST_F_LAST_AGT] = 0,
223 [ST_F_QTIME] = 0,
224 [ST_F_CTIME] = 0,
225 [ST_F_RTIME] = 0,
226 [ST_F_TTIME] = 0,
227 [ST_F_AGENT_STATUS] = 0,
228 [ST_F_AGENT_CODE] = 0,
229 [ST_F_AGENT_DURATION] = 0,
230 [ST_F_CHECK_DESC] = 0,
231 [ST_F_AGENT_DESC] = 0,
232 [ST_F_CHECK_RISE] = 0,
233 [ST_F_CHECK_FALL] = 0,
234 [ST_F_CHECK_HEALTH] = 0,
235 [ST_F_AGENT_RISE] = 0,
236 [ST_F_AGENT_FALL] = 0,
237 [ST_F_AGENT_HEALTH] = 0,
238 [ST_F_ADDR] = 0,
239 [ST_F_COOKIE] = 0,
240 [ST_F_MODE] = 0,
241 [ST_F_ALGO] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200242 [ST_F_CONN_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100243 [ST_F_CONN_RATE_MAX] = ST_F_CONN_TOT,
244 [ST_F_CONN_TOT] = ST_F_BIN,
245 [ST_F_INTERCEPTED] = ST_F_CACHE_LOOKUPS,
246 [ST_F_DCON] = ST_F_DSES,
247 [ST_F_DSES] = ST_F_WREW,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100248 [ST_F_WREW] = ST_F_EINT,
Christopher Fauletf959d082019-02-07 15:38:42 +0100249 [ST_F_CONNECT] = 0,
250 [ST_F_REUSE] = 0,
251 [ST_F_CACHE_LOOKUPS] = ST_F_CACHE_HITS,
252 [ST_F_CACHE_HITS] = ST_F_COMP_IN,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100253 [ST_F_SRV_ICUR] = 0,
254 [ST_F_SRV_ILIM] = 0,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100255 [ST_F_QT_MAX] = 0,
256 [ST_F_CT_MAX] = 0,
257 [ST_F_RT_MAX] = 0,
258 [ST_F_TT_MAX] = 0,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100259 [ST_F_EINT] = ST_F_REQ_RATE_MAX,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200260 [ST_F_IDLE_CONN_CUR] = 0,
261 [ST_F_SAFE_CONN_CUR] = 0,
262 [ST_F_USED_CONN_CUR] = 0,
263 [ST_F_NEED_CONN_EST] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100264};
265
266/* Matrix used to dump backend metrics. Each metric points to the next one to be
267 * processed or 0 to stop the dump. */
268const int promex_back_metrics[ST_F_TOTAL_FIELDS] = {
269 [ST_F_PXNAME] = ST_F_STATUS,
270 [ST_F_SVNAME] = 0,
271 [ST_F_QCUR] = ST_F_QMAX,
272 [ST_F_QMAX] = ST_F_CONNECT,
273 [ST_F_SCUR] = ST_F_SMAX,
274 [ST_F_SMAX] = ST_F_SLIM,
275 [ST_F_SLIM] = ST_F_STOT,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200276 [ST_F_STOT] = ST_F_RATE_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100277 [ST_F_BIN] = ST_F_BOUT,
278 [ST_F_BOUT] = ST_F_QTIME,
279 [ST_F_DREQ] = ST_F_DRESP,
280 [ST_F_DRESP] = ST_F_ECON,
281 [ST_F_EREQ] = 0,
282 [ST_F_ECON] = ST_F_ERESP,
283 [ST_F_ERESP] = ST_F_WRETR,
284 [ST_F_WRETR] = ST_F_WREDIS,
285 [ST_F_WREDIS] = ST_F_WREW,
286 [ST_F_STATUS] = ST_F_SCUR,
287 [ST_F_WEIGHT] = ST_F_ACT,
288 [ST_F_ACT] = ST_F_BCK,
289 [ST_F_BCK] = ST_F_CHKDOWN,
290 [ST_F_CHKFAIL] = 0,
291 [ST_F_CHKDOWN] = ST_F_LASTCHG,
292 [ST_F_LASTCHG] = ST_F_DOWNTIME,
293 [ST_F_DOWNTIME] = ST_F_LBTOT,
294 [ST_F_QLIMIT] = 0,
295 [ST_F_PID] = 0,
296 [ST_F_IID] = 0,
297 [ST_F_SID] = 0,
298 [ST_F_THROTTLE] = 0,
299 [ST_F_LBTOT] = ST_F_REQ_TOT,
300 [ST_F_TRACKED] = 9,
301 [ST_F_TYPE] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200302 [ST_F_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100303 [ST_F_RATE_LIM] = 0,
304 [ST_F_RATE_MAX] = ST_F_LASTSESS,
305 [ST_F_CHECK_STATUS] = 0,
306 [ST_F_CHECK_CODE] = 0,
307 [ST_F_CHECK_DURATION] = 0,
308 [ST_F_HRSP_1XX] = ST_F_HRSP_2XX,
309 [ST_F_HRSP_2XX] = ST_F_HRSP_3XX,
310 [ST_F_HRSP_3XX] = ST_F_HRSP_4XX,
311 [ST_F_HRSP_4XX] = ST_F_HRSP_5XX,
312 [ST_F_HRSP_5XX] = ST_F_HRSP_OTHER,
313 [ST_F_HRSP_OTHER] = ST_F_CACHE_LOOKUPS,
314 [ST_F_HANAFAIL] = 0,
315 [ST_F_REQ_RATE] = 0,
316 [ST_F_REQ_RATE_MAX] = 0,
317 [ST_F_REQ_TOT] = ST_F_HRSP_1XX,
318 [ST_F_CLI_ABRT] = ST_F_SRV_ABRT,
319 [ST_F_SRV_ABRT] = ST_F_WEIGHT,
320 [ST_F_COMP_IN] = ST_F_COMP_OUT,
321 [ST_F_COMP_OUT] = ST_F_COMP_BYP,
322 [ST_F_COMP_BYP] = ST_F_COMP_RSP,
323 [ST_F_COMP_RSP] = 0,
324 [ST_F_LASTSESS] = ST_F_QCUR,
325 [ST_F_LAST_CHK] = 0,
326 [ST_F_LAST_AGT] = 0,
327 [ST_F_QTIME] = ST_F_CTIME,
328 [ST_F_CTIME] = ST_F_RTIME,
329 [ST_F_RTIME] = ST_F_TTIME,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100330 [ST_F_TTIME] = ST_F_QT_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100331 [ST_F_AGENT_STATUS] = 0,
332 [ST_F_AGENT_CODE] = 0,
333 [ST_F_AGENT_DURATION] = 0,
334 [ST_F_CHECK_DESC] = 0,
335 [ST_F_AGENT_DESC] = 0,
336 [ST_F_CHECK_RISE] = 0,
337 [ST_F_CHECK_FALL] = 0,
338 [ST_F_CHECK_HEALTH] = 0,
339 [ST_F_AGENT_RISE] = 0,
340 [ST_F_AGENT_FALL] = 0,
341 [ST_F_AGENT_HEALTH] = 0,
342 [ST_F_ADDR] = 0,
343 [ST_F_COOKIE] = 0,
344 [ST_F_MODE] = 0,
345 [ST_F_ALGO] = 0,
346 [ST_F_CONN_RATE] = 0,
347 [ST_F_CONN_RATE_MAX] = 0,
348 [ST_F_CONN_TOT] = 0,
349 [ST_F_INTERCEPTED] = 0,
350 [ST_F_DCON] = 0,
351 [ST_F_DSES] = 0,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100352 [ST_F_WREW] = ST_F_EINT,
Christopher Fauletf959d082019-02-07 15:38:42 +0100353 [ST_F_CONNECT] = ST_F_REUSE,
354 [ST_F_REUSE] = ST_F_BIN,
355 [ST_F_CACHE_LOOKUPS] = ST_F_CACHE_HITS,
356 [ST_F_CACHE_HITS] = ST_F_COMP_IN,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100357 [ST_F_SRV_ICUR] = 0,
358 [ST_F_SRV_ILIM] = 0,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100359 [ST_F_QT_MAX] = ST_F_CT_MAX,
360 [ST_F_CT_MAX] = ST_F_RT_MAX,
361 [ST_F_RT_MAX] = ST_F_TT_MAX,
362 [ST_F_TT_MAX] = ST_F_DREQ,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100363 [ST_F_EINT] = ST_F_CLI_ABRT,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200364 [ST_F_IDLE_CONN_CUR] = 0,
365 [ST_F_SAFE_CONN_CUR] = 0,
366 [ST_F_USED_CONN_CUR] = 0,
367 [ST_F_NEED_CONN_EST] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100368};
369
370/* Matrix used to dump server metrics. Each metric points to the next one to be
371 * processed or 0 to stop the dump. */
372const int promex_srv_metrics[ST_F_TOTAL_FIELDS] = {
373 [ST_F_PXNAME] = ST_F_STATUS,
374 [ST_F_SVNAME] = 0,
375 [ST_F_QCUR] = ST_F_QMAX,
376 [ST_F_QMAX] = ST_F_QLIMIT,
377 [ST_F_SCUR] = ST_F_SMAX,
378 [ST_F_SMAX] = ST_F_SLIM,
379 [ST_F_SLIM] = ST_F_STOT,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200380 [ST_F_STOT] = ST_F_RATE_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100381 [ST_F_BIN] = ST_F_BOUT,
382 [ST_F_BOUT] = ST_F_QTIME,
383 [ST_F_DREQ] = 0,
384 [ST_F_DRESP] = ST_F_ECON,
385 [ST_F_EREQ] = 0,
386 [ST_F_ECON] = ST_F_ERESP,
387 [ST_F_ERESP] = ST_F_WRETR,
388 [ST_F_WRETR] = ST_F_WREDIS,
389 [ST_F_WREDIS] = ST_F_WREW,
390 [ST_F_STATUS] = ST_F_SCUR,
Christopher Fauletcf403f32019-11-21 14:35:46 +0100391 [ST_F_WEIGHT] = ST_F_CHECK_STATUS,
Christopher Fauletf959d082019-02-07 15:38:42 +0100392 [ST_F_ACT] = 0,
393 [ST_F_BCK] = 0,
394 [ST_F_CHKFAIL] = ST_F_CHKDOWN,
395 [ST_F_CHKDOWN] = ST_F_DOWNTIME,
396 [ST_F_LASTCHG] = ST_F_THROTTLE,
397 [ST_F_DOWNTIME] = ST_F_LASTCHG,
398 [ST_F_QLIMIT] = ST_F_BIN,
399 [ST_F_PID] = 0,
400 [ST_F_IID] = 0,
401 [ST_F_SID] = 0,
402 [ST_F_THROTTLE] = ST_F_LBTOT,
403 [ST_F_LBTOT] = ST_F_HRSP_1XX,
404 [ST_F_TRACKED] = 0,
405 [ST_F_TYPE] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200406 [ST_F_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100407 [ST_F_RATE_LIM] = 0,
408 [ST_F_RATE_MAX] = ST_F_LASTSESS,
Christopher Fauletcf403f32019-11-21 14:35:46 +0100409 [ST_F_CHECK_STATUS] = ST_F_CHECK_CODE,
Christopher Faulet2711e512020-02-27 16:12:07 +0100410 [ST_F_CHECK_CODE] = ST_F_CHECK_DURATION,
411 [ST_F_CHECK_DURATION] = ST_F_CHKFAIL,
Christopher Fauletf959d082019-02-07 15:38:42 +0100412 [ST_F_HRSP_1XX] = ST_F_HRSP_2XX,
413 [ST_F_HRSP_2XX] = ST_F_HRSP_3XX,
414 [ST_F_HRSP_3XX] = ST_F_HRSP_4XX,
415 [ST_F_HRSP_4XX] = ST_F_HRSP_5XX,
416 [ST_F_HRSP_5XX] = ST_F_HRSP_OTHER,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100417 [ST_F_HRSP_OTHER] = ST_F_SRV_ICUR,
Christopher Fauletf959d082019-02-07 15:38:42 +0100418 [ST_F_HANAFAIL] = 0,
419 [ST_F_REQ_RATE] = 0,
420 [ST_F_REQ_RATE_MAX] = 0,
421 [ST_F_REQ_TOT] = 0,
422 [ST_F_CLI_ABRT] = ST_F_SRV_ABRT,
423 [ST_F_SRV_ABRT] = ST_F_WEIGHT,
424 [ST_F_COMP_IN] = 0,
425 [ST_F_COMP_OUT] = 0,
426 [ST_F_COMP_BYP] = 0,
427 [ST_F_COMP_RSP] = 0,
428 [ST_F_LASTSESS] = ST_F_QCUR,
429 [ST_F_LAST_CHK] = 0,
430 [ST_F_LAST_AGT] = 0,
431 [ST_F_QTIME] = ST_F_CTIME,
432 [ST_F_CTIME] = ST_F_RTIME,
433 [ST_F_RTIME] = ST_F_TTIME,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100434 [ST_F_TTIME] = ST_F_QT_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100435 [ST_F_AGENT_STATUS] = 0,
436 [ST_F_AGENT_CODE] = 0,
437 [ST_F_AGENT_DURATION] = 0,
438 [ST_F_CHECK_DESC] = 0,
439 [ST_F_AGENT_DESC] = 0,
440 [ST_F_CHECK_RISE] = 0,
441 [ST_F_CHECK_FALL] = 0,
442 [ST_F_CHECK_HEALTH] = 0,
443 [ST_F_AGENT_RISE] = 0,
444 [ST_F_AGENT_FALL] = 0,
445 [ST_F_AGENT_HEALTH] = 0,
446 [ST_F_ADDR] = 0,
447 [ST_F_COOKIE] = 0,
448 [ST_F_MODE] = 0,
449 [ST_F_ALGO] = 0,
450 [ST_F_CONN_RATE] = 0,
451 [ST_F_CONN_RATE_MAX] = 0,
452 [ST_F_CONN_TOT] = 0,
453 [ST_F_INTERCEPTED] = 0,
454 [ST_F_DCON] = 0,
455 [ST_F_DSES] = 0,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100456 [ST_F_WREW] = ST_F_EINT,
Christopher Fauletf959d082019-02-07 15:38:42 +0100457 [ST_F_CONNECT] = ST_F_REUSE,
458 [ST_F_REUSE] = ST_F_DRESP,
459 [ST_F_CACHE_LOOKUPS] = 0,
460 [ST_F_CACHE_HITS] = 0,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100461 [ST_F_SRV_ICUR] = ST_F_SRV_ILIM,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200462 [ST_F_SRV_ILIM] = ST_F_IDLE_CONN_CUR,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100463 [ST_F_QT_MAX] = ST_F_CT_MAX,
464 [ST_F_CT_MAX] = ST_F_RT_MAX,
465 [ST_F_RT_MAX] = ST_F_TT_MAX,
466 [ST_F_TT_MAX] = ST_F_CONNECT,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100467 [ST_F_EINT] = ST_F_CLI_ABRT,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200468 [ST_F_IDLE_CONN_CUR] = ST_F_SAFE_CONN_CUR,
469 [ST_F_SAFE_CONN_CUR] = ST_F_USED_CONN_CUR,
470 [ST_F_USED_CONN_CUR] = ST_F_NEED_CONN_EST,
471 [ST_F_NEED_CONN_EST] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100472};
473
474/* Name of all info fields */
475const struct ist promex_inf_metric_names[INF_TOTAL_FIELDS] = {
476 [INF_NAME] = IST("name"),
477 [INF_VERSION] = IST("version"),
478 [INF_RELEASE_DATE] = IST("release_date"),
William Dauchy5a982a72021-01-08 13:18:06 +0100479 [INF_BUILD_INFO] = IST("build_info"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100480 [INF_NBTHREAD] = IST("nbthread"),
481 [INF_NBPROC] = IST("nbproc"),
482 [INF_PROCESS_NUM] = IST("relative_process_id"),
483 [INF_PID] = IST("pid"),
484 [INF_UPTIME] = IST("uptime"),
William Dauchydefd1562021-01-15 22:41:38 +0100485 [INF_UPTIME_SEC] = IST("uptime_seconds"),
486 [INF_START_TIME_SEC] = IST("start_time_seconds"),
William Dauchya8766cf2021-01-15 22:41:37 +0100487 [INF_MEMMAX_BYTES] = IST("max_memory_bytes"),
488 [INF_POOL_ALLOC_BYTES] = IST("pool_allocated_bytes"),
489 [INF_POOL_USED_BYTES] = IST("pool_used_bytes"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100490 [INF_POOL_FAILED] = IST("pool_failures_total"),
491 [INF_ULIMIT_N] = IST("max_fds"),
492 [INF_MAXSOCK] = IST("max_sockets"),
493 [INF_MAXCONN] = IST("max_connections"),
494 [INF_HARD_MAXCONN] = IST("hard_max_connections"),
495 [INF_CURR_CONN] = IST("current_connections"),
496 [INF_CUM_CONN] = IST("connections_total"),
497 [INF_CUM_REQ] = IST("requests_total"),
498 [INF_MAX_SSL_CONNS] = IST("max_ssl_connections"),
499 [INF_CURR_SSL_CONNS] = IST("current_ssl_connections"),
500 [INF_CUM_SSL_CONNS] = IST("ssl_connections_total"),
501 [INF_MAXPIPES] = IST("max_pipes"),
502 [INF_PIPES_USED] = IST("pipes_used_total"),
503 [INF_PIPES_FREE] = IST("pipes_free_total"),
504 [INF_CONN_RATE] = IST("current_connection_rate"),
505 [INF_CONN_RATE_LIMIT] = IST("limit_connection_rate"),
506 [INF_MAX_CONN_RATE] = IST("max_connection_rate"),
507 [INF_SESS_RATE] = IST("current_session_rate"),
508 [INF_SESS_RATE_LIMIT] = IST("limit_session_rate"),
509 [INF_MAX_SESS_RATE] = IST("max_session_rate"),
510 [INF_SSL_RATE] = IST("current_ssl_rate"),
511 [INF_SSL_RATE_LIMIT] = IST("limit_ssl_rate"),
512 [INF_MAX_SSL_RATE] = IST("max_ssl_rate"),
513 [INF_SSL_FRONTEND_KEY_RATE] = IST("current_frontend_ssl_key_rate"),
514 [INF_SSL_FRONTEND_MAX_KEY_RATE] = IST("max_frontend_ssl_key_rate"),
Pierre Cheynier1e369762020-07-07 19:14:08 +0200515 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = IST("frontend_ssl_reuse"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100516 [INF_SSL_BACKEND_KEY_RATE] = IST("current_backend_ssl_key_rate"),
517 [INF_SSL_BACKEND_MAX_KEY_RATE] = IST("max_backend_ssl_key_rate"),
Christopher Faulet8c8e4b12019-04-18 10:15:15 +0200518 [INF_SSL_CACHE_LOOKUPS] = IST("ssl_cache_lookups_total"),
519 [INF_SSL_CACHE_MISSES] = IST("ssl_cache_misses_total"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100520 [INF_COMPRESS_BPS_IN] = IST("http_comp_bytes_in_total"),
521 [INF_COMPRESS_BPS_OUT] = IST("http_comp_bytes_out_total"),
522 [INF_COMPRESS_BPS_RATE_LIM] = IST("limit_http_comp"),
523 [INF_ZLIB_MEM_USAGE] = IST("current_zlib_memory"),
524 [INF_MAX_ZLIB_MEM_USAGE] = IST("max_zlib_memory"),
525 [INF_TASKS] = IST("current_tasks"),
526 [INF_RUN_QUEUE] = IST("current_run_queue"),
527 [INF_IDLE_PCT] = IST("idle_time_percent"),
528 [INF_NODE] = IST("node"),
529 [INF_DESCRIPTION] = IST("description"),
530 [INF_STOPPING] = IST("stopping"),
531 [INF_JOBS] = IST("jobs"),
532 [INF_UNSTOPPABLE_JOBS] = IST("unstoppable_jobs"),
533 [INF_LISTENERS] = IST("listeners"),
534 [INF_ACTIVE_PEERS] = IST("active_peers"),
535 [INF_CONNECTED_PEERS] = IST("connected_peers"),
536 [INF_DROPPED_LOGS] = IST("dropped_logs_total"),
537 [INF_BUSY_POLLING] = IST("busy_polling_enabled"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200538 [INF_FAILED_RESOLUTIONS] = IST("failed_resolutions"),
539 [INF_TOTAL_BYTES_OUT] = IST("bytes_out_total"),
540 [INF_TOTAL_SPLICED_BYTES_OUT] = IST("spliced_bytes_out_total"),
541 [INF_BYTES_OUT_RATE] = IST("bytes_out_rate"),
542 [INF_DEBUG_COMMANDS_ISSUED] = IST("debug_commands_issued"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100543};
544
545/* Name of all stats fields */
546const struct ist promex_st_metric_names[ST_F_TOTAL_FIELDS] = {
547 [ST_F_PXNAME] = IST("proxy_name"),
548 [ST_F_SVNAME] = IST("service_name"),
549 [ST_F_QCUR] = IST("current_queue"),
550 [ST_F_QMAX] = IST("max_queue"),
551 [ST_F_SCUR] = IST("current_sessions"),
552 [ST_F_SMAX] = IST("max_sessions"),
553 [ST_F_SLIM] = IST("limit_sessions"),
554 [ST_F_STOT] = IST("sessions_total"),
555 [ST_F_BIN] = IST("bytes_in_total"),
556 [ST_F_BOUT] = IST("bytes_out_total"),
557 [ST_F_DREQ] = IST("requests_denied_total"),
558 [ST_F_DRESP] = IST("responses_denied_total"),
559 [ST_F_EREQ] = IST("request_errors_total"),
560 [ST_F_ECON] = IST("connection_errors_total"),
561 [ST_F_ERESP] = IST("response_errors_total"),
562 [ST_F_WRETR] = IST("retry_warnings_total"),
563 [ST_F_WREDIS] = IST("redispatch_warnings_total"),
564 [ST_F_STATUS] = IST("status"),
565 [ST_F_WEIGHT] = IST("weight"),
566 [ST_F_ACT] = IST("active_servers"),
567 [ST_F_BCK] = IST("backup_servers"),
568 [ST_F_CHKFAIL] = IST("check_failures_total"),
569 [ST_F_CHKDOWN] = IST("check_up_down_total"),
570 [ST_F_LASTCHG] = IST("check_last_change_seconds"),
571 [ST_F_DOWNTIME] = IST("downtime_seconds_total"),
572 [ST_F_QLIMIT] = IST("queue_limit"),
573 [ST_F_PID] = IST("pid"),
574 [ST_F_IID] = IST("proxy_id"),
575 [ST_F_SID] = IST("server_id"),
576 [ST_F_THROTTLE] = IST("current_throttle"),
577 [ST_F_LBTOT] = IST("loadbalanced_total"),
578 [ST_F_TRACKED] = IST("tracked"),
579 [ST_F_TYPE] = IST("type"),
580 [ST_F_RATE] = IST("current_session_rate"),
581 [ST_F_RATE_LIM] = IST("limit_session_rate"),
582 [ST_F_RATE_MAX] = IST("max_session_rate"),
583 [ST_F_CHECK_STATUS] = IST("check_status"),
584 [ST_F_CHECK_CODE] = IST("check_code"),
Christopher Faulet2711e512020-02-27 16:12:07 +0100585 [ST_F_CHECK_DURATION] = IST("check_duration_seconds"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100586 [ST_F_HRSP_1XX] = IST("http_responses_total"),
587 [ST_F_HRSP_2XX] = IST("http_responses_total"),
588 [ST_F_HRSP_3XX] = IST("http_responses_total"),
589 [ST_F_HRSP_4XX] = IST("http_responses_total"),
590 [ST_F_HRSP_5XX] = IST("http_responses_total"),
591 [ST_F_HRSP_OTHER] = IST("http_responses_total"),
592 [ST_F_HANAFAIL] = IST("check_analyses_failures_total"),
593 [ST_F_REQ_RATE] = IST("http_requests_rate_current"),
594 [ST_F_REQ_RATE_MAX] = IST("http_requests_rate_max"),
595 [ST_F_REQ_TOT] = IST("http_requests_total"),
596 [ST_F_CLI_ABRT] = IST("client_aborts_total"),
597 [ST_F_SRV_ABRT] = IST("server_aborts_total"),
598 [ST_F_COMP_IN] = IST("http_comp_bytes_in_total"),
599 [ST_F_COMP_OUT] = IST("http_comp_bytes_out_total"),
600 [ST_F_COMP_BYP] = IST("http_comp_bytes_bypassed_total"),
601 [ST_F_COMP_RSP] = IST("http_comp_responses_total"),
602 [ST_F_LASTSESS] = IST("last_session_seconds"),
603 [ST_F_LAST_CHK] = IST("check_last_content"),
604 [ST_F_LAST_AGT] = IST("agentcheck_last_content"),
Christopher Faulet68b69682019-11-08 15:12:29 +0100605 [ST_F_QTIME] = IST("queue_time_average_seconds"),
606 [ST_F_CTIME] = IST("connect_time_average_seconds"),
607 [ST_F_RTIME] = IST("response_time_average_seconds"),
608 [ST_F_TTIME] = IST("total_time_average_seconds"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100609 [ST_F_AGENT_STATUS] = IST("agentcheck_status"),
610 [ST_F_AGENT_CODE] = IST("agentcheck_code"),
611 [ST_F_AGENT_DURATION] = IST("agentcheck_duration_milliseconds"),
612 [ST_F_CHECK_DESC] = IST("check_description"),
613 [ST_F_AGENT_DESC] = IST("agentcheck_description"),
614 [ST_F_CHECK_RISE] = IST("check_rise"),
615 [ST_F_CHECK_FALL] = IST("check_fall"),
616 [ST_F_CHECK_HEALTH] = IST("check_value"),
617 [ST_F_AGENT_RISE] = IST("agentcheck_rise"),
618 [ST_F_AGENT_FALL] = IST("agentcheck_fall"),
619 [ST_F_AGENT_HEALTH] = IST("agentcheck_value"),
620 [ST_F_ADDR] = IST("address"),
621 [ST_F_COOKIE] = IST("cookie"),
622 [ST_F_MODE] = IST("mode"),
623 [ST_F_ALGO] = IST("loadbalance_algorithm"),
624 [ST_F_CONN_RATE] = IST("connections_rate_current"),
625 [ST_F_CONN_RATE_MAX] = IST("connections_rate_max"),
626 [ST_F_CONN_TOT] = IST("connections_total"),
627 [ST_F_INTERCEPTED] = IST("intercepted_requests_total"),
628 [ST_F_DCON] = IST("denied_connections_total"),
629 [ST_F_DSES] = IST("denied_sessions_total"),
630 [ST_F_WREW] = IST("failed_header_rewriting_total"),
631 [ST_F_CONNECT] = IST("connection_attempts_total"),
632 [ST_F_REUSE] = IST("connection_reuses_total"),
633 [ST_F_CACHE_LOOKUPS] = IST("http_cache_lookups_total"),
634 [ST_F_CACHE_HITS] = IST("http_cache_hits_total"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200635 [ST_F_SRV_ICUR] = IST("idle_connections_current"),
636 [ST_F_SRV_ILIM] = IST("idle_connections_limit"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100637 [ST_F_QT_MAX] = IST("max_queue_time_seconds"),
638 [ST_F_CT_MAX] = IST("max_connect_time_seconds"),
639 [ST_F_RT_MAX] = IST("max_response_time_seconds"),
640 [ST_F_TT_MAX] = IST("max_total_time_seconds"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100641 [ST_F_EINT] = IST("internal_errors_total"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200642 [ST_F_IDLE_CONN_CUR] = IST("unsafe_idle_connections_current"),
643 [ST_F_SAFE_CONN_CUR] = IST("safe_idle_connections_current"),
644 [ST_F_USED_CONN_CUR] = IST("used_connections_current"),
645 [ST_F_NEED_CONN_EST] = IST("need_connections_current"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100646};
647
Christopher Fauletf959d082019-02-07 15:38:42 +0100648/* Description of all stats fields */
649const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
650 [ST_F_PXNAME] = IST("The proxy name."),
651 [ST_F_SVNAME] = IST("The service name (FRONTEND for frontend, BACKEND for backend, any name for server/listener)."),
652 [ST_F_QCUR] = IST("Current number of queued requests."),
653 [ST_F_QMAX] = IST("Maximum observed number of queued requests."),
654 [ST_F_SCUR] = IST("Current number of active sessions."),
655 [ST_F_SMAX] = IST("Maximum observed number of active sessions."),
656 [ST_F_SLIM] = IST("Configured session limit."),
657 [ST_F_STOT] = IST("Total number of sessions."),
658 [ST_F_BIN] = IST("Current total of incoming bytes."),
659 [ST_F_BOUT] = IST("Current total of outgoing bytes."),
660 [ST_F_DREQ] = IST("Total number of denied requests."),
661 [ST_F_DRESP] = IST("Total number of denied responses."),
662 [ST_F_EREQ] = IST("Total number of request errors."),
663 [ST_F_ECON] = IST("Total number of connection errors."),
664 [ST_F_ERESP] = IST("Total number of response errors."),
665 [ST_F_WRETR] = IST("Total number of retry warnings."),
666 [ST_F_WREDIS] = IST("Total number of redispatch warnings."),
Willy Tarreau6b3bf732020-09-24 07:35:46 +0200667 [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 +0100668 [ST_F_WEIGHT] = IST("Service weight."),
669 [ST_F_ACT] = IST("Current number of active servers."),
670 [ST_F_BCK] = IST("Current number of backup servers."),
671 [ST_F_CHKFAIL] = IST("Total number of failed check (Only counts checks failed when the server is up)."),
672 [ST_F_CHKDOWN] = IST("Total number of UP->DOWN transitions."),
673 [ST_F_LASTCHG] = IST("Number of seconds since the last UP<->DOWN transition."),
674 [ST_F_DOWNTIME] = IST("Total downtime (in seconds) for the service."),
675 [ST_F_QLIMIT] = IST("Configured maxqueue for the server (0 meaning no limit)."),
676 [ST_F_PID] = IST("Process id (0 for first instance, 1 for second, ...)"),
677 [ST_F_IID] = IST("Unique proxy id."),
678 [ST_F_SID] = IST("Server id (unique inside a proxy)."),
679 [ST_F_THROTTLE] = IST("Current throttle percentage for the server, when slowstart is active, or no value if not in slowstart."),
680 [ST_F_LBTOT] = IST("Total number of times a service was selected, either for new sessions, or when redispatching."),
681 [ST_F_TRACKED] = IST("Id of proxy/server if tracking is enabled."),
682 [ST_F_TYPE] = IST("Service type (0=frontend, 1=backend, 2=server, 3=socket/listener)."),
683 [ST_F_RATE] = IST("Current number of sessions per second over last elapsed second."),
684 [ST_F_RATE_LIM] = IST("Configured limit on new sessions per second."),
685 [ST_F_RATE_MAX] = IST("Maximum observed number of sessions per second."),
Christopher Fauletcf403f32019-11-21 14:35:46 +0100686 [ST_F_CHECK_STATUS] = IST("Status of last health check (HCHK_STATUS_* values)."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100687 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100688 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100689 [ST_F_HRSP_1XX] = IST("Total number of HTTP responses."),
690 [ST_F_HRSP_2XX] = IST("Total number of HTTP responses."),
691 [ST_F_HRSP_3XX] = IST("Total number of HTTP responses."),
692 [ST_F_HRSP_4XX] = IST("Total number of HTTP responses."),
693 [ST_F_HRSP_5XX] = IST("Total number of HTTP responses."),
694 [ST_F_HRSP_OTHER] = IST("Total number of HTTP responses."),
695 [ST_F_HANAFAIL] = IST("Total number of failed health checks."),
696 [ST_F_REQ_RATE] = IST("Current number of HTTP requests per second over last elapsed second."),
697 [ST_F_REQ_RATE_MAX] = IST("Maximum observed number of HTTP requests per second."),
698 [ST_F_REQ_TOT] = IST("Total number of HTTP requests received."),
699 [ST_F_CLI_ABRT] = IST("Total number of data transfers aborted by the client."),
700 [ST_F_SRV_ABRT] = IST("Total number of data transfers aborted by the server."),
701 [ST_F_COMP_IN] = IST("Total number of HTTP response bytes fed to the compressor."),
702 [ST_F_COMP_OUT] = IST("Total number of HTTP response bytes emitted by the compressor."),
703 [ST_F_COMP_BYP] = IST("Total number of bytes that bypassed the HTTP compressor (CPU/BW limit)."),
704 [ST_F_COMP_RSP] = IST("Total number of HTTP responses that were compressed."),
705 [ST_F_LASTSESS] = IST("Number of seconds since last session assigned to server/backend."),
706 [ST_F_LAST_CHK] = IST("Last health check contents or textual error"),
707 [ST_F_LAST_AGT] = IST("Last agent check contents or textual error"),
708 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
709 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
710 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
711 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
712 [ST_F_AGENT_STATUS] = IST("Status of last agent check."),
713 [ST_F_AGENT_CODE] = IST("Numeric code reported by agent if any (unused for now)."),
714 [ST_F_AGENT_DURATION] = IST("Time in ms taken to finish last agent check."),
715 [ST_F_CHECK_DESC] = IST("Short human-readable description of the last health status."),
716 [ST_F_AGENT_DESC] = IST("Short human-readable description of the last agent status."),
717 [ST_F_CHECK_RISE] = IST("Server's \"rise\" parameter used by health checks"),
718 [ST_F_CHECK_FALL] = IST("Server's \"fall\" parameter used by health checks"),
719 [ST_F_CHECK_HEALTH] = IST("Server's health check value between 0 and rise+fall-1"),
720 [ST_F_AGENT_RISE] = IST("Agent's \"rise\" parameter, normally 1."),
721 [ST_F_AGENT_FALL] = IST("Agent's \"fall\" parameter, normally 1."),
722 [ST_F_AGENT_HEALTH] = IST("Agent's health parameter, between 0 and rise+fall-1"),
723 [ST_F_ADDR] = IST("address:port or \"unix\". IPv6 has brackets around the address."),
724 [ST_F_COOKIE] = IST("Server's cookie value or backend's cookie name."),
725 [ST_F_MODE] = IST("Proxy mode (tcp, http, health, unknown)."),
726 [ST_F_ALGO] = IST("Load balancing algorithm."),
727 [ST_F_CONN_RATE] = IST("Current number of connections per second over the last elapsed second."),
728 [ST_F_CONN_RATE_MAX] = IST("Maximum observed number of connections per second."),
729 [ST_F_CONN_TOT] = IST("Total number of connections."),
730 [ST_F_INTERCEPTED] = IST("Total number of intercepted HTTP requests."),
731 [ST_F_DCON] = IST("Total number of requests denied by \"tcp-request connection\" rules."),
732 [ST_F_DSES] = IST("Total number of requests denied by \"tcp-request session\" rules."),
733 [ST_F_WREW] = IST("Total number of failed header rewriting warnings."),
734 [ST_F_CONNECT] = IST("Total number of connection establishment attempts."),
735 [ST_F_REUSE] = IST("Total number of connection reuses."),
736 [ST_F_CACHE_LOOKUPS] = IST("Total number of HTTP cache lookups."),
737 [ST_F_CACHE_HITS] = IST("Total number of HTTP cache hits."),
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100738 [ST_F_SRV_ICUR] = IST("Current number of idle connections available for reuse"),
739 [ST_F_SRV_ILIM] = IST("Limit on the number of available idle connections"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100740 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
741 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
742 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
743 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100744 [ST_F_EINT] = IST("Total number of internal errors."),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200745 [ST_F_IDLE_CONN_CUR] = IST("Current number of unsafe idle connections."),
746 [ST_F_SAFE_CONN_CUR] = IST("Current number of safe idle connections."),
747 [ST_F_USED_CONN_CUR] = IST("Current number of connections in use."),
748 [ST_F_NEED_CONN_EST] = IST("Estimated needed number of connections."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100749};
750
751/* Specific labels for all info fields. Empty by default. */
752const struct ist promex_inf_metric_labels[INF_TOTAL_FIELDS] = {
Christopher Faulet0175b1f2021-01-20 15:28:22 +0100753 [INF_BUILD_INFO] = IST(PROMEX_BUILDINFO_LABEL),
Christopher Fauletf959d082019-02-07 15:38:42 +0100754};
755
756/* Specific labels for all stats fields. Empty by default. */
757const struct ist promex_st_metric_labels[ST_F_TOTAL_FIELDS] = {
Christopher Faulet0175b1f2021-01-20 15:28:22 +0100758 [ST_F_HRSP_1XX] = IST("code=\"1xx\""),
759 [ST_F_HRSP_2XX] = IST("code=\"2xx\""),
760 [ST_F_HRSP_3XX] = IST("code=\"3xx\""),
761 [ST_F_HRSP_4XX] = IST("code=\"4xx\""),
762 [ST_F_HRSP_5XX] = IST("code=\"5xx\""),
763 [ST_F_HRSP_OTHER] = IST("code=\"other\""),
Christopher Fauletf959d082019-02-07 15:38:42 +0100764};
765
766/* Type for all info fields. "untyped" is used for unsupported field. */
767const struct ist promex_inf_metric_types[INF_TOTAL_FIELDS] = {
768 [INF_NAME] = IST("untyped"),
769 [INF_VERSION] = IST("untyped"),
770 [INF_RELEASE_DATE] = IST("untyped"),
William Dauchy5a982a72021-01-08 13:18:06 +0100771 [INF_BUILD_INFO] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200772 [INF_NBTHREAD] = IST("gauge"),
773 [INF_NBPROC] = IST("gauge"),
774 [INF_PROCESS_NUM] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100775 [INF_PID] = IST("untyped"),
776 [INF_UPTIME] = IST("untyped"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200777 [INF_UPTIME_SEC] = IST("gauge"),
William Dauchydefd1562021-01-15 22:41:38 +0100778 [INF_START_TIME_SEC] = IST("gauge"),
William Dauchya8766cf2021-01-15 22:41:37 +0100779 [INF_MEMMAX_BYTES] = IST("gauge"),
780 [INF_POOL_ALLOC_BYTES] = IST("gauge"),
781 [INF_POOL_USED_BYTES] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100782 [INF_POOL_FAILED] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200783 [INF_ULIMIT_N] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100784 [INF_MAXSOCK] = IST("gauge"),
785 [INF_MAXCONN] = IST("gauge"),
786 [INF_HARD_MAXCONN] = IST("gauge"),
787 [INF_CURR_CONN] = IST("gauge"),
788 [INF_CUM_CONN] = IST("counter"),
789 [INF_CUM_REQ] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200790 [INF_MAX_SSL_CONNS] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100791 [INF_CURR_SSL_CONNS] = IST("gauge"),
792 [INF_CUM_SSL_CONNS] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200793 [INF_MAXPIPES] = IST("gauge"),
794 [INF_PIPES_USED] = IST("counter"),
795 [INF_PIPES_FREE] = IST("counter"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100796 [INF_CONN_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200797 [INF_CONN_RATE_LIMIT] = IST("gauge"),
798 [INF_MAX_CONN_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100799 [INF_SESS_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200800 [INF_SESS_RATE_LIMIT] = IST("gauge"),
801 [INF_MAX_SESS_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100802 [INF_SSL_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200803 [INF_SSL_RATE_LIMIT] = IST("gauge"),
804 [INF_MAX_SSL_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100805 [INF_SSL_FRONTEND_KEY_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200806 [INF_SSL_FRONTEND_MAX_KEY_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100807 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = IST("gauge"),
808 [INF_SSL_BACKEND_KEY_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200809 [INF_SSL_BACKEND_MAX_KEY_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100810 [INF_SSL_CACHE_LOOKUPS] = IST("counter"),
811 [INF_SSL_CACHE_MISSES] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200812 [INF_COMPRESS_BPS_IN] = IST("counter"),
813 [INF_COMPRESS_BPS_OUT] = IST("counter"),
814 [INF_COMPRESS_BPS_RATE_LIM] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100815 [INF_ZLIB_MEM_USAGE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200816 [INF_MAX_ZLIB_MEM_USAGE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100817 [INF_TASKS] = IST("gauge"),
Christopher Fauletf782c232019-04-17 16:04:44 +0200818 [INF_RUN_QUEUE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100819 [INF_IDLE_PCT] = IST("gauge"),
820 [INF_NODE] = IST("untyped"),
821 [INF_DESCRIPTION] = IST("untyped"),
822 [INF_STOPPING] = IST("gauge"),
823 [INF_JOBS] = IST("gauge"),
824 [INF_UNSTOPPABLE_JOBS] = IST("gauge"),
825 [INF_LISTENERS] = IST("gauge"),
826 [INF_ACTIVE_PEERS] = IST("gauge"),
827 [INF_CONNECTED_PEERS] = IST("gauge"),
828 [INF_DROPPED_LOGS] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200829 [INF_BUSY_POLLING] = IST("gauge"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200830 [INF_FAILED_RESOLUTIONS] = IST("counter"),
831 [INF_TOTAL_BYTES_OUT] = IST("counter"),
832 [INF_TOTAL_SPLICED_BYTES_OUT] = IST("counter"),
833 [INF_BYTES_OUT_RATE] = IST("gauge"),
834 [INF_DEBUG_COMMANDS_ISSUED] = IST(""),
Christopher Fauletf959d082019-02-07 15:38:42 +0100835};
836
837/* Type for all stats fields. "untyped" is used for unsupported field. */
838const struct ist promex_st_metric_types[ST_F_TOTAL_FIELDS] = {
839 [ST_F_PXNAME] = IST("untyped"),
840 [ST_F_SVNAME] = IST("untyped"),
841 [ST_F_QCUR] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200842 [ST_F_QMAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100843 [ST_F_SCUR] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200844 [ST_F_SMAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100845 [ST_F_SLIM] = IST("gauge"),
846 [ST_F_STOT] = IST("counter"),
847 [ST_F_BIN] = IST("counter"),
848 [ST_F_BOUT] = IST("counter"),
849 [ST_F_DREQ] = IST("counter"),
850 [ST_F_DRESP] = IST("counter"),
851 [ST_F_EREQ] = IST("counter"),
852 [ST_F_ECON] = IST("counter"),
853 [ST_F_ERESP] = IST("counter"),
854 [ST_F_WRETR] = IST("counter"),
855 [ST_F_WREDIS] = IST("counter"),
856 [ST_F_STATUS] = IST("gauge"),
857 [ST_F_WEIGHT] = IST("gauge"),
858 [ST_F_ACT] = IST("gauge"),
859 [ST_F_BCK] = IST("gauge"),
860 [ST_F_CHKFAIL] = IST("counter"),
861 [ST_F_CHKDOWN] = IST("counter"),
862 [ST_F_LASTCHG] = IST("gauge"),
863 [ST_F_DOWNTIME] = IST("counter"),
864 [ST_F_QLIMIT] = IST("gauge"),
865 [ST_F_PID] = IST("untyped"),
866 [ST_F_IID] = IST("untyped"),
867 [ST_F_SID] = IST("untyped"),
868 [ST_F_THROTTLE] = IST("gauge"),
869 [ST_F_LBTOT] = IST("counter"),
870 [ST_F_TRACKED] = IST("untyped"),
871 [ST_F_TYPE] = IST("untyped"),
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200872 [ST_F_RATE] = IST("untyped"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100873 [ST_F_RATE_LIM] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200874 [ST_F_RATE_MAX] = IST("gauge"),
Christopher Fauletcf403f32019-11-21 14:35:46 +0100875 [ST_F_CHECK_STATUS] = IST("gauge"),
876 [ST_F_CHECK_CODE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100877 [ST_F_CHECK_DURATION] = IST("gauge"),
878 [ST_F_HRSP_1XX] = IST("counter"),
879 [ST_F_HRSP_2XX] = IST("counter"),
880 [ST_F_HRSP_3XX] = IST("counter"),
881 [ST_F_HRSP_4XX] = IST("counter"),
882 [ST_F_HRSP_5XX] = IST("counter"),
883 [ST_F_HRSP_OTHER] = IST("counter"),
884 [ST_F_HANAFAIL] = IST("counter"),
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200885 [ST_F_REQ_RATE] = IST("untyped"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200886 [ST_F_REQ_RATE_MAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100887 [ST_F_REQ_TOT] = IST("counter"),
888 [ST_F_CLI_ABRT] = IST("counter"),
889 [ST_F_SRV_ABRT] = IST("counter"),
890 [ST_F_COMP_IN] = IST("counter"),
891 [ST_F_COMP_OUT] = IST("counter"),
892 [ST_F_COMP_BYP] = IST("counter"),
893 [ST_F_COMP_RSP] = IST("counter"),
894 [ST_F_LASTSESS] = IST("gauge"),
895 [ST_F_LAST_CHK] = IST("untyped"),
896 [ST_F_LAST_AGT] = IST("untyped"),
897 [ST_F_QTIME] = IST("gauge"),
898 [ST_F_CTIME] = IST("gauge"),
899 [ST_F_RTIME] = IST("gauge"),
900 [ST_F_TTIME] = IST("gauge"),
901 [ST_F_AGENT_STATUS] = IST("untyped"),
902 [ST_F_AGENT_CODE] = IST("untyped"),
903 [ST_F_AGENT_DURATION] = IST("gauge"),
904 [ST_F_CHECK_DESC] = IST("untyped"),
905 [ST_F_AGENT_DESC] = IST("untyped"),
906 [ST_F_CHECK_RISE] = IST("gauge"),
907 [ST_F_CHECK_FALL] = IST("gauge"),
908 [ST_F_CHECK_HEALTH] = IST("gauge"),
909 [ST_F_AGENT_RISE] = IST("gauge"),
910 [ST_F_AGENT_FALL] = IST("gauge"),
911 [ST_F_AGENT_HEALTH] = IST("gauge"),
912 [ST_F_ADDR] = IST("untyped"),
913 [ST_F_COOKIE] = IST("untyped"),
914 [ST_F_MODE] = IST("untyped"),
915 [ST_F_ALGO] = IST("untyped"),
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200916 [ST_F_CONN_RATE] = IST("untyped"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200917 [ST_F_CONN_RATE_MAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100918 [ST_F_CONN_TOT] = IST("counter"),
919 [ST_F_INTERCEPTED] = IST("counter"),
920 [ST_F_DCON] = IST("counter"),
921 [ST_F_DSES] = IST("counter"),
922 [ST_F_WREW] = IST("counter"),
923 [ST_F_CONNECT] = IST("counter"),
924 [ST_F_REUSE] = IST("counter"),
925 [ST_F_CACHE_LOOKUPS] = IST("counter"),
926 [ST_F_CACHE_HITS] = IST("counter"),
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100927 [ST_F_SRV_ICUR] = IST("gauge"),
928 [ST_F_SRV_ILIM] = IST("gauge"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100929 [ST_F_QT_MAX] = IST("gauge"),
930 [ST_F_CT_MAX] = IST("gauge"),
931 [ST_F_RT_MAX] = IST("gauge"),
932 [ST_F_TT_MAX] = IST("gauge"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100933 [ST_F_EINT] = IST("counter"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200934 [ST_F_IDLE_CONN_CUR] = IST("gauge"),
935 [ST_F_SAFE_CONN_CUR] = IST("gauge"),
936 [ST_F_USED_CONN_CUR] = IST("gauge"),
937 [ST_F_NEED_CONN_EST] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100938};
939
Christopher Fauletd45d1052019-09-06 16:10:19 +0200940/* Return the server status: 0=DOWN, 1=UP, 2=MAINT, 3=DRAIN, 4=NOLB. */
Christopher Fauletf959d082019-02-07 15:38:42 +0100941static int promex_srv_status(struct server *sv)
942{
Christopher Fauletf959d082019-02-07 15:38:42 +0100943 int state = 0;
944
Christopher Fauletf959d082019-02-07 15:38:42 +0100945 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
946 state = 1;
947 if (sv->cur_admin & SRV_ADMF_DRAIN)
Christopher Fauletd45d1052019-09-06 16:10:19 +0200948 state = 3;
Christopher Fauletf959d082019-02-07 15:38:42 +0100949 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200950 else if (sv->cur_state == SRV_ST_STOPPING)
951 state = 4;
952
953 if (sv->cur_admin & SRV_ADMF_MAINT)
954 state = 2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100955
956 return state;
957}
958
959/* Convert a field to its string representation and write it in <out>, followed
960 * by a newline, if there is enough space. non-numeric value are converted in
961 * "Nan" because Prometheus only support numerical values (but it is unexepceted
962 * to process this kind of value). It returns 1 on success. Otherwise, it
963 * returns 0. The buffer's length must not exceed <max> value.
964 */
965static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
966{
967 int ret = 0;
968
969 switch (field_format(f, 0)) {
970 case FF_EMPTY: ret = chunk_strcat(out, "Nan\n"); break;
971 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
972 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
973 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
974 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200975 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100976 case FF_STR: ret = chunk_strcat(out, "Nan\n"); break;
977 default: ret = chunk_strcat(out, "Nan\n"); break;
978 }
979 if (!ret || out->data > max)
980 return 0;
981 return 1;
982}
983
984/* Concatenate the <prefix> with the field name using the array
985 * <promex_st_metric_names> and store it in <name>. The field type is in
986 * <appctx->st2>. This function never fails but relies on
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500987 * <PROMEX_MAX_NAME_LEN>. So by sure the result is small enough to be copied in
Christopher Fauletf959d082019-02-07 15:38:42 +0100988 * <name>
989 */
990static void promex_metric_name(struct appctx *appctx, struct ist *name, const struct ist prefix)
991{
992 const struct ist *names;
993
994 names = ((appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC)
995 ? promex_inf_metric_names
996 : promex_st_metric_names);
997
998 istcat(name, prefix, PROMEX_MAX_NAME_LEN);
999 istcat(name, names[appctx->st2], PROMEX_MAX_NAME_LEN);
1000}
1001
1002/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
1003 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
1004 */
1005static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
1006 const struct ist name, struct ist *out, size_t max)
1007{
1008 const struct ist *desc, *types;
1009
William Dauchya191b772021-01-15 22:41:39 +01001010 if (istcat(out, ist("# HELP "), max) == -1 ||
1011 istcat(out, name, max) == -1 ||
1012 istcat(out, ist(" "), max) == -1)
1013 goto full;
1014
Christopher Fauletf959d082019-02-07 15:38:42 +01001015 if (appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC) {
Christopher Fauletf959d082019-02-07 15:38:42 +01001016 types = promex_inf_metric_types;
William Dauchya191b772021-01-15 22:41:39 +01001017
1018 if (istcat(out, ist(info_fields[appctx->st2].desc), max) == -1)
1019 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +01001020 }
1021 else {
1022 desc = promex_st_metric_desc;
1023 types = promex_st_metric_types;
William Dauchya191b772021-01-15 22:41:39 +01001024
1025 if (istcat(out, desc[appctx->st2], max) == -1)
1026 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +01001027 }
1028
William Dauchya191b772021-01-15 22:41:39 +01001029 if (istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001030 istcat(out, name, max) == -1 ||
1031 istcat(out, ist(" "), max) == -1 ||
1032 istcat(out, types[appctx->st2], max) == -1 ||
1033 istcat(out, ist("\n"), max) == -1)
1034 goto full;
1035
1036 return 1;
1037
1038 full:
1039 return 0;
1040}
1041
1042/* Dump the line for <metric>. It starts by the metric name followed by its
1043 * labels (proxy name, server name...) between braces and finally its value. If
1044 * not already done, the header lines are dumped first. It returns 1 on
1045 * success. Otherwise if <out> length exceeds <max>, it returns 0.
1046 */
1047static int promex_dump_metric(struct appctx *appctx, struct htx *htx,
1048 const struct ist prefix, struct field *metric,
1049 struct ist *out, size_t max)
1050{
1051 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
1052 size_t len = out->len;
1053
1054 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
1055 return 0;
1056
1057 promex_metric_name(appctx, &name, prefix);
1058 if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) &&
1059 !promex_dump_metric_header(appctx, htx, name, out, max))
1060 goto full;
1061
1062 if (appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC) {
1063 const struct ist label = promex_inf_metric_labels[appctx->st2];
1064
1065 if (istcat(out, name, max) == -1 ||
1066 (label.len && istcat(out, ist("{"), max) == -1) ||
1067 (label.len && istcat(out, label, max) == -1) ||
1068 (label.len && istcat(out, ist("}"), max) == -1) ||
1069 istcat(out, ist(" "), max) == -1)
1070 goto full;
1071 }
1072 else {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001073 struct proxy *px = appctx->ctx.stats.obj1;
1074 struct server *srv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +01001075 const struct ist label = promex_st_metric_labels[appctx->st2];
1076
1077 if (istcat(out, name, max) == -1 ||
1078 istcat(out, ist("{proxy=\""), max) == -1 ||
1079 istcat(out, ist2(px->id, strlen(px->id)), max) == -1 ||
1080 istcat(out, ist("\""), max) == -1 ||
1081 (srv && istcat(out, ist(",server=\""), max) == -1) ||
1082 (srv && istcat(out, ist2(srv->id, strlen(srv->id)), max) == -1) ||
1083 (srv && istcat(out, ist("\""), max) == -1) ||
1084 (label.len && istcat(out, ist(","), max) == -1) ||
1085 (label.len && istcat(out, label, max) == -1) ||
1086 istcat(out, ist("} "), max) == -1)
1087 goto full;
1088 }
1089
1090 trash.data = out->len;
1091 if (!promex_metric_to_str(&trash, metric, max))
1092 goto full;
1093 out->len = trash.data;
1094
1095 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1096 return 1;
1097 full:
1098 // Restore previous length
1099 out->len = len;
1100 return 0;
1101
1102}
1103
1104
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001105/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001106 * 0 if <htx> is full and -1 in case of any error. */
1107static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
1108{
1109 static struct ist prefix = IST("haproxy_process_");
1110 struct field metric;
1111 struct channel *chn = si_ic(appctx->owner);
1112 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001113 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001114 int ret = 1;
1115
William Dauchy5d9b8f32021-01-11 20:07:49 +01001116 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
1117 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001118
Christopher Fauletf959d082019-02-07 15:38:42 +01001119 while (appctx->st2 && appctx->st2 < INF_TOTAL_FIELDS) {
1120 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +01001121 case INF_BUILD_INFO:
1122 metric = mkf_u32(FN_GAUGE, 1);
1123 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001124
1125 default:
William Dauchy5d9b8f32021-01-11 20:07:49 +01001126 metric = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001127 }
1128
1129 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1130 goto full;
1131
Christopher Fauletf959d082019-02-07 15:38:42 +01001132 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
1133 appctx->st2 = promex_global_metrics[appctx->st2];
1134 }
1135
1136 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001137 if (out.len) {
1138 if (!htx_add_data_atonce(htx, out))
1139 return -1; /* Unexpected and unrecoverable error */
1140 channel_add_input(chn, out.len);
1141 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001142 return ret;
1143 full:
1144 ret = 0;
1145 goto end;
1146}
1147
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001148/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001149 * 0 if <htx> is full and -1 in case of any error. */
1150static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
1151{
1152 static struct ist prefix = IST("haproxy_frontend_");
1153 struct proxy *px;
1154 struct field metric;
1155 struct channel *chn = si_ic(appctx->owner);
1156 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001157 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +01001158 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +01001159 int ret = 1;
1160
1161 while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001162 while (appctx->ctx.stats.obj1) {
1163 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001164
1165 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001166 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +01001167 goto next_px;
1168
William Dauchyb9577452021-01-17 18:27:46 +01001169 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
1170 return -1;
1171
Christopher Fauletf959d082019-02-07 15:38:42 +01001172 switch (appctx->st2) {
1173 case ST_F_STATUS:
Willy Tarreauc3914d42020-09-24 08:39:22 +02001174 metric = mkf_u32(FO_STATUS, !px->disabled);
Christopher Fauletf959d082019-02-07 15:38:42 +01001175 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001176 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001177 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001178 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001179 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +01001180 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +01001181 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +01001182 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +01001183 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001184 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +01001185 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +01001186 if (px->mode != PR_MODE_HTTP)
1187 goto next_px;
William Dauchyb9577452021-01-17 18:27:46 +01001188 metric = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001189 break;
William Dauchyb9577452021-01-17 18:27:46 +01001190 case ST_F_HRSP_2XX:
1191 case ST_F_HRSP_3XX:
1192 case ST_F_HRSP_4XX:
1193 case ST_F_HRSP_5XX:
1194 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +01001195 if (px->mode != PR_MODE_HTTP)
1196 goto next_px;
William Dauchyb9577452021-01-17 18:27:46 +01001197 metric = stats[appctx->st2];
1198 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +01001199 break;
1200
1201 default:
William Dauchyb9577452021-01-17 18:27:46 +01001202 metric = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001203 }
1204
1205 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1206 goto full;
1207 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001208 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001209 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001210 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001211 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +01001212 appctx->st2 = promex_front_metrics[appctx->st2];
1213 }
1214
1215 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001216 if (out.len) {
1217 if (!htx_add_data_atonce(htx, out))
1218 return -1; /* Unexpected and unrecoverable error */
1219 channel_add_input(chn, out.len);
1220 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001221 return ret;
1222 full:
1223 ret = 0;
1224 goto end;
1225}
1226
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001227/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001228 * 0 if <htx> is full and -1 in case of any error. */
1229static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
1230{
1231 static struct ist prefix = IST("haproxy_backend_");
1232 struct proxy *px;
1233 struct field metric;
1234 struct channel *chn = si_ic(appctx->owner);
1235 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001236 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001237 int ret = 1;
1238 uint32_t weight;
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001239 double secs;
Christopher Fauletf959d082019-02-07 15:38:42 +01001240
1241 while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001242 while (appctx->ctx.stats.obj1) {
1243 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001244
1245 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001246 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +01001247 goto next_px;
1248
1249 switch (appctx->st2) {
1250 case ST_F_STATUS:
1251 metric = mkf_u32(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
1252 break;
1253 case ST_F_SCUR:
1254 metric = mkf_u32(0, px->beconn);
1255 break;
1256 case ST_F_SMAX:
1257 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
1258 break;
1259 case ST_F_SLIM:
1260 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1261 break;
1262 case ST_F_STOT:
1263 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1264 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001265 case ST_F_RATE_MAX:
1266 metric = mkf_u32(0, px->be_counters.sps_max);
1267 break;
1268 case ST_F_LASTSESS:
1269 metric = mkf_s32(FN_AGE, be_lastsession(px));
1270 break;
1271 case ST_F_QCUR:
1272 metric = mkf_u32(0, px->nbpend);
1273 break;
1274 case ST_F_QMAX:
1275 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
1276 break;
1277 case ST_F_CONNECT:
1278 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
1279 break;
1280 case ST_F_REUSE:
1281 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
1282 break;
1283 case ST_F_BIN:
1284 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1285 break;
1286 case ST_F_BOUT:
1287 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1288 break;
1289 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001290 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
1291 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001292 break;
1293 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001294 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
1295 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001296 break;
1297 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001298 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
1299 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001300 break;
1301 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001302 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
1303 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001304 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001305 case ST_F_QT_MAX:
1306 secs = (double)px->be_counters.qtime_max / 1000.0;
1307 metric = mkf_flt(FN_MAX, secs);
1308 break;
1309 case ST_F_CT_MAX:
1310 secs = (double)px->be_counters.ctime_max / 1000.0;
1311 metric = mkf_flt(FN_MAX, secs);
1312 break;
1313 case ST_F_RT_MAX:
1314 secs = (double)px->be_counters.dtime_max / 1000.0;
1315 metric = mkf_flt(FN_MAX, secs);
1316 break;
1317 case ST_F_TT_MAX:
1318 secs = (double)px->be_counters.ttime_max / 1000.0;
1319 metric = mkf_flt(FN_MAX, secs);
1320 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001321 case ST_F_DREQ:
1322 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1323 break;
1324 case ST_F_DRESP:
1325 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1326 break;
1327 case ST_F_ECON:
1328 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1329 break;
1330 case ST_F_ERESP:
1331 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1332 break;
1333 case ST_F_WRETR:
1334 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
1335 break;
1336 case ST_F_WREDIS:
1337 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
1338 break;
1339 case ST_F_WREW:
1340 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
1341 break;
Christopher Faulete4a2c8d2019-12-16 14:44:01 +01001342 case ST_F_EINT:
1343 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
1344 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001345 case ST_F_CLI_ABRT:
1346 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1347 break;
1348 case ST_F_SRV_ABRT:
1349 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1350 break;
1351 case ST_F_WEIGHT:
1352 weight = (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
1353 metric = mkf_u32(FN_AVG, weight);
1354 break;
1355 case ST_F_ACT:
1356 metric = mkf_u32(0, px->srv_act);
1357 break;
1358 case ST_F_BCK:
1359 metric = mkf_u32(0, px->srv_bck);
1360 break;
1361 case ST_F_CHKDOWN:
1362 metric = mkf_u64(FN_COUNTER, px->down_trans);
1363 break;
1364 case ST_F_LASTCHG:
1365 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1366 break;
1367 case ST_F_DOWNTIME:
1368 metric = mkf_u32(FN_COUNTER, be_downtime(px));
1369 break;
1370 case ST_F_LBTOT:
1371 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1372 break;
1373 case ST_F_REQ_TOT:
1374 if (px->mode != PR_MODE_HTTP)
1375 goto next_px;
1376 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1377 break;
1378 case ST_F_HRSP_1XX:
1379 if (px->mode != PR_MODE_HTTP)
1380 goto next_px;
1381 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1382 break;
1383 case ST_F_HRSP_2XX:
1384 if (px->mode != PR_MODE_HTTP)
1385 goto next_px;
1386 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1387 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1388 break;
1389 case ST_F_HRSP_3XX:
1390 if (px->mode != PR_MODE_HTTP)
1391 goto next_px;
1392 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1393 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1394 break;
1395 case ST_F_HRSP_4XX:
1396 if (px->mode != PR_MODE_HTTP)
1397 goto next_px;
1398 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1399 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1400 break;
1401 case ST_F_HRSP_5XX:
1402 if (px->mode != PR_MODE_HTTP)
1403 goto next_px;
1404 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1405 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1406 break;
1407 case ST_F_HRSP_OTHER:
1408 if (px->mode != PR_MODE_HTTP)
1409 goto next_px;
1410 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1411 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
1412 break;
1413 case ST_F_CACHE_LOOKUPS:
1414 if (px->mode != PR_MODE_HTTP)
1415 goto next_px;
1416 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1417 break;
1418 case ST_F_CACHE_HITS:
1419 if (px->mode != PR_MODE_HTTP)
1420 goto next_px;
1421 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
1422 break;
1423 case ST_F_COMP_IN:
1424 if (px->mode != PR_MODE_HTTP)
1425 goto next_px;
1426 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1427 break;
1428 case ST_F_COMP_OUT:
1429 if (px->mode != PR_MODE_HTTP)
1430 goto next_px;
1431 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1432 break;
1433 case ST_F_COMP_BYP:
1434 if (px->mode != PR_MODE_HTTP)
1435 goto next_px;
1436 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1437 break;
1438 case ST_F_COMP_RSP:
1439 if (px->mode != PR_MODE_HTTP)
1440 goto next_px;
1441 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1442 break;
1443
1444 default:
1445 goto next_metric;
1446 }
1447
1448 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1449 goto full;
1450 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001451 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001452 }
1453 next_metric:
1454 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001455 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +01001456 appctx->st2 = promex_back_metrics[appctx->st2];
1457 }
1458
1459 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001460 if (out.len) {
1461 if (!htx_add_data_atonce(htx, out))
1462 return -1; /* Unexpected and unrecoverable error */
1463 channel_add_input(chn, out.len);
1464 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001465 return ret;
1466 full:
1467 ret = 0;
1468 goto end;
1469}
1470
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001471/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001472 * 0 if <htx> is full and -1 in case of any error. */
1473static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
1474{
1475 static struct ist prefix = IST("haproxy_server_");
1476 struct proxy *px;
1477 struct server *sv;
1478 struct field metric;
1479 struct channel *chn = si_ic(appctx->owner);
1480 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001481 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001482 int ret = 1;
1483 uint32_t weight;
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001484 double secs;
Christopher Fauletf959d082019-02-07 15:38:42 +01001485
1486 while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001487 while (appctx->ctx.stats.obj1) {
1488 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001489
1490 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001491 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +01001492 goto next_px;
1493
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001494 while (appctx->ctx.stats.obj2) {
1495 sv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +01001496
Christopher Fauleteba22942019-11-19 14:18:24 +01001497 if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
1498 goto next_sv;
1499
Christopher Fauletf959d082019-02-07 15:38:42 +01001500 switch (appctx->st2) {
1501 case ST_F_STATUS:
1502 metric = mkf_u32(FO_STATUS, promex_srv_status(sv));
1503 break;
1504 case ST_F_SCUR:
1505 metric = mkf_u32(0, sv->cur_sess);
1506 break;
1507 case ST_F_SMAX:
1508 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1509 break;
1510 case ST_F_SLIM:
1511 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1512 break;
1513 case ST_F_STOT:
1514 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1515 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001516 case ST_F_RATE_MAX:
1517 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
1518 break;
1519 case ST_F_LASTSESS:
1520 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
1521 break;
1522 case ST_F_QCUR:
1523 metric = mkf_u32(0, sv->nbpend);
1524 break;
1525 case ST_F_QMAX:
1526 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1527 break;
1528 case ST_F_QLIMIT:
1529 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1530 break;
1531 case ST_F_BIN:
1532 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1533 break;
1534 case ST_F_BOUT:
1535 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1536 break;
1537 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001538 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
1539 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001540 break;
1541 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001542 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
1543 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001544 break;
1545 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001546 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
1547 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001548 break;
1549 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001550 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
1551 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001552 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001553 case ST_F_QT_MAX:
1554 secs = (double)sv->counters.qtime_max / 1000.0;
1555 metric = mkf_flt(FN_MAX, secs);
1556 break;
1557 case ST_F_CT_MAX:
1558 secs = (double)sv->counters.ctime_max / 1000.0;
1559 metric = mkf_flt(FN_MAX, secs);
1560 break;
1561 case ST_F_RT_MAX:
1562 secs = (double)sv->counters.dtime_max / 1000.0;
1563 metric = mkf_flt(FN_MAX, secs);
1564 break;
1565 case ST_F_TT_MAX:
1566 secs = (double)sv->counters.ttime_max / 1000.0;
1567 metric = mkf_flt(FN_MAX, secs);
1568 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001569 case ST_F_CONNECT:
1570 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
1571 break;
1572 case ST_F_REUSE:
1573 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
1574 break;
1575 case ST_F_DRESP:
Christopher Fauleta08546b2019-12-16 16:07:34 +01001576 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
Christopher Fauletf959d082019-02-07 15:38:42 +01001577 break;
1578 case ST_F_ECON:
1579 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1580 break;
1581 case ST_F_ERESP:
1582 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1583 break;
1584 case ST_F_WRETR:
1585 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
1586 break;
1587 case ST_F_WREDIS:
1588 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
1589 break;
1590 case ST_F_WREW:
1591 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
1592 break;
Christopher Faulete4a2c8d2019-12-16 14:44:01 +01001593 case ST_F_EINT:
1594 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
1595 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001596 case ST_F_CLI_ABRT:
1597 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1598 break;
1599 case ST_F_SRV_ABRT:
1600 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1601 break;
1602 case ST_F_WEIGHT:
1603 weight = (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
1604 metric = mkf_u32(FN_AVG, weight);
1605 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001606 case ST_F_CHECK_STATUS:
1607 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1608 goto next_sv;
1609 metric = mkf_u32(FN_OUTPUT, sv->check.status);
1610 break;
1611 case ST_F_CHECK_CODE:
1612 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1613 goto next_sv;
1614 metric = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
1615 break;
Christopher Faulet2711e512020-02-27 16:12:07 +01001616 case ST_F_CHECK_DURATION:
1617 if (sv->check.status < HCHK_STATUS_CHECKED)
1618 goto next_sv;
1619 secs = (double)sv->check.duration / 1000.0;
1620 metric = mkf_flt(FN_DURATION, secs);
1621 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001622 case ST_F_CHKFAIL:
1623 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1624 break;
1625 case ST_F_CHKDOWN:
1626 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1627 break;
1628 case ST_F_DOWNTIME:
1629 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
1630 break;
1631 case ST_F_LASTCHG:
1632 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
1633 break;
1634 case ST_F_THROTTLE:
1635 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
1636 break;
1637 case ST_F_LBTOT:
1638 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1639 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001640 case ST_F_REQ_TOT:
1641 if (px->mode != PR_MODE_HTTP)
1642 goto next_px;
1643 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
1644 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001645 case ST_F_HRSP_1XX:
1646 if (px->mode != PR_MODE_HTTP)
1647 goto next_px;
1648 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1649 break;
1650 case ST_F_HRSP_2XX:
1651 if (px->mode != PR_MODE_HTTP)
1652 goto next_px;
1653 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1654 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1655 break;
1656 case ST_F_HRSP_3XX:
1657 if (px->mode != PR_MODE_HTTP)
1658 goto next_px;
1659 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1660 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1661 break;
1662 case ST_F_HRSP_4XX:
1663 if (px->mode != PR_MODE_HTTP)
1664 goto next_px;
1665 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1666 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1667 break;
1668 case ST_F_HRSP_5XX:
1669 if (px->mode != PR_MODE_HTTP)
1670 goto next_px;
1671 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1672 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1673 break;
1674 case ST_F_HRSP_OTHER:
1675 if (px->mode != PR_MODE_HTTP)
1676 goto next_px;
1677 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1678 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1679 break;
Christopher Faulet20ab80c2019-11-08 15:24:32 +01001680 case ST_F_SRV_ICUR:
1681 metric = mkf_u32(0, sv->curr_idle_conns);
1682 break;
1683 case ST_F_SRV_ILIM:
1684 metric = mkf_u32(FO_CONFIG|FN_LIMIT, (sv->max_idle_conns == -1) ? 0 : sv->max_idle_conns);
1685 break;
Christopher Fauletc55a6262020-07-10 15:39:39 +02001686 case ST_F_IDLE_CONN_CUR:
1687 metric = mkf_u32(0, sv->curr_idle_nb);
1688 break;
1689 case ST_F_SAFE_CONN_CUR:
1690 metric = mkf_u32(0, sv->curr_safe_nb);
1691 break;
1692 case ST_F_USED_CONN_CUR:
1693 metric = mkf_u32(0, sv->curr_used_conns);
1694 break;
1695 case ST_F_NEED_CONN_EST:
1696 metric = mkf_u32(0, sv->est_need_conns);
1697 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001698
1699 default:
1700 goto next_metric;
1701 }
1702
1703 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1704 goto full;
1705
Christopher Fauleteba22942019-11-19 14:18:24 +01001706 next_sv:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001707 appctx->ctx.stats.obj2 = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001708 }
1709
1710 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001711 appctx->ctx.stats.obj1 = px->next;
1712 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001713 }
1714 next_metric:
1715 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001716 appctx->ctx.stats.obj1 = proxies_list;
1717 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001718 appctx->st2 = promex_srv_metrics[appctx->st2];
1719 }
1720
1721
1722 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001723 if (out.len) {
1724 if (!htx_add_data_atonce(htx, out))
1725 return -1; /* Unexpected and unrecoverable error */
1726 channel_add_input(chn, out.len);
1727 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001728 return ret;
1729 full:
1730 ret = 0;
1731 goto end;
1732}
1733
1734/* Dump all metrics (global, frontends, backends and servers) depending on the
1735 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001736 * -1 in case of any error.
1737 * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
1738 * a pointer to the current server/listener. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001739static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1740{
1741 int ret;
1742
1743 switch (appctx->st1) {
1744 case PROMEX_DUMPER_INIT:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001745 appctx->ctx.stats.obj1 = NULL;
1746 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001747 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001748 appctx->st2 = promex_global_metrics[INF_NAME];
1749 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1750 /* fall through */
1751
1752 case PROMEX_DUMPER_GLOBAL:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001753 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) {
1754 ret = promex_dump_global_metrics(appctx, htx);
1755 if (ret <= 0) {
1756 if (ret == -1)
1757 goto error;
1758 goto full;
1759 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001760 }
1761
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001762 appctx->ctx.stats.obj1 = proxies_list;
1763 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001764 appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
1765 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_STATS_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001766 appctx->st2 = promex_front_metrics[ST_F_PXNAME];
1767 appctx->st1 = PROMEX_DUMPER_FRONT;
1768 /* fall through */
1769
1770 case PROMEX_DUMPER_FRONT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001771 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) {
1772 ret = promex_dump_front_metrics(appctx, htx);
1773 if (ret <= 0) {
1774 if (ret == -1)
1775 goto error;
1776 goto full;
1777 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001778 }
1779
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001780 appctx->ctx.stats.obj1 = proxies_list;
1781 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001782 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +01001783 appctx->st2 = promex_back_metrics[ST_F_PXNAME];
1784 appctx->st1 = PROMEX_DUMPER_BACK;
1785 /* fall through */
1786
1787 case PROMEX_DUMPER_BACK:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001788 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) {
1789 ret = promex_dump_back_metrics(appctx, htx);
1790 if (ret <= 0) {
1791 if (ret == -1)
1792 goto error;
1793 goto full;
1794 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001795 }
1796
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001797 appctx->ctx.stats.obj1 = proxies_list;
1798 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletefde9552020-06-05 08:18:56 +02001799 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +01001800 appctx->st2 = promex_srv_metrics[ST_F_PXNAME];
1801 appctx->st1 = PROMEX_DUMPER_SRV;
1802 /* fall through */
1803
1804 case PROMEX_DUMPER_SRV:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001805 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) {
1806 ret = promex_dump_srv_metrics(appctx, htx);
1807 if (ret <= 0) {
1808 if (ret == -1)
1809 goto error;
1810 goto full;
1811 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001812 }
1813
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001814 appctx->ctx.stats.obj1 = NULL;
1815 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001816 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC|PROMEX_FL_STATS_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001817 appctx->st2 = 0;
1818 appctx->st1 = PROMEX_DUMPER_DONE;
1819 /* fall through */
1820
1821 case PROMEX_DUMPER_DONE:
1822 default:
1823 break;
1824 }
1825
1826 return 1;
1827
1828 full:
1829 si_rx_room_blk(si);
1830 return 0;
1831 error:
1832 /* unrecoverable error */
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001833 appctx->ctx.stats.obj1 = NULL;
1834 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletf959d082019-02-07 15:38:42 +01001835 appctx->ctx.stats.flags = 0;
1836 appctx->st2 = 0;
1837 appctx->st1 = PROMEX_DUMPER_DONE;
1838 return -1;
1839}
1840
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001841/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001842 * success and -1 on error. */
1843static int promex_parse_uri(struct appctx *appctx, struct stream_interface *si)
1844{
1845 struct channel *req = si_oc(si);
1846 struct channel *res = si_ic(si);
1847 struct htx *req_htx, *res_htx;
1848 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001849 char *p, *key, *value;
1850 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001851 struct buffer *err;
1852 int default_scopes = PROMEX_FL_SCOPE_ALL;
1853 int len;
1854
1855 /* Get the query-string */
1856 req_htx = htxbuf(&req->buf);
1857 sl = http_get_stline(req_htx);
1858 if (!sl)
1859 goto error;
1860 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1861 if (!p)
1862 goto end;
1863 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001864
William Dauchyc65f6562019-11-26 12:56:26 +01001865 /* copy the query-string */
1866 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001867 chunk_reset(&trash);
1868 memcpy(trash.area, p, len);
1869 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001870 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001871 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001872
1873 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001874 while (p < end && *p && *p != '#') {
1875 value = NULL;
1876
1877 /* decode parameter name */
1878 key = p;
1879 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001880 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001881 /* found a value */
1882 if (*p == '=') {
1883 *(p++) = 0;
1884 value = p;
1885 }
1886 else if (*p == '&')
1887 *(p++) = 0;
1888 else if (*p == '#')
1889 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001890 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001891 if (len == -1)
1892 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001893
William Dauchyc65f6562019-11-26 12:56:26 +01001894 /* decode value */
1895 if (value) {
1896 while (p < end && *p != '=' && *p != '&' && *p != '#')
1897 ++p;
1898 if (*p == '=')
1899 goto error;
1900 if (*p == '&')
1901 *(p++) = 0;
1902 else if (*p == '#')
1903 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001904 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001905 if (len == -1)
1906 goto error;
1907 }
1908
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001909 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001910 default_scopes = 0; /* at least a scope defined, unset default scopes */
1911 if (!value)
1912 goto error;
1913 else if (*value == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001914 appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001915 else if (*value == '*')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001916 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001917 else if (strcmp(value, "global") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001918 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001919 else if (strcmp(value, "server") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001920 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001921 else if (strcmp(value, "backend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001922 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001923 else if (strcmp(value, "frontend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001924 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT;
1925 else
1926 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001927 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001928 else if (strcmp(key, "no-maint") == 0)
Christopher Fauleteba22942019-11-19 14:18:24 +01001929 appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001930 }
1931
1932 end:
1933 appctx->ctx.stats.flags |= default_scopes;
1934 return 1;
1935
1936 error:
1937 err = &http_err_chunks[HTTP_ERR_400];
1938 channel_erase(res);
1939 res->buf.data = b_data(err);
1940 memcpy(res->buf.area, b_head(err), b_data(err));
1941 res_htx = htx_from_buf(&res->buf);
1942 channel_add_input(res, res_htx->data);
1943 appctx->st0 = PROMEX_ST_END;
1944 return -1;
1945}
1946
Christopher Fauletf959d082019-02-07 15:38:42 +01001947/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1948 * full. */
1949static int promex_send_headers(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1950{
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001951 struct channel *chn = si_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +01001952 struct htx_sl *sl;
1953 unsigned int flags;
1954
1955 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);
1956 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1957 if (!sl)
1958 goto full;
1959 sl->info.res.status = 200;
1960 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001961 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1962 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1963 !htx_add_endof(htx, HTX_BLK_EOH))
1964 goto full;
1965
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001966 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001967 return 1;
1968 full:
1969 htx_reset(htx);
1970 si_rx_room_blk(si);
1971 return 0;
1972}
1973
1974/* The function returns 1 if the initialisation is complete, 0 if
1975 * an errors occurs and -1 if more data are required for initializing
1976 * the applet.
1977 */
1978static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm)
1979{
1980 appctx->st0 = PROMEX_ST_INIT;
1981 return 1;
1982}
1983
1984/* The main I/O handler for the promex applet. */
1985static void promex_appctx_handle_io(struct appctx *appctx)
1986{
1987 struct stream_interface *si = appctx->owner;
1988 struct stream *s = si_strm(si);
1989 struct channel *req = si_oc(si);
1990 struct channel *res = si_ic(si);
1991 struct htx *req_htx, *res_htx;
1992 int ret;
1993
1994 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf959d082019-02-07 15:38:42 +01001995 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1996 goto out;
1997
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001998 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001999 if (!b_size(&res->buf)) {
2000 si_rx_room_blk(si);
2001 goto out;
2002 }
2003
2004 switch (appctx->st0) {
2005 case PROMEX_ST_INIT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01002006 ret = promex_parse_uri(appctx, si);
2007 if (ret <= 0) {
2008 if (ret == -1)
2009 goto error;
2010 goto out;
2011 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002012 appctx->st0 = PROMEX_ST_HEAD;
2013 appctx->st1 = PROMEX_DUMPER_INIT;
2014 /* fall through */
2015
2016 case PROMEX_ST_HEAD:
2017 if (!promex_send_headers(appctx, si, res_htx))
2018 goto out;
2019 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
2020 /* fall through */
2021
2022 case PROMEX_ST_DUMP:
2023 ret = promex_dump_metrics(appctx, si, res_htx);
2024 if (ret <= 0) {
2025 if (ret == -1)
2026 goto error;
2027 goto out;
2028 }
2029 appctx->st0 = PROMEX_ST_DONE;
2030 /* fall through */
2031
2032 case PROMEX_ST_DONE:
Christopher Faulet54b5e212019-06-04 10:08:28 +02002033 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02002034 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletf959d082019-02-07 15:38:42 +01002035 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
2036 si_rx_room_blk(si);
2037 goto out;
2038 }
Christopher Faulet9744f7c2019-03-27 15:48:53 +01002039 channel_add_input(res, 1);
2040 appctx->st0 = PROMEX_ST_END;
2041 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01002042
Christopher Faulet9744f7c2019-03-27 15:48:53 +01002043 case PROMEX_ST_END:
2044 if (!(res->flags & CF_SHUTR)) {
2045 res->flags |= CF_READ_NULL;
2046 si_shutr(si);
2047 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002048 }
2049
Christopher Fauletf959d082019-02-07 15:38:42 +01002050 out:
2051 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01002052
2053 /* eat the whole request */
2054 if (co_data(req)) {
2055 req_htx = htx_from_buf(&req->buf);
2056 co_htx_skip(req, req_htx, co_data(req));
2057 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002058 return;
2059
2060 error:
2061 res->flags |= CF_READ_NULL;
2062 si_shutr(si);
2063 si_shutw(si);
2064}
2065
2066struct applet promex_applet = {
2067 .obj_type = OBJ_TYPE_APPLET,
2068 .name = "<PROMEX>", /* used for logging */
2069 .init = promex_appctx_init,
2070 .fct = promex_appctx_handle_io,
2071};
2072
2073static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
2074 struct act_rule *rule, char **err)
2075{
2076 /* Prometheus exporter service is only available on "http-request" rulesets */
2077 if (rule->from != ACT_F_HTTP_REQ) {
2078 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
2079 return ACT_RET_PRS_ERR;
2080 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002081
2082 /* Add applet pointer in the rule. */
2083 rule->applet = promex_applet;
2084
2085 return ACT_RET_PRS_OK;
2086}
2087static void promex_register_build_options(void)
2088{
2089 char *ptr = NULL;
2090
2091 memprintf(&ptr, "Built with the Prometheus exporter as a service");
2092 hap_register_build_opts(ptr, 1);
2093}
2094
2095
2096static struct action_kw_list service_actions = { ILH, {
2097 { "prometheus-exporter", service_parse_prometheus_exporter },
2098 { /* END */ }
2099}};
2100
2101INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
2102INITCALL0(STG_REGISTER, promex_register_build_options);