blob: 2dcbb9d7eeaff8b6fe2461c4dd9dc48cf0e1cf00 [file] [log] [blame]
Christopher Fauletf959d082019-02-07 15:38:42 +01001/*
2 * Promex is a Prometheus exporter for HAProxy
3 *
4 * It is highly inspired by the official Prometheus exporter.
5 * See: https://github.com/prometheus/haproxy_exporter
6 *
7 * Copyright 2019 Christopher Faulet <cfaulet@haproxy.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020018#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020019#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/compression.h>
Christopher Fauletc55a6262020-07-10 15:39:39 +020022#include <haproxy/dns.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020023#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020024#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020025#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020029#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020031#include <haproxy/pipe.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020034#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020036#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020039#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020040#include <haproxy/task.h>
William Dauchy5a982a72021-01-08 13:18:06 +010041#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010042
43/* Prometheus exporter applet states (appctx->st0) */
44enum {
45 PROMEX_ST_INIT = 0, /* initialized */
46 PROMEX_ST_HEAD, /* send headers before dump */
47 PROMEX_ST_DUMP, /* dumping stats */
48 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010049 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010050};
51
52/* Prometheus exporter dumper states (appctx->st1) */
53enum {
54 PROMEX_DUMPER_INIT = 0, /* initialized */
55 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
56 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
57 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
58 PROMEX_DUMPER_LI, /* dump metrics of listeners */
59 PROMEX_DUMPER_SRV, /* dump metrics of servers */
60 PROMEX_DUMPER_DONE, /* finished */
61};
62
63/* Prometheus exporter flags (appctx->ctx.stats.flags) */
64#define PROMEX_FL_METRIC_HDR 0x00000001
65#define PROMEX_FL_INFO_METRIC 0x00000002
Christopher Fauletb713c4f2021-01-20 15:02:50 +010066#define PROMEX_FL_FRONT_METRIC 0x00000004
67#define PROMEX_FL_BACK_METRIC 0x00000008
68#define PROMEX_FL_SRV_METRIC 0x00000010
69#define PROMEX_FL_SCOPE_GLOBAL 0x00000020
70#define PROMEX_FL_SCOPE_FRONT 0x00000040
71#define PROMEX_FL_SCOPE_BACK 0x00000080
72#define PROMEX_FL_SCOPE_SERVER 0x00000100
73#define PROMEX_FL_NO_MAINT_SRV 0x00000200
Christopher Faulet78407ce2019-11-18 14:47:08 +010074
75#define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL|PROMEX_FL_SCOPE_FRONT|PROMEX_FL_SCOPE_BACK|PROMEX_FL_SCOPE_SERVER)
Christopher Fauletf959d082019-02-07 15:38:42 +010076
Christopher Faulet0312c0d2021-01-20 15:19:12 +010077/* Promtheus metric type (gauge or counter) */
78enum promex_mt_type {
79 PROMEX_MT_GAUGE = 1,
80 PROMEX_MT_COUNTER = 2,
81};
82
Christopher Fauletf959d082019-02-07 15:38:42 +010083/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +050084 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +010085 */
86#define PROMEX_MAX_NAME_LEN 128
87
88/* The expected max length for a metric dump, including its header lines. It is
89 * just a soft limit to avoid extra work. We don't try to dump a metric if less
90 * than this size is available in the HTX.
91 */
92#define PROMEX_MAX_METRIC_LENGTH 512
93
William Dauchy5a982a72021-01-08 13:18:06 +010094/* Some labels for build_info */
95#define PROMEX_VERSION_LABEL "version=\"" HAPROXY_VERSION "\""
96#define PROMEX_BUILDINFO_LABEL PROMEX_VERSION_LABEL
97
Christopher Faulet0312c0d2021-01-20 15:19:12 +010098/* Describe a prometheus metric */
99struct promex_metric {
100 const struct ist n; /* The metric name */
101 enum promex_mt_type type; /* The metric type (gauge or counter) */
102 unsigned int flags; /* PROMEX_FL_* flags */
103};
104
Christopher Fauletf959d082019-02-07 15:38:42 +0100105/* Matrix used to dump global metrics. Each metric points to the next one to be
106 * processed or 0 to stop the dump. */
107const int promex_global_metrics[INF_TOTAL_FIELDS] = {
William Dauchy5a982a72021-01-08 13:18:06 +0100108 [INF_NAME] = INF_BUILD_INFO,
Christopher Fauletf959d082019-02-07 15:38:42 +0100109 [INF_VERSION] = 0,
110 [INF_RELEASE_DATE] = 0,
William Dauchy5a982a72021-01-08 13:18:06 +0100111 [INF_BUILD_INFO] = INF_NBTHREAD,
Christopher Fauletf959d082019-02-07 15:38:42 +0100112 [INF_NBTHREAD] = INF_NBPROC,
113 [INF_NBPROC] = INF_PROCESS_NUM,
114 [INF_PROCESS_NUM] = INF_UPTIME_SEC,
115 [INF_PID] = 0,
116 [INF_UPTIME] = 0,
William Dauchydefd1562021-01-15 22:41:38 +0100117 [INF_UPTIME_SEC] = INF_START_TIME_SEC,
118 [INF_START_TIME_SEC] = INF_MEMMAX_BYTES,
William Dauchya8766cf2021-01-15 22:41:37 +0100119 [INF_MEMMAX_BYTES] = INF_POOL_ALLOC_BYTES,
120 [INF_POOL_ALLOC_BYTES] = INF_POOL_USED_BYTES,
121 [INF_POOL_USED_BYTES] = INF_POOL_FAILED,
Christopher Fauletf959d082019-02-07 15:38:42 +0100122 [INF_POOL_FAILED] = INF_ULIMIT_N,
123 [INF_ULIMIT_N] = INF_MAXSOCK,
124 [INF_MAXSOCK] = INF_MAXCONN,
125 [INF_MAXCONN] = INF_HARD_MAXCONN,
126 [INF_HARD_MAXCONN] = INF_CURR_CONN,
127 [INF_CURR_CONN] = INF_CUM_CONN,
128 [INF_CUM_CONN] = INF_CUM_REQ,
129 [INF_CUM_REQ] = INF_MAX_SSL_CONNS,
130 [INF_MAX_SSL_CONNS] = INF_CURR_SSL_CONNS,
131 [INF_CURR_SSL_CONNS] = INF_CUM_SSL_CONNS,
132 [INF_CUM_SSL_CONNS] = INF_MAXPIPES,
133 [INF_MAXPIPES] = INF_PIPES_USED,
134 [INF_PIPES_USED] = INF_PIPES_FREE,
135 [INF_PIPES_FREE] = INF_CONN_RATE,
136 [INF_CONN_RATE] = INF_CONN_RATE_LIMIT,
137 [INF_CONN_RATE_LIMIT] = INF_MAX_CONN_RATE,
138 [INF_MAX_CONN_RATE] = INF_SESS_RATE,
139 [INF_SESS_RATE] = INF_SESS_RATE_LIMIT,
140 [INF_SESS_RATE_LIMIT] = INF_MAX_SESS_RATE,
141 [INF_MAX_SESS_RATE] = INF_SSL_RATE,
142 [INF_SSL_RATE] = INF_SSL_RATE_LIMIT,
143 [INF_SSL_RATE_LIMIT] = INF_MAX_SSL_RATE,
144 [INF_MAX_SSL_RATE] = INF_SSL_FRONTEND_KEY_RATE,
145 [INF_SSL_FRONTEND_KEY_RATE] = INF_SSL_FRONTEND_MAX_KEY_RATE,
146 [INF_SSL_FRONTEND_MAX_KEY_RATE] = INF_SSL_FRONTEND_SESSION_REUSE_PCT,
147 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = INF_SSL_BACKEND_KEY_RATE,
148 [INF_SSL_BACKEND_KEY_RATE] = INF_SSL_BACKEND_MAX_KEY_RATE,
149 [INF_SSL_BACKEND_MAX_KEY_RATE] = INF_SSL_CACHE_LOOKUPS,
150 [INF_SSL_CACHE_LOOKUPS] = INF_SSL_CACHE_MISSES,
151 [INF_SSL_CACHE_MISSES] = INF_COMPRESS_BPS_IN,
152 [INF_COMPRESS_BPS_IN] = INF_COMPRESS_BPS_OUT,
153 [INF_COMPRESS_BPS_OUT] = INF_COMPRESS_BPS_RATE_LIM,
154 [INF_COMPRESS_BPS_RATE_LIM] = INF_ZLIB_MEM_USAGE,
155 [INF_ZLIB_MEM_USAGE] = INF_MAX_ZLIB_MEM_USAGE,
156 [INF_MAX_ZLIB_MEM_USAGE] = INF_TASKS,
157 [INF_TASKS] = INF_RUN_QUEUE,
158 [INF_RUN_QUEUE] = INF_IDLE_PCT,
159 [INF_IDLE_PCT] = INF_STOPPING,
160 [INF_NODE] = 0,
161 [INF_DESCRIPTION] = 0,
162 [INF_STOPPING] = INF_JOBS,
163 [INF_JOBS] = INF_UNSTOPPABLE_JOBS,
164 [INF_UNSTOPPABLE_JOBS] = INF_LISTENERS,
165 [INF_LISTENERS] = INF_ACTIVE_PEERS,
166 [INF_ACTIVE_PEERS] = INF_CONNECTED_PEERS,
167 [INF_CONNECTED_PEERS] = INF_DROPPED_LOGS,
168 [INF_DROPPED_LOGS] = INF_BUSY_POLLING,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200169 [INF_BUSY_POLLING] = INF_FAILED_RESOLUTIONS,
170 [INF_FAILED_RESOLUTIONS] = INF_TOTAL_BYTES_OUT,
171 [INF_TOTAL_BYTES_OUT] = INF_TOTAL_SPLICED_BYTES_OUT,
172 [INF_TOTAL_SPLICED_BYTES_OUT] = INF_BYTES_OUT_RATE,
173 [INF_BYTES_OUT_RATE] = 0,
174 [INF_DEBUG_COMMANDS_ISSUED] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100175};
176
177/* Matrix used to dump frontend metrics. Each metric points to the next one to be
178 * processed or 0 to stop the dump. */
179const int promex_front_metrics[ST_F_TOTAL_FIELDS] = {
180 [ST_F_PXNAME] = ST_F_STATUS,
181 [ST_F_SVNAME] = 0,
182 [ST_F_QCUR] = 0,
183 [ST_F_QMAX] = 0,
184 [ST_F_SCUR] = ST_F_SMAX,
185 [ST_F_SMAX] = ST_F_SLIM,
186 [ST_F_SLIM] = ST_F_STOT,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200187 [ST_F_STOT] = ST_F_RATE_LIM,
Christopher Fauletf959d082019-02-07 15:38:42 +0100188 [ST_F_BIN] = ST_F_BOUT,
189 [ST_F_BOUT] = ST_F_DREQ,
190 [ST_F_DREQ] = ST_F_DRESP,
191 [ST_F_DRESP] = ST_F_EREQ,
192 [ST_F_EREQ] = ST_F_DCON,
193 [ST_F_ECON] = 0,
194 [ST_F_ERESP] = 0,
195 [ST_F_WRETR] = 0,
196 [ST_F_WREDIS] = 0,
197 [ST_F_STATUS] = ST_F_SCUR,
198 [ST_F_WEIGHT] = 0,
199 [ST_F_ACT] = 0,
200 [ST_F_BCK] = 0,
201 [ST_F_CHKFAIL] = 0,
202 [ST_F_CHKDOWN] = 0,
203 [ST_F_LASTCHG] = 0,
204 [ST_F_DOWNTIME] = 0,
205 [ST_F_QLIMIT] = 0,
206 [ST_F_PID] = 0,
207 [ST_F_IID] = 0,
208 [ST_F_SID] = 0,
209 [ST_F_THROTTLE] = 0,
210 [ST_F_LBTOT] = 0,
211 [ST_F_TRACKED] = 0,
212 [ST_F_TYPE] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200213 [ST_F_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100214 [ST_F_RATE_LIM] = ST_F_RATE_MAX,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200215 [ST_F_RATE_MAX] = ST_F_CONN_RATE_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100216 [ST_F_CHECK_STATUS] = 0,
217 [ST_F_CHECK_CODE] = 0,
218 [ST_F_CHECK_DURATION] = 0,
219 [ST_F_HRSP_1XX] = ST_F_HRSP_2XX,
220 [ST_F_HRSP_2XX] = ST_F_HRSP_3XX,
221 [ST_F_HRSP_3XX] = ST_F_HRSP_4XX,
222 [ST_F_HRSP_4XX] = ST_F_HRSP_5XX,
223 [ST_F_HRSP_5XX] = ST_F_HRSP_OTHER,
224 [ST_F_HRSP_OTHER] = ST_F_INTERCEPTED,
225 [ST_F_HANAFAIL] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200226 [ST_F_REQ_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100227 [ST_F_REQ_RATE_MAX] = ST_F_REQ_TOT,
228 [ST_F_REQ_TOT] = ST_F_HRSP_1XX,
229 [ST_F_CLI_ABRT] = 0,
230 [ST_F_SRV_ABRT] = 0,
231 [ST_F_COMP_IN] = ST_F_COMP_OUT,
232 [ST_F_COMP_OUT] = ST_F_COMP_BYP,
233 [ST_F_COMP_BYP] = ST_F_COMP_RSP,
234 [ST_F_COMP_RSP] = 0,
235 [ST_F_LASTSESS] = 0,
236 [ST_F_LAST_CHK] = 0,
237 [ST_F_LAST_AGT] = 0,
238 [ST_F_QTIME] = 0,
239 [ST_F_CTIME] = 0,
240 [ST_F_RTIME] = 0,
241 [ST_F_TTIME] = 0,
242 [ST_F_AGENT_STATUS] = 0,
243 [ST_F_AGENT_CODE] = 0,
244 [ST_F_AGENT_DURATION] = 0,
245 [ST_F_CHECK_DESC] = 0,
246 [ST_F_AGENT_DESC] = 0,
247 [ST_F_CHECK_RISE] = 0,
248 [ST_F_CHECK_FALL] = 0,
249 [ST_F_CHECK_HEALTH] = 0,
250 [ST_F_AGENT_RISE] = 0,
251 [ST_F_AGENT_FALL] = 0,
252 [ST_F_AGENT_HEALTH] = 0,
253 [ST_F_ADDR] = 0,
254 [ST_F_COOKIE] = 0,
255 [ST_F_MODE] = 0,
256 [ST_F_ALGO] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200257 [ST_F_CONN_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100258 [ST_F_CONN_RATE_MAX] = ST_F_CONN_TOT,
259 [ST_F_CONN_TOT] = ST_F_BIN,
260 [ST_F_INTERCEPTED] = ST_F_CACHE_LOOKUPS,
261 [ST_F_DCON] = ST_F_DSES,
262 [ST_F_DSES] = ST_F_WREW,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100263 [ST_F_WREW] = ST_F_EINT,
Christopher Fauletf959d082019-02-07 15:38:42 +0100264 [ST_F_CONNECT] = 0,
265 [ST_F_REUSE] = 0,
266 [ST_F_CACHE_LOOKUPS] = ST_F_CACHE_HITS,
267 [ST_F_CACHE_HITS] = ST_F_COMP_IN,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100268 [ST_F_SRV_ICUR] = 0,
269 [ST_F_SRV_ILIM] = 0,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100270 [ST_F_QT_MAX] = 0,
271 [ST_F_CT_MAX] = 0,
272 [ST_F_RT_MAX] = 0,
273 [ST_F_TT_MAX] = 0,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100274 [ST_F_EINT] = ST_F_REQ_RATE_MAX,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200275 [ST_F_IDLE_CONN_CUR] = 0,
276 [ST_F_SAFE_CONN_CUR] = 0,
277 [ST_F_USED_CONN_CUR] = 0,
278 [ST_F_NEED_CONN_EST] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100279};
280
281/* Matrix used to dump backend metrics. Each metric points to the next one to be
282 * processed or 0 to stop the dump. */
283const int promex_back_metrics[ST_F_TOTAL_FIELDS] = {
284 [ST_F_PXNAME] = ST_F_STATUS,
285 [ST_F_SVNAME] = 0,
286 [ST_F_QCUR] = ST_F_QMAX,
287 [ST_F_QMAX] = ST_F_CONNECT,
288 [ST_F_SCUR] = ST_F_SMAX,
289 [ST_F_SMAX] = ST_F_SLIM,
290 [ST_F_SLIM] = ST_F_STOT,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200291 [ST_F_STOT] = ST_F_RATE_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100292 [ST_F_BIN] = ST_F_BOUT,
293 [ST_F_BOUT] = ST_F_QTIME,
294 [ST_F_DREQ] = ST_F_DRESP,
295 [ST_F_DRESP] = ST_F_ECON,
296 [ST_F_EREQ] = 0,
297 [ST_F_ECON] = ST_F_ERESP,
298 [ST_F_ERESP] = ST_F_WRETR,
299 [ST_F_WRETR] = ST_F_WREDIS,
300 [ST_F_WREDIS] = ST_F_WREW,
301 [ST_F_STATUS] = ST_F_SCUR,
302 [ST_F_WEIGHT] = ST_F_ACT,
303 [ST_F_ACT] = ST_F_BCK,
304 [ST_F_BCK] = ST_F_CHKDOWN,
305 [ST_F_CHKFAIL] = 0,
306 [ST_F_CHKDOWN] = ST_F_LASTCHG,
307 [ST_F_LASTCHG] = ST_F_DOWNTIME,
308 [ST_F_DOWNTIME] = ST_F_LBTOT,
309 [ST_F_QLIMIT] = 0,
310 [ST_F_PID] = 0,
311 [ST_F_IID] = 0,
312 [ST_F_SID] = 0,
313 [ST_F_THROTTLE] = 0,
314 [ST_F_LBTOT] = ST_F_REQ_TOT,
315 [ST_F_TRACKED] = 9,
316 [ST_F_TYPE] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200317 [ST_F_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100318 [ST_F_RATE_LIM] = 0,
319 [ST_F_RATE_MAX] = ST_F_LASTSESS,
320 [ST_F_CHECK_STATUS] = 0,
321 [ST_F_CHECK_CODE] = 0,
322 [ST_F_CHECK_DURATION] = 0,
323 [ST_F_HRSP_1XX] = ST_F_HRSP_2XX,
324 [ST_F_HRSP_2XX] = ST_F_HRSP_3XX,
325 [ST_F_HRSP_3XX] = ST_F_HRSP_4XX,
326 [ST_F_HRSP_4XX] = ST_F_HRSP_5XX,
327 [ST_F_HRSP_5XX] = ST_F_HRSP_OTHER,
328 [ST_F_HRSP_OTHER] = ST_F_CACHE_LOOKUPS,
329 [ST_F_HANAFAIL] = 0,
330 [ST_F_REQ_RATE] = 0,
331 [ST_F_REQ_RATE_MAX] = 0,
332 [ST_F_REQ_TOT] = ST_F_HRSP_1XX,
333 [ST_F_CLI_ABRT] = ST_F_SRV_ABRT,
334 [ST_F_SRV_ABRT] = ST_F_WEIGHT,
335 [ST_F_COMP_IN] = ST_F_COMP_OUT,
336 [ST_F_COMP_OUT] = ST_F_COMP_BYP,
337 [ST_F_COMP_BYP] = ST_F_COMP_RSP,
338 [ST_F_COMP_RSP] = 0,
339 [ST_F_LASTSESS] = ST_F_QCUR,
340 [ST_F_LAST_CHK] = 0,
341 [ST_F_LAST_AGT] = 0,
342 [ST_F_QTIME] = ST_F_CTIME,
343 [ST_F_CTIME] = ST_F_RTIME,
344 [ST_F_RTIME] = ST_F_TTIME,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100345 [ST_F_TTIME] = ST_F_QT_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100346 [ST_F_AGENT_STATUS] = 0,
347 [ST_F_AGENT_CODE] = 0,
348 [ST_F_AGENT_DURATION] = 0,
349 [ST_F_CHECK_DESC] = 0,
350 [ST_F_AGENT_DESC] = 0,
351 [ST_F_CHECK_RISE] = 0,
352 [ST_F_CHECK_FALL] = 0,
353 [ST_F_CHECK_HEALTH] = 0,
354 [ST_F_AGENT_RISE] = 0,
355 [ST_F_AGENT_FALL] = 0,
356 [ST_F_AGENT_HEALTH] = 0,
357 [ST_F_ADDR] = 0,
358 [ST_F_COOKIE] = 0,
359 [ST_F_MODE] = 0,
360 [ST_F_ALGO] = 0,
361 [ST_F_CONN_RATE] = 0,
362 [ST_F_CONN_RATE_MAX] = 0,
363 [ST_F_CONN_TOT] = 0,
364 [ST_F_INTERCEPTED] = 0,
365 [ST_F_DCON] = 0,
366 [ST_F_DSES] = 0,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100367 [ST_F_WREW] = ST_F_EINT,
Christopher Fauletf959d082019-02-07 15:38:42 +0100368 [ST_F_CONNECT] = ST_F_REUSE,
369 [ST_F_REUSE] = ST_F_BIN,
370 [ST_F_CACHE_LOOKUPS] = ST_F_CACHE_HITS,
371 [ST_F_CACHE_HITS] = ST_F_COMP_IN,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100372 [ST_F_SRV_ICUR] = 0,
373 [ST_F_SRV_ILIM] = 0,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100374 [ST_F_QT_MAX] = ST_F_CT_MAX,
375 [ST_F_CT_MAX] = ST_F_RT_MAX,
376 [ST_F_RT_MAX] = ST_F_TT_MAX,
377 [ST_F_TT_MAX] = ST_F_DREQ,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100378 [ST_F_EINT] = ST_F_CLI_ABRT,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200379 [ST_F_IDLE_CONN_CUR] = 0,
380 [ST_F_SAFE_CONN_CUR] = 0,
381 [ST_F_USED_CONN_CUR] = 0,
382 [ST_F_NEED_CONN_EST] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100383};
384
385/* Matrix used to dump server metrics. Each metric points to the next one to be
386 * processed or 0 to stop the dump. */
387const int promex_srv_metrics[ST_F_TOTAL_FIELDS] = {
388 [ST_F_PXNAME] = ST_F_STATUS,
389 [ST_F_SVNAME] = 0,
390 [ST_F_QCUR] = ST_F_QMAX,
391 [ST_F_QMAX] = ST_F_QLIMIT,
392 [ST_F_SCUR] = ST_F_SMAX,
393 [ST_F_SMAX] = ST_F_SLIM,
394 [ST_F_SLIM] = ST_F_STOT,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200395 [ST_F_STOT] = ST_F_RATE_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100396 [ST_F_BIN] = ST_F_BOUT,
397 [ST_F_BOUT] = ST_F_QTIME,
398 [ST_F_DREQ] = 0,
399 [ST_F_DRESP] = ST_F_ECON,
400 [ST_F_EREQ] = 0,
401 [ST_F_ECON] = ST_F_ERESP,
402 [ST_F_ERESP] = ST_F_WRETR,
403 [ST_F_WRETR] = ST_F_WREDIS,
404 [ST_F_WREDIS] = ST_F_WREW,
405 [ST_F_STATUS] = ST_F_SCUR,
Christopher Fauletcf403f32019-11-21 14:35:46 +0100406 [ST_F_WEIGHT] = ST_F_CHECK_STATUS,
Christopher Fauletf959d082019-02-07 15:38:42 +0100407 [ST_F_ACT] = 0,
408 [ST_F_BCK] = 0,
409 [ST_F_CHKFAIL] = ST_F_CHKDOWN,
410 [ST_F_CHKDOWN] = ST_F_DOWNTIME,
411 [ST_F_LASTCHG] = ST_F_THROTTLE,
412 [ST_F_DOWNTIME] = ST_F_LASTCHG,
413 [ST_F_QLIMIT] = ST_F_BIN,
414 [ST_F_PID] = 0,
415 [ST_F_IID] = 0,
416 [ST_F_SID] = 0,
417 [ST_F_THROTTLE] = ST_F_LBTOT,
418 [ST_F_LBTOT] = ST_F_HRSP_1XX,
419 [ST_F_TRACKED] = 0,
420 [ST_F_TYPE] = 0,
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200421 [ST_F_RATE] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100422 [ST_F_RATE_LIM] = 0,
423 [ST_F_RATE_MAX] = ST_F_LASTSESS,
Christopher Fauletcf403f32019-11-21 14:35:46 +0100424 [ST_F_CHECK_STATUS] = ST_F_CHECK_CODE,
Christopher Faulet2711e512020-02-27 16:12:07 +0100425 [ST_F_CHECK_CODE] = ST_F_CHECK_DURATION,
426 [ST_F_CHECK_DURATION] = ST_F_CHKFAIL,
Christopher Fauletf959d082019-02-07 15:38:42 +0100427 [ST_F_HRSP_1XX] = ST_F_HRSP_2XX,
428 [ST_F_HRSP_2XX] = ST_F_HRSP_3XX,
429 [ST_F_HRSP_3XX] = ST_F_HRSP_4XX,
430 [ST_F_HRSP_4XX] = ST_F_HRSP_5XX,
431 [ST_F_HRSP_5XX] = ST_F_HRSP_OTHER,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100432 [ST_F_HRSP_OTHER] = ST_F_SRV_ICUR,
Christopher Fauletf959d082019-02-07 15:38:42 +0100433 [ST_F_HANAFAIL] = 0,
434 [ST_F_REQ_RATE] = 0,
435 [ST_F_REQ_RATE_MAX] = 0,
436 [ST_F_REQ_TOT] = 0,
437 [ST_F_CLI_ABRT] = ST_F_SRV_ABRT,
438 [ST_F_SRV_ABRT] = ST_F_WEIGHT,
439 [ST_F_COMP_IN] = 0,
440 [ST_F_COMP_OUT] = 0,
441 [ST_F_COMP_BYP] = 0,
442 [ST_F_COMP_RSP] = 0,
443 [ST_F_LASTSESS] = ST_F_QCUR,
444 [ST_F_LAST_CHK] = 0,
445 [ST_F_LAST_AGT] = 0,
446 [ST_F_QTIME] = ST_F_CTIME,
447 [ST_F_CTIME] = ST_F_RTIME,
448 [ST_F_RTIME] = ST_F_TTIME,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100449 [ST_F_TTIME] = ST_F_QT_MAX,
Christopher Fauletf959d082019-02-07 15:38:42 +0100450 [ST_F_AGENT_STATUS] = 0,
451 [ST_F_AGENT_CODE] = 0,
452 [ST_F_AGENT_DURATION] = 0,
453 [ST_F_CHECK_DESC] = 0,
454 [ST_F_AGENT_DESC] = 0,
455 [ST_F_CHECK_RISE] = 0,
456 [ST_F_CHECK_FALL] = 0,
457 [ST_F_CHECK_HEALTH] = 0,
458 [ST_F_AGENT_RISE] = 0,
459 [ST_F_AGENT_FALL] = 0,
460 [ST_F_AGENT_HEALTH] = 0,
461 [ST_F_ADDR] = 0,
462 [ST_F_COOKIE] = 0,
463 [ST_F_MODE] = 0,
464 [ST_F_ALGO] = 0,
465 [ST_F_CONN_RATE] = 0,
466 [ST_F_CONN_RATE_MAX] = 0,
467 [ST_F_CONN_TOT] = 0,
468 [ST_F_INTERCEPTED] = 0,
469 [ST_F_DCON] = 0,
470 [ST_F_DSES] = 0,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100471 [ST_F_WREW] = ST_F_EINT,
Christopher Fauletf959d082019-02-07 15:38:42 +0100472 [ST_F_CONNECT] = ST_F_REUSE,
473 [ST_F_REUSE] = ST_F_DRESP,
474 [ST_F_CACHE_LOOKUPS] = 0,
475 [ST_F_CACHE_HITS] = 0,
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100476 [ST_F_SRV_ICUR] = ST_F_SRV_ILIM,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200477 [ST_F_SRV_ILIM] = ST_F_IDLE_CONN_CUR,
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100478 [ST_F_QT_MAX] = ST_F_CT_MAX,
479 [ST_F_CT_MAX] = ST_F_RT_MAX,
480 [ST_F_RT_MAX] = ST_F_TT_MAX,
481 [ST_F_TT_MAX] = ST_F_CONNECT,
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100482 [ST_F_EINT] = ST_F_CLI_ABRT,
Christopher Fauletc55a6262020-07-10 15:39:39 +0200483 [ST_F_IDLE_CONN_CUR] = ST_F_SAFE_CONN_CUR,
484 [ST_F_SAFE_CONN_CUR] = ST_F_USED_CONN_CUR,
485 [ST_F_USED_CONN_CUR] = ST_F_NEED_CONN_EST,
486 [ST_F_NEED_CONN_EST] = 0,
Christopher Fauletf959d082019-02-07 15:38:42 +0100487};
488
489/* Name of all info fields */
490const struct ist promex_inf_metric_names[INF_TOTAL_FIELDS] = {
491 [INF_NAME] = IST("name"),
492 [INF_VERSION] = IST("version"),
493 [INF_RELEASE_DATE] = IST("release_date"),
William Dauchy5a982a72021-01-08 13:18:06 +0100494 [INF_BUILD_INFO] = IST("build_info"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100495 [INF_NBTHREAD] = IST("nbthread"),
496 [INF_NBPROC] = IST("nbproc"),
497 [INF_PROCESS_NUM] = IST("relative_process_id"),
498 [INF_PID] = IST("pid"),
499 [INF_UPTIME] = IST("uptime"),
William Dauchydefd1562021-01-15 22:41:38 +0100500 [INF_UPTIME_SEC] = IST("uptime_seconds"),
501 [INF_START_TIME_SEC] = IST("start_time_seconds"),
William Dauchya8766cf2021-01-15 22:41:37 +0100502 [INF_MEMMAX_BYTES] = IST("max_memory_bytes"),
503 [INF_POOL_ALLOC_BYTES] = IST("pool_allocated_bytes"),
504 [INF_POOL_USED_BYTES] = IST("pool_used_bytes"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100505 [INF_POOL_FAILED] = IST("pool_failures_total"),
506 [INF_ULIMIT_N] = IST("max_fds"),
507 [INF_MAXSOCK] = IST("max_sockets"),
508 [INF_MAXCONN] = IST("max_connections"),
509 [INF_HARD_MAXCONN] = IST("hard_max_connections"),
510 [INF_CURR_CONN] = IST("current_connections"),
511 [INF_CUM_CONN] = IST("connections_total"),
512 [INF_CUM_REQ] = IST("requests_total"),
513 [INF_MAX_SSL_CONNS] = IST("max_ssl_connections"),
514 [INF_CURR_SSL_CONNS] = IST("current_ssl_connections"),
515 [INF_CUM_SSL_CONNS] = IST("ssl_connections_total"),
516 [INF_MAXPIPES] = IST("max_pipes"),
517 [INF_PIPES_USED] = IST("pipes_used_total"),
518 [INF_PIPES_FREE] = IST("pipes_free_total"),
519 [INF_CONN_RATE] = IST("current_connection_rate"),
520 [INF_CONN_RATE_LIMIT] = IST("limit_connection_rate"),
521 [INF_MAX_CONN_RATE] = IST("max_connection_rate"),
522 [INF_SESS_RATE] = IST("current_session_rate"),
523 [INF_SESS_RATE_LIMIT] = IST("limit_session_rate"),
524 [INF_MAX_SESS_RATE] = IST("max_session_rate"),
525 [INF_SSL_RATE] = IST("current_ssl_rate"),
526 [INF_SSL_RATE_LIMIT] = IST("limit_ssl_rate"),
527 [INF_MAX_SSL_RATE] = IST("max_ssl_rate"),
528 [INF_SSL_FRONTEND_KEY_RATE] = IST("current_frontend_ssl_key_rate"),
529 [INF_SSL_FRONTEND_MAX_KEY_RATE] = IST("max_frontend_ssl_key_rate"),
Pierre Cheynier1e369762020-07-07 19:14:08 +0200530 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = IST("frontend_ssl_reuse"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100531 [INF_SSL_BACKEND_KEY_RATE] = IST("current_backend_ssl_key_rate"),
532 [INF_SSL_BACKEND_MAX_KEY_RATE] = IST("max_backend_ssl_key_rate"),
Christopher Faulet8c8e4b12019-04-18 10:15:15 +0200533 [INF_SSL_CACHE_LOOKUPS] = IST("ssl_cache_lookups_total"),
534 [INF_SSL_CACHE_MISSES] = IST("ssl_cache_misses_total"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100535 [INF_COMPRESS_BPS_IN] = IST("http_comp_bytes_in_total"),
536 [INF_COMPRESS_BPS_OUT] = IST("http_comp_bytes_out_total"),
537 [INF_COMPRESS_BPS_RATE_LIM] = IST("limit_http_comp"),
538 [INF_ZLIB_MEM_USAGE] = IST("current_zlib_memory"),
539 [INF_MAX_ZLIB_MEM_USAGE] = IST("max_zlib_memory"),
540 [INF_TASKS] = IST("current_tasks"),
541 [INF_RUN_QUEUE] = IST("current_run_queue"),
542 [INF_IDLE_PCT] = IST("idle_time_percent"),
543 [INF_NODE] = IST("node"),
544 [INF_DESCRIPTION] = IST("description"),
545 [INF_STOPPING] = IST("stopping"),
546 [INF_JOBS] = IST("jobs"),
547 [INF_UNSTOPPABLE_JOBS] = IST("unstoppable_jobs"),
548 [INF_LISTENERS] = IST("listeners"),
549 [INF_ACTIVE_PEERS] = IST("active_peers"),
550 [INF_CONNECTED_PEERS] = IST("connected_peers"),
551 [INF_DROPPED_LOGS] = IST("dropped_logs_total"),
552 [INF_BUSY_POLLING] = IST("busy_polling_enabled"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200553 [INF_FAILED_RESOLUTIONS] = IST("failed_resolutions"),
554 [INF_TOTAL_BYTES_OUT] = IST("bytes_out_total"),
555 [INF_TOTAL_SPLICED_BYTES_OUT] = IST("spliced_bytes_out_total"),
556 [INF_BYTES_OUT_RATE] = IST("bytes_out_rate"),
557 [INF_DEBUG_COMMANDS_ISSUED] = IST("debug_commands_issued"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100558};
559
560/* Name of all stats fields */
561const struct ist promex_st_metric_names[ST_F_TOTAL_FIELDS] = {
562 [ST_F_PXNAME] = IST("proxy_name"),
563 [ST_F_SVNAME] = IST("service_name"),
564 [ST_F_QCUR] = IST("current_queue"),
565 [ST_F_QMAX] = IST("max_queue"),
566 [ST_F_SCUR] = IST("current_sessions"),
567 [ST_F_SMAX] = IST("max_sessions"),
568 [ST_F_SLIM] = IST("limit_sessions"),
569 [ST_F_STOT] = IST("sessions_total"),
570 [ST_F_BIN] = IST("bytes_in_total"),
571 [ST_F_BOUT] = IST("bytes_out_total"),
572 [ST_F_DREQ] = IST("requests_denied_total"),
573 [ST_F_DRESP] = IST("responses_denied_total"),
574 [ST_F_EREQ] = IST("request_errors_total"),
575 [ST_F_ECON] = IST("connection_errors_total"),
576 [ST_F_ERESP] = IST("response_errors_total"),
577 [ST_F_WRETR] = IST("retry_warnings_total"),
578 [ST_F_WREDIS] = IST("redispatch_warnings_total"),
579 [ST_F_STATUS] = IST("status"),
580 [ST_F_WEIGHT] = IST("weight"),
581 [ST_F_ACT] = IST("active_servers"),
582 [ST_F_BCK] = IST("backup_servers"),
583 [ST_F_CHKFAIL] = IST("check_failures_total"),
584 [ST_F_CHKDOWN] = IST("check_up_down_total"),
585 [ST_F_LASTCHG] = IST("check_last_change_seconds"),
586 [ST_F_DOWNTIME] = IST("downtime_seconds_total"),
587 [ST_F_QLIMIT] = IST("queue_limit"),
588 [ST_F_PID] = IST("pid"),
589 [ST_F_IID] = IST("proxy_id"),
590 [ST_F_SID] = IST("server_id"),
591 [ST_F_THROTTLE] = IST("current_throttle"),
592 [ST_F_LBTOT] = IST("loadbalanced_total"),
593 [ST_F_TRACKED] = IST("tracked"),
594 [ST_F_TYPE] = IST("type"),
595 [ST_F_RATE] = IST("current_session_rate"),
596 [ST_F_RATE_LIM] = IST("limit_session_rate"),
597 [ST_F_RATE_MAX] = IST("max_session_rate"),
598 [ST_F_CHECK_STATUS] = IST("check_status"),
599 [ST_F_CHECK_CODE] = IST("check_code"),
Christopher Faulet2711e512020-02-27 16:12:07 +0100600 [ST_F_CHECK_DURATION] = IST("check_duration_seconds"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100601 [ST_F_HRSP_1XX] = IST("http_responses_total"),
602 [ST_F_HRSP_2XX] = IST("http_responses_total"),
603 [ST_F_HRSP_3XX] = IST("http_responses_total"),
604 [ST_F_HRSP_4XX] = IST("http_responses_total"),
605 [ST_F_HRSP_5XX] = IST("http_responses_total"),
606 [ST_F_HRSP_OTHER] = IST("http_responses_total"),
607 [ST_F_HANAFAIL] = IST("check_analyses_failures_total"),
608 [ST_F_REQ_RATE] = IST("http_requests_rate_current"),
609 [ST_F_REQ_RATE_MAX] = IST("http_requests_rate_max"),
610 [ST_F_REQ_TOT] = IST("http_requests_total"),
611 [ST_F_CLI_ABRT] = IST("client_aborts_total"),
612 [ST_F_SRV_ABRT] = IST("server_aborts_total"),
613 [ST_F_COMP_IN] = IST("http_comp_bytes_in_total"),
614 [ST_F_COMP_OUT] = IST("http_comp_bytes_out_total"),
615 [ST_F_COMP_BYP] = IST("http_comp_bytes_bypassed_total"),
616 [ST_F_COMP_RSP] = IST("http_comp_responses_total"),
617 [ST_F_LASTSESS] = IST("last_session_seconds"),
618 [ST_F_LAST_CHK] = IST("check_last_content"),
619 [ST_F_LAST_AGT] = IST("agentcheck_last_content"),
Christopher Faulet68b69682019-11-08 15:12:29 +0100620 [ST_F_QTIME] = IST("queue_time_average_seconds"),
621 [ST_F_CTIME] = IST("connect_time_average_seconds"),
622 [ST_F_RTIME] = IST("response_time_average_seconds"),
623 [ST_F_TTIME] = IST("total_time_average_seconds"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100624 [ST_F_AGENT_STATUS] = IST("agentcheck_status"),
625 [ST_F_AGENT_CODE] = IST("agentcheck_code"),
626 [ST_F_AGENT_DURATION] = IST("agentcheck_duration_milliseconds"),
627 [ST_F_CHECK_DESC] = IST("check_description"),
628 [ST_F_AGENT_DESC] = IST("agentcheck_description"),
629 [ST_F_CHECK_RISE] = IST("check_rise"),
630 [ST_F_CHECK_FALL] = IST("check_fall"),
631 [ST_F_CHECK_HEALTH] = IST("check_value"),
632 [ST_F_AGENT_RISE] = IST("agentcheck_rise"),
633 [ST_F_AGENT_FALL] = IST("agentcheck_fall"),
634 [ST_F_AGENT_HEALTH] = IST("agentcheck_value"),
635 [ST_F_ADDR] = IST("address"),
636 [ST_F_COOKIE] = IST("cookie"),
637 [ST_F_MODE] = IST("mode"),
638 [ST_F_ALGO] = IST("loadbalance_algorithm"),
639 [ST_F_CONN_RATE] = IST("connections_rate_current"),
640 [ST_F_CONN_RATE_MAX] = IST("connections_rate_max"),
641 [ST_F_CONN_TOT] = IST("connections_total"),
642 [ST_F_INTERCEPTED] = IST("intercepted_requests_total"),
643 [ST_F_DCON] = IST("denied_connections_total"),
644 [ST_F_DSES] = IST("denied_sessions_total"),
645 [ST_F_WREW] = IST("failed_header_rewriting_total"),
646 [ST_F_CONNECT] = IST("connection_attempts_total"),
647 [ST_F_REUSE] = IST("connection_reuses_total"),
648 [ST_F_CACHE_LOOKUPS] = IST("http_cache_lookups_total"),
649 [ST_F_CACHE_HITS] = IST("http_cache_hits_total"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200650 [ST_F_SRV_ICUR] = IST("idle_connections_current"),
651 [ST_F_SRV_ILIM] = IST("idle_connections_limit"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100652 [ST_F_QT_MAX] = IST("max_queue_time_seconds"),
653 [ST_F_CT_MAX] = IST("max_connect_time_seconds"),
654 [ST_F_RT_MAX] = IST("max_response_time_seconds"),
655 [ST_F_TT_MAX] = IST("max_total_time_seconds"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100656 [ST_F_EINT] = IST("internal_errors_total"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200657 [ST_F_IDLE_CONN_CUR] = IST("unsafe_idle_connections_current"),
658 [ST_F_SAFE_CONN_CUR] = IST("safe_idle_connections_current"),
659 [ST_F_USED_CONN_CUR] = IST("used_connections_current"),
660 [ST_F_NEED_CONN_EST] = IST("need_connections_current"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100661};
662
Christopher Fauletf959d082019-02-07 15:38:42 +0100663/* Description of all stats fields */
664const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
665 [ST_F_PXNAME] = IST("The proxy name."),
666 [ST_F_SVNAME] = IST("The service name (FRONTEND for frontend, BACKEND for backend, any name for server/listener)."),
667 [ST_F_QCUR] = IST("Current number of queued requests."),
668 [ST_F_QMAX] = IST("Maximum observed number of queued requests."),
669 [ST_F_SCUR] = IST("Current number of active sessions."),
670 [ST_F_SMAX] = IST("Maximum observed number of active sessions."),
671 [ST_F_SLIM] = IST("Configured session limit."),
672 [ST_F_STOT] = IST("Total number of sessions."),
673 [ST_F_BIN] = IST("Current total of incoming bytes."),
674 [ST_F_BOUT] = IST("Current total of outgoing bytes."),
675 [ST_F_DREQ] = IST("Total number of denied requests."),
676 [ST_F_DRESP] = IST("Total number of denied responses."),
677 [ST_F_EREQ] = IST("Total number of request errors."),
678 [ST_F_ECON] = IST("Total number of connection errors."),
679 [ST_F_ERESP] = IST("Total number of response errors."),
680 [ST_F_WRETR] = IST("Total number of retry warnings."),
681 [ST_F_WREDIS] = IST("Total number of redispatch warnings."),
Willy Tarreau6b3bf732020-09-24 07:35:46 +0200682 [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 +0100683 [ST_F_WEIGHT] = IST("Service weight."),
684 [ST_F_ACT] = IST("Current number of active servers."),
685 [ST_F_BCK] = IST("Current number of backup servers."),
686 [ST_F_CHKFAIL] = IST("Total number of failed check (Only counts checks failed when the server is up)."),
687 [ST_F_CHKDOWN] = IST("Total number of UP->DOWN transitions."),
688 [ST_F_LASTCHG] = IST("Number of seconds since the last UP<->DOWN transition."),
689 [ST_F_DOWNTIME] = IST("Total downtime (in seconds) for the service."),
690 [ST_F_QLIMIT] = IST("Configured maxqueue for the server (0 meaning no limit)."),
691 [ST_F_PID] = IST("Process id (0 for first instance, 1 for second, ...)"),
692 [ST_F_IID] = IST("Unique proxy id."),
693 [ST_F_SID] = IST("Server id (unique inside a proxy)."),
694 [ST_F_THROTTLE] = IST("Current throttle percentage for the server, when slowstart is active, or no value if not in slowstart."),
695 [ST_F_LBTOT] = IST("Total number of times a service was selected, either for new sessions, or when redispatching."),
696 [ST_F_TRACKED] = IST("Id of proxy/server if tracking is enabled."),
697 [ST_F_TYPE] = IST("Service type (0=frontend, 1=backend, 2=server, 3=socket/listener)."),
698 [ST_F_RATE] = IST("Current number of sessions per second over last elapsed second."),
699 [ST_F_RATE_LIM] = IST("Configured limit on new sessions per second."),
700 [ST_F_RATE_MAX] = IST("Maximum observed number of sessions per second."),
Christopher Fauletcf403f32019-11-21 14:35:46 +0100701 [ST_F_CHECK_STATUS] = IST("Status of last health check (HCHK_STATUS_* values)."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100702 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100703 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100704 [ST_F_HRSP_1XX] = IST("Total number of HTTP responses."),
705 [ST_F_HRSP_2XX] = IST("Total number of HTTP responses."),
706 [ST_F_HRSP_3XX] = IST("Total number of HTTP responses."),
707 [ST_F_HRSP_4XX] = IST("Total number of HTTP responses."),
708 [ST_F_HRSP_5XX] = IST("Total number of HTTP responses."),
709 [ST_F_HRSP_OTHER] = IST("Total number of HTTP responses."),
710 [ST_F_HANAFAIL] = IST("Total number of failed health checks."),
711 [ST_F_REQ_RATE] = IST("Current number of HTTP requests per second over last elapsed second."),
712 [ST_F_REQ_RATE_MAX] = IST("Maximum observed number of HTTP requests per second."),
713 [ST_F_REQ_TOT] = IST("Total number of HTTP requests received."),
714 [ST_F_CLI_ABRT] = IST("Total number of data transfers aborted by the client."),
715 [ST_F_SRV_ABRT] = IST("Total number of data transfers aborted by the server."),
716 [ST_F_COMP_IN] = IST("Total number of HTTP response bytes fed to the compressor."),
717 [ST_F_COMP_OUT] = IST("Total number of HTTP response bytes emitted by the compressor."),
718 [ST_F_COMP_BYP] = IST("Total number of bytes that bypassed the HTTP compressor (CPU/BW limit)."),
719 [ST_F_COMP_RSP] = IST("Total number of HTTP responses that were compressed."),
720 [ST_F_LASTSESS] = IST("Number of seconds since last session assigned to server/backend."),
721 [ST_F_LAST_CHK] = IST("Last health check contents or textual error"),
722 [ST_F_LAST_AGT] = IST("Last agent check contents or textual error"),
723 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
724 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
725 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
726 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
727 [ST_F_AGENT_STATUS] = IST("Status of last agent check."),
728 [ST_F_AGENT_CODE] = IST("Numeric code reported by agent if any (unused for now)."),
729 [ST_F_AGENT_DURATION] = IST("Time in ms taken to finish last agent check."),
730 [ST_F_CHECK_DESC] = IST("Short human-readable description of the last health status."),
731 [ST_F_AGENT_DESC] = IST("Short human-readable description of the last agent status."),
732 [ST_F_CHECK_RISE] = IST("Server's \"rise\" parameter used by health checks"),
733 [ST_F_CHECK_FALL] = IST("Server's \"fall\" parameter used by health checks"),
734 [ST_F_CHECK_HEALTH] = IST("Server's health check value between 0 and rise+fall-1"),
735 [ST_F_AGENT_RISE] = IST("Agent's \"rise\" parameter, normally 1."),
736 [ST_F_AGENT_FALL] = IST("Agent's \"fall\" parameter, normally 1."),
737 [ST_F_AGENT_HEALTH] = IST("Agent's health parameter, between 0 and rise+fall-1"),
738 [ST_F_ADDR] = IST("address:port or \"unix\". IPv6 has brackets around the address."),
739 [ST_F_COOKIE] = IST("Server's cookie value or backend's cookie name."),
740 [ST_F_MODE] = IST("Proxy mode (tcp, http, health, unknown)."),
741 [ST_F_ALGO] = IST("Load balancing algorithm."),
742 [ST_F_CONN_RATE] = IST("Current number of connections per second over the last elapsed second."),
743 [ST_F_CONN_RATE_MAX] = IST("Maximum observed number of connections per second."),
744 [ST_F_CONN_TOT] = IST("Total number of connections."),
745 [ST_F_INTERCEPTED] = IST("Total number of intercepted HTTP requests."),
746 [ST_F_DCON] = IST("Total number of requests denied by \"tcp-request connection\" rules."),
747 [ST_F_DSES] = IST("Total number of requests denied by \"tcp-request session\" rules."),
748 [ST_F_WREW] = IST("Total number of failed header rewriting warnings."),
749 [ST_F_CONNECT] = IST("Total number of connection establishment attempts."),
750 [ST_F_REUSE] = IST("Total number of connection reuses."),
751 [ST_F_CACHE_LOOKUPS] = IST("Total number of HTTP cache lookups."),
752 [ST_F_CACHE_HITS] = IST("Total number of HTTP cache hits."),
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100753 [ST_F_SRV_ICUR] = IST("Current number of idle connections available for reuse"),
754 [ST_F_SRV_ILIM] = IST("Limit on the number of available idle connections"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100755 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
756 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
757 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
758 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100759 [ST_F_EINT] = IST("Total number of internal errors."),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200760 [ST_F_IDLE_CONN_CUR] = IST("Current number of unsafe idle connections."),
761 [ST_F_SAFE_CONN_CUR] = IST("Current number of safe idle connections."),
762 [ST_F_USED_CONN_CUR] = IST("Current number of connections in use."),
763 [ST_F_NEED_CONN_EST] = IST("Estimated needed number of connections."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100764};
765
766/* Specific labels for all info fields. Empty by default. */
767const struct ist promex_inf_metric_labels[INF_TOTAL_FIELDS] = {
Christopher Faulet0175b1f2021-01-20 15:28:22 +0100768 [INF_BUILD_INFO] = IST(PROMEX_BUILDINFO_LABEL),
Christopher Fauletf959d082019-02-07 15:38:42 +0100769};
770
771/* Specific labels for all stats fields. Empty by default. */
772const struct ist promex_st_metric_labels[ST_F_TOTAL_FIELDS] = {
Christopher Faulet0175b1f2021-01-20 15:28:22 +0100773 [ST_F_HRSP_1XX] = IST("code=\"1xx\""),
774 [ST_F_HRSP_2XX] = IST("code=\"2xx\""),
775 [ST_F_HRSP_3XX] = IST("code=\"3xx\""),
776 [ST_F_HRSP_4XX] = IST("code=\"4xx\""),
777 [ST_F_HRSP_5XX] = IST("code=\"5xx\""),
778 [ST_F_HRSP_OTHER] = IST("code=\"other\""),
Christopher Fauletf959d082019-02-07 15:38:42 +0100779};
780
781/* Type for all info fields. "untyped" is used for unsupported field. */
782const struct ist promex_inf_metric_types[INF_TOTAL_FIELDS] = {
783 [INF_NAME] = IST("untyped"),
784 [INF_VERSION] = IST("untyped"),
785 [INF_RELEASE_DATE] = IST("untyped"),
William Dauchy5a982a72021-01-08 13:18:06 +0100786 [INF_BUILD_INFO] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200787 [INF_NBTHREAD] = IST("gauge"),
788 [INF_NBPROC] = IST("gauge"),
789 [INF_PROCESS_NUM] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100790 [INF_PID] = IST("untyped"),
791 [INF_UPTIME] = IST("untyped"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200792 [INF_UPTIME_SEC] = IST("gauge"),
William Dauchydefd1562021-01-15 22:41:38 +0100793 [INF_START_TIME_SEC] = IST("gauge"),
William Dauchya8766cf2021-01-15 22:41:37 +0100794 [INF_MEMMAX_BYTES] = IST("gauge"),
795 [INF_POOL_ALLOC_BYTES] = IST("gauge"),
796 [INF_POOL_USED_BYTES] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100797 [INF_POOL_FAILED] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200798 [INF_ULIMIT_N] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100799 [INF_MAXSOCK] = IST("gauge"),
800 [INF_MAXCONN] = IST("gauge"),
801 [INF_HARD_MAXCONN] = IST("gauge"),
802 [INF_CURR_CONN] = IST("gauge"),
803 [INF_CUM_CONN] = IST("counter"),
804 [INF_CUM_REQ] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200805 [INF_MAX_SSL_CONNS] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100806 [INF_CURR_SSL_CONNS] = IST("gauge"),
807 [INF_CUM_SSL_CONNS] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200808 [INF_MAXPIPES] = IST("gauge"),
809 [INF_PIPES_USED] = IST("counter"),
810 [INF_PIPES_FREE] = IST("counter"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100811 [INF_CONN_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200812 [INF_CONN_RATE_LIMIT] = IST("gauge"),
813 [INF_MAX_CONN_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100814 [INF_SESS_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200815 [INF_SESS_RATE_LIMIT] = IST("gauge"),
816 [INF_MAX_SESS_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100817 [INF_SSL_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200818 [INF_SSL_RATE_LIMIT] = IST("gauge"),
819 [INF_MAX_SSL_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100820 [INF_SSL_FRONTEND_KEY_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200821 [INF_SSL_FRONTEND_MAX_KEY_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100822 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = IST("gauge"),
823 [INF_SSL_BACKEND_KEY_RATE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200824 [INF_SSL_BACKEND_MAX_KEY_RATE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100825 [INF_SSL_CACHE_LOOKUPS] = IST("counter"),
826 [INF_SSL_CACHE_MISSES] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200827 [INF_COMPRESS_BPS_IN] = IST("counter"),
828 [INF_COMPRESS_BPS_OUT] = IST("counter"),
829 [INF_COMPRESS_BPS_RATE_LIM] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100830 [INF_ZLIB_MEM_USAGE] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200831 [INF_MAX_ZLIB_MEM_USAGE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100832 [INF_TASKS] = IST("gauge"),
Christopher Fauletf782c232019-04-17 16:04:44 +0200833 [INF_RUN_QUEUE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100834 [INF_IDLE_PCT] = IST("gauge"),
835 [INF_NODE] = IST("untyped"),
836 [INF_DESCRIPTION] = IST("untyped"),
837 [INF_STOPPING] = IST("gauge"),
838 [INF_JOBS] = IST("gauge"),
839 [INF_UNSTOPPABLE_JOBS] = IST("gauge"),
840 [INF_LISTENERS] = IST("gauge"),
841 [INF_ACTIVE_PEERS] = IST("gauge"),
842 [INF_CONNECTED_PEERS] = IST("gauge"),
843 [INF_DROPPED_LOGS] = IST("counter"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200844 [INF_BUSY_POLLING] = IST("gauge"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200845 [INF_FAILED_RESOLUTIONS] = IST("counter"),
846 [INF_TOTAL_BYTES_OUT] = IST("counter"),
847 [INF_TOTAL_SPLICED_BYTES_OUT] = IST("counter"),
848 [INF_BYTES_OUT_RATE] = IST("gauge"),
849 [INF_DEBUG_COMMANDS_ISSUED] = IST(""),
Christopher Fauletf959d082019-02-07 15:38:42 +0100850};
851
852/* Type for all stats fields. "untyped" is used for unsupported field. */
853const struct ist promex_st_metric_types[ST_F_TOTAL_FIELDS] = {
854 [ST_F_PXNAME] = IST("untyped"),
855 [ST_F_SVNAME] = IST("untyped"),
856 [ST_F_QCUR] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200857 [ST_F_QMAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100858 [ST_F_SCUR] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200859 [ST_F_SMAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100860 [ST_F_SLIM] = IST("gauge"),
861 [ST_F_STOT] = IST("counter"),
862 [ST_F_BIN] = IST("counter"),
863 [ST_F_BOUT] = IST("counter"),
864 [ST_F_DREQ] = IST("counter"),
865 [ST_F_DRESP] = IST("counter"),
866 [ST_F_EREQ] = IST("counter"),
867 [ST_F_ECON] = IST("counter"),
868 [ST_F_ERESP] = IST("counter"),
869 [ST_F_WRETR] = IST("counter"),
870 [ST_F_WREDIS] = IST("counter"),
871 [ST_F_STATUS] = IST("gauge"),
872 [ST_F_WEIGHT] = IST("gauge"),
873 [ST_F_ACT] = IST("gauge"),
874 [ST_F_BCK] = IST("gauge"),
875 [ST_F_CHKFAIL] = IST("counter"),
876 [ST_F_CHKDOWN] = IST("counter"),
877 [ST_F_LASTCHG] = IST("gauge"),
878 [ST_F_DOWNTIME] = IST("counter"),
879 [ST_F_QLIMIT] = IST("gauge"),
880 [ST_F_PID] = IST("untyped"),
881 [ST_F_IID] = IST("untyped"),
882 [ST_F_SID] = IST("untyped"),
883 [ST_F_THROTTLE] = IST("gauge"),
884 [ST_F_LBTOT] = IST("counter"),
885 [ST_F_TRACKED] = IST("untyped"),
886 [ST_F_TYPE] = IST("untyped"),
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200887 [ST_F_RATE] = IST("untyped"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100888 [ST_F_RATE_LIM] = IST("gauge"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200889 [ST_F_RATE_MAX] = IST("gauge"),
Christopher Fauletcf403f32019-11-21 14:35:46 +0100890 [ST_F_CHECK_STATUS] = IST("gauge"),
891 [ST_F_CHECK_CODE] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100892 [ST_F_CHECK_DURATION] = IST("gauge"),
893 [ST_F_HRSP_1XX] = IST("counter"),
894 [ST_F_HRSP_2XX] = IST("counter"),
895 [ST_F_HRSP_3XX] = IST("counter"),
896 [ST_F_HRSP_4XX] = IST("counter"),
897 [ST_F_HRSP_5XX] = IST("counter"),
898 [ST_F_HRSP_OTHER] = IST("counter"),
899 [ST_F_HANAFAIL] = IST("counter"),
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200900 [ST_F_REQ_RATE] = IST("untyped"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200901 [ST_F_REQ_RATE_MAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100902 [ST_F_REQ_TOT] = IST("counter"),
903 [ST_F_CLI_ABRT] = IST("counter"),
904 [ST_F_SRV_ABRT] = IST("counter"),
905 [ST_F_COMP_IN] = IST("counter"),
906 [ST_F_COMP_OUT] = IST("counter"),
907 [ST_F_COMP_BYP] = IST("counter"),
908 [ST_F_COMP_RSP] = IST("counter"),
909 [ST_F_LASTSESS] = IST("gauge"),
910 [ST_F_LAST_CHK] = IST("untyped"),
911 [ST_F_LAST_AGT] = IST("untyped"),
912 [ST_F_QTIME] = IST("gauge"),
913 [ST_F_CTIME] = IST("gauge"),
914 [ST_F_RTIME] = IST("gauge"),
915 [ST_F_TTIME] = IST("gauge"),
916 [ST_F_AGENT_STATUS] = IST("untyped"),
917 [ST_F_AGENT_CODE] = IST("untyped"),
918 [ST_F_AGENT_DURATION] = IST("gauge"),
919 [ST_F_CHECK_DESC] = IST("untyped"),
920 [ST_F_AGENT_DESC] = IST("untyped"),
921 [ST_F_CHECK_RISE] = IST("gauge"),
922 [ST_F_CHECK_FALL] = IST("gauge"),
923 [ST_F_CHECK_HEALTH] = IST("gauge"),
924 [ST_F_AGENT_RISE] = IST("gauge"),
925 [ST_F_AGENT_FALL] = IST("gauge"),
926 [ST_F_AGENT_HEALTH] = IST("gauge"),
927 [ST_F_ADDR] = IST("untyped"),
928 [ST_F_COOKIE] = IST("untyped"),
929 [ST_F_MODE] = IST("untyped"),
930 [ST_F_ALGO] = IST("untyped"),
Christopher Fauletc58fc0d2019-04-18 10:10:49 +0200931 [ST_F_CONN_RATE] = IST("untyped"),
Christopher Faulet769a92d2019-04-18 10:18:44 +0200932 [ST_F_CONN_RATE_MAX] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100933 [ST_F_CONN_TOT] = IST("counter"),
934 [ST_F_INTERCEPTED] = IST("counter"),
935 [ST_F_DCON] = IST("counter"),
936 [ST_F_DSES] = IST("counter"),
937 [ST_F_WREW] = IST("counter"),
938 [ST_F_CONNECT] = IST("counter"),
939 [ST_F_REUSE] = IST("counter"),
940 [ST_F_CACHE_LOOKUPS] = IST("counter"),
941 [ST_F_CACHE_HITS] = IST("counter"),
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100942 [ST_F_SRV_ICUR] = IST("gauge"),
943 [ST_F_SRV_ILIM] = IST("gauge"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100944 [ST_F_QT_MAX] = IST("gauge"),
945 [ST_F_CT_MAX] = IST("gauge"),
946 [ST_F_RT_MAX] = IST("gauge"),
947 [ST_F_TT_MAX] = IST("gauge"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100948 [ST_F_EINT] = IST("counter"),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200949 [ST_F_IDLE_CONN_CUR] = IST("gauge"),
950 [ST_F_SAFE_CONN_CUR] = IST("gauge"),
951 [ST_F_USED_CONN_CUR] = IST("gauge"),
952 [ST_F_NEED_CONN_EST] = IST("gauge"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100953};
954
Christopher Fauletd45d1052019-09-06 16:10:19 +0200955/* Return the server status: 0=DOWN, 1=UP, 2=MAINT, 3=DRAIN, 4=NOLB. */
Christopher Fauletf959d082019-02-07 15:38:42 +0100956static int promex_srv_status(struct server *sv)
957{
Christopher Fauletf959d082019-02-07 15:38:42 +0100958 int state = 0;
959
Christopher Fauletf959d082019-02-07 15:38:42 +0100960 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
961 state = 1;
962 if (sv->cur_admin & SRV_ADMF_DRAIN)
Christopher Fauletd45d1052019-09-06 16:10:19 +0200963 state = 3;
Christopher Fauletf959d082019-02-07 15:38:42 +0100964 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200965 else if (sv->cur_state == SRV_ST_STOPPING)
966 state = 4;
967
968 if (sv->cur_admin & SRV_ADMF_MAINT)
969 state = 2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100970
971 return state;
972}
973
974/* Convert a field to its string representation and write it in <out>, followed
975 * by a newline, if there is enough space. non-numeric value are converted in
976 * "Nan" because Prometheus only support numerical values (but it is unexepceted
977 * to process this kind of value). It returns 1 on success. Otherwise, it
978 * returns 0. The buffer's length must not exceed <max> value.
979 */
980static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
981{
982 int ret = 0;
983
984 switch (field_format(f, 0)) {
985 case FF_EMPTY: ret = chunk_strcat(out, "Nan\n"); break;
986 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
987 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
988 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
989 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200990 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100991 case FF_STR: ret = chunk_strcat(out, "Nan\n"); break;
992 default: ret = chunk_strcat(out, "Nan\n"); break;
993 }
994 if (!ret || out->data > max)
995 return 0;
996 return 1;
997}
998
999/* Concatenate the <prefix> with the field name using the array
1000 * <promex_st_metric_names> and store it in <name>. The field type is in
1001 * <appctx->st2>. This function never fails but relies on
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001002 * <PROMEX_MAX_NAME_LEN>. So by sure the result is small enough to be copied in
Christopher Fauletf959d082019-02-07 15:38:42 +01001003 * <name>
1004 */
1005static void promex_metric_name(struct appctx *appctx, struct ist *name, const struct ist prefix)
1006{
1007 const struct ist *names;
1008
1009 names = ((appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC)
1010 ? promex_inf_metric_names
1011 : promex_st_metric_names);
1012
1013 istcat(name, prefix, PROMEX_MAX_NAME_LEN);
1014 istcat(name, names[appctx->st2], PROMEX_MAX_NAME_LEN);
1015}
1016
1017/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
1018 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
1019 */
1020static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
1021 const struct ist name, struct ist *out, size_t max)
1022{
1023 const struct ist *desc, *types;
1024
William Dauchya191b772021-01-15 22:41:39 +01001025 if (istcat(out, ist("# HELP "), max) == -1 ||
1026 istcat(out, name, max) == -1 ||
1027 istcat(out, ist(" "), max) == -1)
1028 goto full;
1029
Christopher Fauletf959d082019-02-07 15:38:42 +01001030 if (appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC) {
Christopher Fauletf959d082019-02-07 15:38:42 +01001031 types = promex_inf_metric_types;
William Dauchya191b772021-01-15 22:41:39 +01001032
1033 if (istcat(out, ist(info_fields[appctx->st2].desc), max) == -1)
1034 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +01001035 }
1036 else {
1037 desc = promex_st_metric_desc;
1038 types = promex_st_metric_types;
William Dauchya191b772021-01-15 22:41:39 +01001039
1040 if (istcat(out, desc[appctx->st2], max) == -1)
1041 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +01001042 }
1043
William Dauchya191b772021-01-15 22:41:39 +01001044 if (istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001045 istcat(out, name, max) == -1 ||
1046 istcat(out, ist(" "), max) == -1 ||
1047 istcat(out, types[appctx->st2], max) == -1 ||
1048 istcat(out, ist("\n"), max) == -1)
1049 goto full;
1050
1051 return 1;
1052
1053 full:
1054 return 0;
1055}
1056
1057/* Dump the line for <metric>. It starts by the metric name followed by its
1058 * labels (proxy name, server name...) between braces and finally its value. If
1059 * not already done, the header lines are dumped first. It returns 1 on
1060 * success. Otherwise if <out> length exceeds <max>, it returns 0.
1061 */
1062static int promex_dump_metric(struct appctx *appctx, struct htx *htx,
1063 const struct ist prefix, struct field *metric,
1064 struct ist *out, size_t max)
1065{
1066 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
1067 size_t len = out->len;
1068
1069 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
1070 return 0;
1071
1072 promex_metric_name(appctx, &name, prefix);
1073 if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) &&
1074 !promex_dump_metric_header(appctx, htx, name, out, max))
1075 goto full;
1076
1077 if (appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC) {
1078 const struct ist label = promex_inf_metric_labels[appctx->st2];
1079
1080 if (istcat(out, name, max) == -1 ||
1081 (label.len && istcat(out, ist("{"), max) == -1) ||
1082 (label.len && istcat(out, label, max) == -1) ||
1083 (label.len && istcat(out, ist("}"), max) == -1) ||
1084 istcat(out, ist(" "), max) == -1)
1085 goto full;
1086 }
1087 else {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001088 struct proxy *px = appctx->ctx.stats.obj1;
1089 struct server *srv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +01001090 const struct ist label = promex_st_metric_labels[appctx->st2];
1091
1092 if (istcat(out, name, max) == -1 ||
1093 istcat(out, ist("{proxy=\""), max) == -1 ||
1094 istcat(out, ist2(px->id, strlen(px->id)), max) == -1 ||
1095 istcat(out, ist("\""), max) == -1 ||
1096 (srv && istcat(out, ist(",server=\""), max) == -1) ||
1097 (srv && istcat(out, ist2(srv->id, strlen(srv->id)), max) == -1) ||
1098 (srv && istcat(out, ist("\""), max) == -1) ||
1099 (label.len && istcat(out, ist(","), max) == -1) ||
1100 (label.len && istcat(out, label, max) == -1) ||
1101 istcat(out, ist("} "), max) == -1)
1102 goto full;
1103 }
1104
1105 trash.data = out->len;
1106 if (!promex_metric_to_str(&trash, metric, max))
1107 goto full;
1108 out->len = trash.data;
1109
1110 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1111 return 1;
1112 full:
1113 // Restore previous length
1114 out->len = len;
1115 return 0;
1116
1117}
1118
1119
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001120/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001121 * 0 if <htx> is full and -1 in case of any error. */
1122static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
1123{
1124 static struct ist prefix = IST("haproxy_process_");
1125 struct field metric;
1126 struct channel *chn = si_ic(appctx->owner);
1127 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001128 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001129 int ret = 1;
1130
William Dauchy5d9b8f32021-01-11 20:07:49 +01001131 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
1132 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001133
Christopher Fauletf959d082019-02-07 15:38:42 +01001134 while (appctx->st2 && appctx->st2 < INF_TOTAL_FIELDS) {
1135 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +01001136 case INF_BUILD_INFO:
1137 metric = mkf_u32(FN_GAUGE, 1);
1138 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001139
1140 default:
William Dauchy5d9b8f32021-01-11 20:07:49 +01001141 metric = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001142 }
1143
1144 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1145 goto full;
1146
Christopher Fauletf959d082019-02-07 15:38:42 +01001147 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
1148 appctx->st2 = promex_global_metrics[appctx->st2];
1149 }
1150
1151 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001152 if (out.len) {
1153 if (!htx_add_data_atonce(htx, out))
1154 return -1; /* Unexpected and unrecoverable error */
1155 channel_add_input(chn, out.len);
1156 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001157 return ret;
1158 full:
1159 ret = 0;
1160 goto end;
1161}
1162
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001163/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001164 * 0 if <htx> is full and -1 in case of any error. */
1165static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
1166{
1167 static struct ist prefix = IST("haproxy_frontend_");
1168 struct proxy *px;
1169 struct field metric;
1170 struct channel *chn = si_ic(appctx->owner);
1171 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001172 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +01001173 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +01001174 int ret = 1;
1175
1176 while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001177 while (appctx->ctx.stats.obj1) {
1178 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001179
1180 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001181 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +01001182 goto next_px;
1183
William Dauchyb9577452021-01-17 18:27:46 +01001184 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
1185 return -1;
1186
Christopher Fauletf959d082019-02-07 15:38:42 +01001187 switch (appctx->st2) {
1188 case ST_F_STATUS:
Willy Tarreauc3914d42020-09-24 08:39:22 +02001189 metric = mkf_u32(FO_STATUS, !px->disabled);
Christopher Fauletf959d082019-02-07 15:38:42 +01001190 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001191 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001192 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001193 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001194 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +01001195 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +01001196 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +01001197 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +01001198 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001199 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +01001200 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +01001201 if (px->mode != PR_MODE_HTTP)
1202 goto next_px;
William Dauchyb9577452021-01-17 18:27:46 +01001203 metric = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001204 break;
William Dauchyb9577452021-01-17 18:27:46 +01001205 case ST_F_HRSP_2XX:
1206 case ST_F_HRSP_3XX:
1207 case ST_F_HRSP_4XX:
1208 case ST_F_HRSP_5XX:
1209 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +01001210 if (px->mode != PR_MODE_HTTP)
1211 goto next_px;
William Dauchyb9577452021-01-17 18:27:46 +01001212 metric = stats[appctx->st2];
1213 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +01001214 break;
1215
1216 default:
William Dauchyb9577452021-01-17 18:27:46 +01001217 metric = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001218 }
1219
1220 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1221 goto full;
1222 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001223 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001224 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001225 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001226 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +01001227 appctx->st2 = promex_front_metrics[appctx->st2];
1228 }
1229
1230 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001231 if (out.len) {
1232 if (!htx_add_data_atonce(htx, out))
1233 return -1; /* Unexpected and unrecoverable error */
1234 channel_add_input(chn, out.len);
1235 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001236 return ret;
1237 full:
1238 ret = 0;
1239 goto end;
1240}
1241
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001242/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001243 * 0 if <htx> is full and -1 in case of any error. */
1244static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
1245{
1246 static struct ist prefix = IST("haproxy_backend_");
1247 struct proxy *px;
1248 struct field metric;
1249 struct channel *chn = si_ic(appctx->owner);
1250 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001251 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001252 int ret = 1;
1253 uint32_t weight;
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001254 double secs;
Christopher Fauletf959d082019-02-07 15:38:42 +01001255
1256 while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001257 while (appctx->ctx.stats.obj1) {
1258 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001259
1260 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001261 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +01001262 goto next_px;
1263
1264 switch (appctx->st2) {
1265 case ST_F_STATUS:
1266 metric = mkf_u32(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
1267 break;
1268 case ST_F_SCUR:
1269 metric = mkf_u32(0, px->beconn);
1270 break;
1271 case ST_F_SMAX:
1272 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
1273 break;
1274 case ST_F_SLIM:
1275 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1276 break;
1277 case ST_F_STOT:
1278 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1279 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001280 case ST_F_RATE_MAX:
1281 metric = mkf_u32(0, px->be_counters.sps_max);
1282 break;
1283 case ST_F_LASTSESS:
1284 metric = mkf_s32(FN_AGE, be_lastsession(px));
1285 break;
1286 case ST_F_QCUR:
1287 metric = mkf_u32(0, px->nbpend);
1288 break;
1289 case ST_F_QMAX:
1290 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
1291 break;
1292 case ST_F_CONNECT:
1293 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
1294 break;
1295 case ST_F_REUSE:
1296 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
1297 break;
1298 case ST_F_BIN:
1299 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1300 break;
1301 case ST_F_BOUT:
1302 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1303 break;
1304 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001305 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
1306 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001307 break;
1308 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001309 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
1310 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001311 break;
1312 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001313 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
1314 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001315 break;
1316 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001317 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
1318 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001319 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001320 case ST_F_QT_MAX:
1321 secs = (double)px->be_counters.qtime_max / 1000.0;
1322 metric = mkf_flt(FN_MAX, secs);
1323 break;
1324 case ST_F_CT_MAX:
1325 secs = (double)px->be_counters.ctime_max / 1000.0;
1326 metric = mkf_flt(FN_MAX, secs);
1327 break;
1328 case ST_F_RT_MAX:
1329 secs = (double)px->be_counters.dtime_max / 1000.0;
1330 metric = mkf_flt(FN_MAX, secs);
1331 break;
1332 case ST_F_TT_MAX:
1333 secs = (double)px->be_counters.ttime_max / 1000.0;
1334 metric = mkf_flt(FN_MAX, secs);
1335 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001336 case ST_F_DREQ:
1337 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1338 break;
1339 case ST_F_DRESP:
1340 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1341 break;
1342 case ST_F_ECON:
1343 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1344 break;
1345 case ST_F_ERESP:
1346 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1347 break;
1348 case ST_F_WRETR:
1349 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
1350 break;
1351 case ST_F_WREDIS:
1352 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
1353 break;
1354 case ST_F_WREW:
1355 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
1356 break;
Christopher Faulete4a2c8d2019-12-16 14:44:01 +01001357 case ST_F_EINT:
1358 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
1359 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001360 case ST_F_CLI_ABRT:
1361 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1362 break;
1363 case ST_F_SRV_ABRT:
1364 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1365 break;
1366 case ST_F_WEIGHT:
1367 weight = (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
1368 metric = mkf_u32(FN_AVG, weight);
1369 break;
1370 case ST_F_ACT:
1371 metric = mkf_u32(0, px->srv_act);
1372 break;
1373 case ST_F_BCK:
1374 metric = mkf_u32(0, px->srv_bck);
1375 break;
1376 case ST_F_CHKDOWN:
1377 metric = mkf_u64(FN_COUNTER, px->down_trans);
1378 break;
1379 case ST_F_LASTCHG:
1380 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1381 break;
1382 case ST_F_DOWNTIME:
1383 metric = mkf_u32(FN_COUNTER, be_downtime(px));
1384 break;
1385 case ST_F_LBTOT:
1386 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1387 break;
1388 case ST_F_REQ_TOT:
1389 if (px->mode != PR_MODE_HTTP)
1390 goto next_px;
1391 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1392 break;
1393 case ST_F_HRSP_1XX:
1394 if (px->mode != PR_MODE_HTTP)
1395 goto next_px;
1396 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1397 break;
1398 case ST_F_HRSP_2XX:
1399 if (px->mode != PR_MODE_HTTP)
1400 goto next_px;
1401 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1402 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1403 break;
1404 case ST_F_HRSP_3XX:
1405 if (px->mode != PR_MODE_HTTP)
1406 goto next_px;
1407 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1408 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1409 break;
1410 case ST_F_HRSP_4XX:
1411 if (px->mode != PR_MODE_HTTP)
1412 goto next_px;
1413 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1414 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1415 break;
1416 case ST_F_HRSP_5XX:
1417 if (px->mode != PR_MODE_HTTP)
1418 goto next_px;
1419 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1420 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1421 break;
1422 case ST_F_HRSP_OTHER:
1423 if (px->mode != PR_MODE_HTTP)
1424 goto next_px;
1425 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1426 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
1427 break;
1428 case ST_F_CACHE_LOOKUPS:
1429 if (px->mode != PR_MODE_HTTP)
1430 goto next_px;
1431 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1432 break;
1433 case ST_F_CACHE_HITS:
1434 if (px->mode != PR_MODE_HTTP)
1435 goto next_px;
1436 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
1437 break;
1438 case ST_F_COMP_IN:
1439 if (px->mode != PR_MODE_HTTP)
1440 goto next_px;
1441 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1442 break;
1443 case ST_F_COMP_OUT:
1444 if (px->mode != PR_MODE_HTTP)
1445 goto next_px;
1446 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1447 break;
1448 case ST_F_COMP_BYP:
1449 if (px->mode != PR_MODE_HTTP)
1450 goto next_px;
1451 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1452 break;
1453 case ST_F_COMP_RSP:
1454 if (px->mode != PR_MODE_HTTP)
1455 goto next_px;
1456 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1457 break;
1458
1459 default:
1460 goto next_metric;
1461 }
1462
1463 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1464 goto full;
1465 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001466 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001467 }
1468 next_metric:
1469 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001470 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +01001471 appctx->st2 = promex_back_metrics[appctx->st2];
1472 }
1473
1474 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001475 if (out.len) {
1476 if (!htx_add_data_atonce(htx, out))
1477 return -1; /* Unexpected and unrecoverable error */
1478 channel_add_input(chn, out.len);
1479 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001480 return ret;
1481 full:
1482 ret = 0;
1483 goto end;
1484}
1485
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001486/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +01001487 * 0 if <htx> is full and -1 in case of any error. */
1488static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
1489{
1490 static struct ist prefix = IST("haproxy_server_");
1491 struct proxy *px;
1492 struct server *sv;
1493 struct field metric;
1494 struct channel *chn = si_ic(appctx->owner);
1495 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +02001496 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001497 int ret = 1;
1498 uint32_t weight;
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001499 double secs;
Christopher Fauletf959d082019-02-07 15:38:42 +01001500
1501 while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001502 while (appctx->ctx.stats.obj1) {
1503 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +01001504
1505 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001506 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +01001507 goto next_px;
1508
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001509 while (appctx->ctx.stats.obj2) {
1510 sv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +01001511
Christopher Fauleteba22942019-11-19 14:18:24 +01001512 if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
1513 goto next_sv;
1514
Christopher Fauletf959d082019-02-07 15:38:42 +01001515 switch (appctx->st2) {
1516 case ST_F_STATUS:
1517 metric = mkf_u32(FO_STATUS, promex_srv_status(sv));
1518 break;
1519 case ST_F_SCUR:
1520 metric = mkf_u32(0, sv->cur_sess);
1521 break;
1522 case ST_F_SMAX:
1523 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1524 break;
1525 case ST_F_SLIM:
1526 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1527 break;
1528 case ST_F_STOT:
1529 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1530 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001531 case ST_F_RATE_MAX:
1532 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
1533 break;
1534 case ST_F_LASTSESS:
1535 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
1536 break;
1537 case ST_F_QCUR:
1538 metric = mkf_u32(0, sv->nbpend);
1539 break;
1540 case ST_F_QMAX:
1541 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1542 break;
1543 case ST_F_QLIMIT:
1544 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1545 break;
1546 case ST_F_BIN:
1547 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1548 break;
1549 case ST_F_BOUT:
1550 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1551 break;
1552 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001553 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
1554 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001555 break;
1556 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001557 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
1558 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001559 break;
1560 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001561 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
1562 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001563 break;
1564 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001565 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
1566 metric = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001567 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001568 case ST_F_QT_MAX:
1569 secs = (double)sv->counters.qtime_max / 1000.0;
1570 metric = mkf_flt(FN_MAX, secs);
1571 break;
1572 case ST_F_CT_MAX:
1573 secs = (double)sv->counters.ctime_max / 1000.0;
1574 metric = mkf_flt(FN_MAX, secs);
1575 break;
1576 case ST_F_RT_MAX:
1577 secs = (double)sv->counters.dtime_max / 1000.0;
1578 metric = mkf_flt(FN_MAX, secs);
1579 break;
1580 case ST_F_TT_MAX:
1581 secs = (double)sv->counters.ttime_max / 1000.0;
1582 metric = mkf_flt(FN_MAX, secs);
1583 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001584 case ST_F_CONNECT:
1585 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
1586 break;
1587 case ST_F_REUSE:
1588 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
1589 break;
1590 case ST_F_DRESP:
Christopher Fauleta08546b2019-12-16 16:07:34 +01001591 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
Christopher Fauletf959d082019-02-07 15:38:42 +01001592 break;
1593 case ST_F_ECON:
1594 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1595 break;
1596 case ST_F_ERESP:
1597 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1598 break;
1599 case ST_F_WRETR:
1600 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
1601 break;
1602 case ST_F_WREDIS:
1603 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
1604 break;
1605 case ST_F_WREW:
1606 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
1607 break;
Christopher Faulete4a2c8d2019-12-16 14:44:01 +01001608 case ST_F_EINT:
1609 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
1610 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001611 case ST_F_CLI_ABRT:
1612 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1613 break;
1614 case ST_F_SRV_ABRT:
1615 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1616 break;
1617 case ST_F_WEIGHT:
1618 weight = (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
1619 metric = mkf_u32(FN_AVG, weight);
1620 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001621 case ST_F_CHECK_STATUS:
1622 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1623 goto next_sv;
1624 metric = mkf_u32(FN_OUTPUT, sv->check.status);
1625 break;
1626 case ST_F_CHECK_CODE:
1627 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1628 goto next_sv;
1629 metric = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
1630 break;
Christopher Faulet2711e512020-02-27 16:12:07 +01001631 case ST_F_CHECK_DURATION:
1632 if (sv->check.status < HCHK_STATUS_CHECKED)
1633 goto next_sv;
1634 secs = (double)sv->check.duration / 1000.0;
1635 metric = mkf_flt(FN_DURATION, secs);
1636 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001637 case ST_F_CHKFAIL:
1638 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1639 break;
1640 case ST_F_CHKDOWN:
1641 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1642 break;
1643 case ST_F_DOWNTIME:
1644 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
1645 break;
1646 case ST_F_LASTCHG:
1647 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
1648 break;
1649 case ST_F_THROTTLE:
1650 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
1651 break;
1652 case ST_F_LBTOT:
1653 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1654 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001655 case ST_F_REQ_TOT:
1656 if (px->mode != PR_MODE_HTTP)
1657 goto next_px;
1658 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
1659 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001660 case ST_F_HRSP_1XX:
1661 if (px->mode != PR_MODE_HTTP)
1662 goto next_px;
1663 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1664 break;
1665 case ST_F_HRSP_2XX:
1666 if (px->mode != PR_MODE_HTTP)
1667 goto next_px;
1668 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1669 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1670 break;
1671 case ST_F_HRSP_3XX:
1672 if (px->mode != PR_MODE_HTTP)
1673 goto next_px;
1674 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1675 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1676 break;
1677 case ST_F_HRSP_4XX:
1678 if (px->mode != PR_MODE_HTTP)
1679 goto next_px;
1680 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1681 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1682 break;
1683 case ST_F_HRSP_5XX:
1684 if (px->mode != PR_MODE_HTTP)
1685 goto next_px;
1686 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1687 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1688 break;
1689 case ST_F_HRSP_OTHER:
1690 if (px->mode != PR_MODE_HTTP)
1691 goto next_px;
1692 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
1693 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1694 break;
Christopher Faulet20ab80c2019-11-08 15:24:32 +01001695 case ST_F_SRV_ICUR:
1696 metric = mkf_u32(0, sv->curr_idle_conns);
1697 break;
1698 case ST_F_SRV_ILIM:
1699 metric = mkf_u32(FO_CONFIG|FN_LIMIT, (sv->max_idle_conns == -1) ? 0 : sv->max_idle_conns);
1700 break;
Christopher Fauletc55a6262020-07-10 15:39:39 +02001701 case ST_F_IDLE_CONN_CUR:
1702 metric = mkf_u32(0, sv->curr_idle_nb);
1703 break;
1704 case ST_F_SAFE_CONN_CUR:
1705 metric = mkf_u32(0, sv->curr_safe_nb);
1706 break;
1707 case ST_F_USED_CONN_CUR:
1708 metric = mkf_u32(0, sv->curr_used_conns);
1709 break;
1710 case ST_F_NEED_CONN_EST:
1711 metric = mkf_u32(0, sv->est_need_conns);
1712 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001713
1714 default:
1715 goto next_metric;
1716 }
1717
1718 if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
1719 goto full;
1720
Christopher Fauleteba22942019-11-19 14:18:24 +01001721 next_sv:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001722 appctx->ctx.stats.obj2 = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001723 }
1724
1725 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001726 appctx->ctx.stats.obj1 = px->next;
1727 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001728 }
1729 next_metric:
1730 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001731 appctx->ctx.stats.obj1 = proxies_list;
1732 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001733 appctx->st2 = promex_srv_metrics[appctx->st2];
1734 }
1735
1736
1737 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001738 if (out.len) {
1739 if (!htx_add_data_atonce(htx, out))
1740 return -1; /* Unexpected and unrecoverable error */
1741 channel_add_input(chn, out.len);
1742 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001743 return ret;
1744 full:
1745 ret = 0;
1746 goto end;
1747}
1748
1749/* Dump all metrics (global, frontends, backends and servers) depending on the
1750 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001751 * -1 in case of any error.
1752 * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
1753 * a pointer to the current server/listener. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001754static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1755{
1756 int ret;
1757
1758 switch (appctx->st1) {
1759 case PROMEX_DUMPER_INIT:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001760 appctx->ctx.stats.obj1 = NULL;
1761 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001762 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001763 appctx->st2 = promex_global_metrics[INF_NAME];
1764 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1765 /* fall through */
1766
1767 case PROMEX_DUMPER_GLOBAL:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001768 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) {
1769 ret = promex_dump_global_metrics(appctx, htx);
1770 if (ret <= 0) {
1771 if (ret == -1)
1772 goto error;
1773 goto full;
1774 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001775 }
1776
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001777 appctx->ctx.stats.obj1 = proxies_list;
1778 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001779 appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001780 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001781 appctx->st2 = promex_front_metrics[ST_F_PXNAME];
1782 appctx->st1 = PROMEX_DUMPER_FRONT;
1783 /* fall through */
1784
1785 case PROMEX_DUMPER_FRONT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001786 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) {
1787 ret = promex_dump_front_metrics(appctx, htx);
1788 if (ret <= 0) {
1789 if (ret == -1)
1790 goto error;
1791 goto full;
1792 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001793 }
1794
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001795 appctx->ctx.stats.obj1 = proxies_list;
1796 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001797 appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC;
1798 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001799 appctx->st2 = promex_back_metrics[ST_F_PXNAME];
1800 appctx->st1 = PROMEX_DUMPER_BACK;
1801 /* fall through */
1802
1803 case PROMEX_DUMPER_BACK:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001804 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) {
1805 ret = promex_dump_back_metrics(appctx, htx);
1806 if (ret <= 0) {
1807 if (ret == -1)
1808 goto error;
1809 goto full;
1810 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001811 }
1812
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001813 appctx->ctx.stats.obj1 = proxies_list;
1814 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001815 appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC;
1816 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001817 appctx->st2 = promex_srv_metrics[ST_F_PXNAME];
1818 appctx->st1 = PROMEX_DUMPER_SRV;
1819 /* fall through */
1820
1821 case PROMEX_DUMPER_SRV:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001822 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) {
1823 ret = promex_dump_srv_metrics(appctx, htx);
1824 if (ret <= 0) {
1825 if (ret == -1)
1826 goto error;
1827 goto full;
1828 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001829 }
1830
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001831 appctx->ctx.stats.obj1 = NULL;
1832 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001833 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001834 appctx->st2 = 0;
1835 appctx->st1 = PROMEX_DUMPER_DONE;
1836 /* fall through */
1837
1838 case PROMEX_DUMPER_DONE:
1839 default:
1840 break;
1841 }
1842
1843 return 1;
1844
1845 full:
1846 si_rx_room_blk(si);
1847 return 0;
1848 error:
1849 /* unrecoverable error */
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001850 appctx->ctx.stats.obj1 = NULL;
1851 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletf959d082019-02-07 15:38:42 +01001852 appctx->ctx.stats.flags = 0;
1853 appctx->st2 = 0;
1854 appctx->st1 = PROMEX_DUMPER_DONE;
1855 return -1;
1856}
1857
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001858/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001859 * success and -1 on error. */
1860static int promex_parse_uri(struct appctx *appctx, struct stream_interface *si)
1861{
1862 struct channel *req = si_oc(si);
1863 struct channel *res = si_ic(si);
1864 struct htx *req_htx, *res_htx;
1865 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001866 char *p, *key, *value;
1867 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001868 struct buffer *err;
1869 int default_scopes = PROMEX_FL_SCOPE_ALL;
1870 int len;
1871
1872 /* Get the query-string */
1873 req_htx = htxbuf(&req->buf);
1874 sl = http_get_stline(req_htx);
1875 if (!sl)
1876 goto error;
1877 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1878 if (!p)
1879 goto end;
1880 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001881
William Dauchyc65f6562019-11-26 12:56:26 +01001882 /* copy the query-string */
1883 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001884 chunk_reset(&trash);
1885 memcpy(trash.area, p, len);
1886 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001887 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001888 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001889
1890 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001891 while (p < end && *p && *p != '#') {
1892 value = NULL;
1893
1894 /* decode parameter name */
1895 key = p;
1896 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001897 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001898 /* found a value */
1899 if (*p == '=') {
1900 *(p++) = 0;
1901 value = p;
1902 }
1903 else if (*p == '&')
1904 *(p++) = 0;
1905 else if (*p == '#')
1906 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001907 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001908 if (len == -1)
1909 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001910
William Dauchyc65f6562019-11-26 12:56:26 +01001911 /* decode value */
1912 if (value) {
1913 while (p < end && *p != '=' && *p != '&' && *p != '#')
1914 ++p;
1915 if (*p == '=')
1916 goto error;
1917 if (*p == '&')
1918 *(p++) = 0;
1919 else if (*p == '#')
1920 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001921 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001922 if (len == -1)
1923 goto error;
1924 }
1925
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001926 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001927 default_scopes = 0; /* at least a scope defined, unset default scopes */
1928 if (!value)
1929 goto error;
1930 else if (*value == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001931 appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001932 else if (*value == '*')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001933 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001934 else if (strcmp(value, "global") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001935 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001936 else if (strcmp(value, "server") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001937 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001938 else if (strcmp(value, "backend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001939 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001940 else if (strcmp(value, "frontend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001941 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT;
1942 else
1943 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001944 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001945 else if (strcmp(key, "no-maint") == 0)
Christopher Fauleteba22942019-11-19 14:18:24 +01001946 appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001947 }
1948
1949 end:
1950 appctx->ctx.stats.flags |= default_scopes;
1951 return 1;
1952
1953 error:
1954 err = &http_err_chunks[HTTP_ERR_400];
1955 channel_erase(res);
1956 res->buf.data = b_data(err);
1957 memcpy(res->buf.area, b_head(err), b_data(err));
1958 res_htx = htx_from_buf(&res->buf);
1959 channel_add_input(res, res_htx->data);
1960 appctx->st0 = PROMEX_ST_END;
1961 return -1;
1962}
1963
Christopher Fauletf959d082019-02-07 15:38:42 +01001964/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1965 * full. */
1966static int promex_send_headers(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1967{
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001968 struct channel *chn = si_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +01001969 struct htx_sl *sl;
1970 unsigned int flags;
1971
1972 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);
1973 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1974 if (!sl)
1975 goto full;
1976 sl->info.res.status = 200;
1977 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001978 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1979 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1980 !htx_add_endof(htx, HTX_BLK_EOH))
1981 goto full;
1982
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001983 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001984 return 1;
1985 full:
1986 htx_reset(htx);
1987 si_rx_room_blk(si);
1988 return 0;
1989}
1990
1991/* The function returns 1 if the initialisation is complete, 0 if
1992 * an errors occurs and -1 if more data are required for initializing
1993 * the applet.
1994 */
1995static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm)
1996{
1997 appctx->st0 = PROMEX_ST_INIT;
1998 return 1;
1999}
2000
2001/* The main I/O handler for the promex applet. */
2002static void promex_appctx_handle_io(struct appctx *appctx)
2003{
2004 struct stream_interface *si = appctx->owner;
2005 struct stream *s = si_strm(si);
2006 struct channel *req = si_oc(si);
2007 struct channel *res = si_ic(si);
2008 struct htx *req_htx, *res_htx;
2009 int ret;
2010
2011 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf959d082019-02-07 15:38:42 +01002012 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2013 goto out;
2014
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05002015 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01002016 if (!b_size(&res->buf)) {
2017 si_rx_room_blk(si);
2018 goto out;
2019 }
2020
2021 switch (appctx->st0) {
2022 case PROMEX_ST_INIT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01002023 ret = promex_parse_uri(appctx, si);
2024 if (ret <= 0) {
2025 if (ret == -1)
2026 goto error;
2027 goto out;
2028 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002029 appctx->st0 = PROMEX_ST_HEAD;
2030 appctx->st1 = PROMEX_DUMPER_INIT;
2031 /* fall through */
2032
2033 case PROMEX_ST_HEAD:
2034 if (!promex_send_headers(appctx, si, res_htx))
2035 goto out;
2036 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
2037 /* fall through */
2038
2039 case PROMEX_ST_DUMP:
2040 ret = promex_dump_metrics(appctx, si, res_htx);
2041 if (ret <= 0) {
2042 if (ret == -1)
2043 goto error;
2044 goto out;
2045 }
2046 appctx->st0 = PROMEX_ST_DONE;
2047 /* fall through */
2048
2049 case PROMEX_ST_DONE:
Christopher Faulet54b5e212019-06-04 10:08:28 +02002050 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02002051 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletf959d082019-02-07 15:38:42 +01002052 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
2053 si_rx_room_blk(si);
2054 goto out;
2055 }
Christopher Faulet9744f7c2019-03-27 15:48:53 +01002056 channel_add_input(res, 1);
2057 appctx->st0 = PROMEX_ST_END;
2058 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01002059
Christopher Faulet9744f7c2019-03-27 15:48:53 +01002060 case PROMEX_ST_END:
2061 if (!(res->flags & CF_SHUTR)) {
2062 res->flags |= CF_READ_NULL;
2063 si_shutr(si);
2064 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002065 }
2066
Christopher Fauletf959d082019-02-07 15:38:42 +01002067 out:
2068 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01002069
2070 /* eat the whole request */
2071 if (co_data(req)) {
2072 req_htx = htx_from_buf(&req->buf);
2073 co_htx_skip(req, req_htx, co_data(req));
2074 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002075 return;
2076
2077 error:
2078 res->flags |= CF_READ_NULL;
2079 si_shutr(si);
2080 si_shutw(si);
2081}
2082
2083struct applet promex_applet = {
2084 .obj_type = OBJ_TYPE_APPLET,
2085 .name = "<PROMEX>", /* used for logging */
2086 .init = promex_appctx_init,
2087 .fct = promex_appctx_handle_io,
2088};
2089
2090static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
2091 struct act_rule *rule, char **err)
2092{
2093 /* Prometheus exporter service is only available on "http-request" rulesets */
2094 if (rule->from != ACT_F_HTTP_REQ) {
2095 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
2096 return ACT_RET_PRS_ERR;
2097 }
Christopher Fauletf959d082019-02-07 15:38:42 +01002098
2099 /* Add applet pointer in the rule. */
2100 rule->applet = promex_applet;
2101
2102 return ACT_RET_PRS_OK;
2103}
2104static void promex_register_build_options(void)
2105{
2106 char *ptr = NULL;
2107
2108 memprintf(&ptr, "Built with the Prometheus exporter as a service");
2109 hap_register_build_opts(ptr, 1);
2110}
2111
2112
2113static struct action_kw_list service_actions = { ILH, {
2114 { "prometheus-exporter", service_parse_prometheus_exporter },
2115 { /* END */ }
2116}};
2117
2118INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
2119INITCALL0(STG_REGISTER, promex_register_build_options);