blob: 61e0549810d0a91d689591efb3d01fa8f5bafb56 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
34#include <common/ticks.h>
35#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38#include <common/base64.h>
39
40#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010041#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010042#include <types/global.h>
43#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045
46#include <proto/backend.h>
47#include <proto/channel.h>
48#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010049#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010050#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010051#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010052#include <proto/fd.h>
53#include <proto/freq_ctr.h>
54#include <proto/frontend.h>
55#include <proto/log.h>
56#include <proto/pattern.h>
57#include <proto/pipe.h>
58#include <proto/listener.h>
59#include <proto/map.h>
60#include <proto/proto_http.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010061#include <proto/proxy.h>
62#include <proto/sample.h>
63#include <proto/session.h>
64#include <proto/stream.h>
65#include <proto/server.h>
66#include <proto/raw_sock.h>
67#include <proto/stream_interface.h>
68#include <proto/task.h>
69
70#ifdef USE_OPENSSL
71#include <proto/ssl_sock.h>
72#include <types/ssl_sock.h>
73#endif
74
75
Willy Tarreau0baac8c2016-11-22 16:36:53 +010076/* These are the field names for each INF_* field position. Please pay attention
77 * to always use the exact same name except that the strings for new names must
78 * be lower case or CamelCase while the enum entries must be upper case.
79 */
80const char *info_field_names[INF_TOTAL_FIELDS] = {
81 [INF_NAME] = "Name",
82 [INF_VERSION] = "Version",
83 [INF_RELEASE_DATE] = "Release_date",
84 [INF_NBPROC] = "Nbproc",
85 [INF_PROCESS_NUM] = "Process_num",
86 [INF_PID] = "Pid",
87 [INF_UPTIME] = "Uptime",
88 [INF_UPTIME_SEC] = "Uptime_sec",
89 [INF_MEMMAX_MB] = "Memmax_MB",
90 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
91 [INF_POOL_USED_MB] = "PoolUsed_MB",
92 [INF_POOL_FAILED] = "PoolFailed",
93 [INF_ULIMIT_N] = "Ulimit-n",
94 [INF_MAXSOCK] = "Maxsock",
95 [INF_MAXCONN] = "Maxconn",
96 [INF_HARD_MAXCONN] = "Hard_maxconn",
97 [INF_CURR_CONN] = "CurrConns",
98 [INF_CUM_CONN] = "CumConns",
99 [INF_CUM_REQ] = "CumReq",
100 [INF_MAX_SSL_CONNS] = "MaxSslConns",
101 [INF_CURR_SSL_CONNS] = "CurrSslConns",
102 [INF_CUM_SSL_CONNS] = "CumSslConns",
103 [INF_MAXPIPES] = "Maxpipes",
104 [INF_PIPES_USED] = "PipesUsed",
105 [INF_PIPES_FREE] = "PipesFree",
106 [INF_CONN_RATE] = "ConnRate",
107 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
108 [INF_MAX_CONN_RATE] = "MaxConnRate",
109 [INF_SESS_RATE] = "SessRate",
110 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
111 [INF_MAX_SESS_RATE] = "MaxSessRate",
112 [INF_SSL_RATE] = "SslRate",
113 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
114 [INF_MAX_SSL_RATE] = "MaxSslRate",
115 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
116 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
117 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
118 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
119 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
120 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
121 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
122 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
123 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
124 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
125 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
126 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
127 [INF_TASKS] = "Tasks",
128 [INF_RUN_QUEUE] = "Run_queue",
129 [INF_IDLE_PCT] = "Idle_pct",
130 [INF_NODE] = "node",
131 [INF_DESCRIPTION] = "description",
132};
133
William Lallemand74c24fb2016-11-21 17:18:36 +0100134const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
135 [ST_F_PXNAME] = "pxname",
136 [ST_F_SVNAME] = "svname",
137 [ST_F_QCUR] = "qcur",
138 [ST_F_QMAX] = "qmax",
139 [ST_F_SCUR] = "scur",
140 [ST_F_SMAX] = "smax",
141 [ST_F_SLIM] = "slim",
142 [ST_F_STOT] = "stot",
143 [ST_F_BIN] = "bin",
144 [ST_F_BOUT] = "bout",
145 [ST_F_DREQ] = "dreq",
146 [ST_F_DRESP] = "dresp",
147 [ST_F_EREQ] = "ereq",
148 [ST_F_ECON] = "econ",
149 [ST_F_ERESP] = "eresp",
150 [ST_F_WRETR] = "wretr",
151 [ST_F_WREDIS] = "wredis",
152 [ST_F_STATUS] = "status",
153 [ST_F_WEIGHT] = "weight",
154 [ST_F_ACT] = "act",
155 [ST_F_BCK] = "bck",
156 [ST_F_CHKFAIL] = "chkfail",
157 [ST_F_CHKDOWN] = "chkdown",
158 [ST_F_LASTCHG] = "lastchg",
159 [ST_F_DOWNTIME] = "downtime",
160 [ST_F_QLIMIT] = "qlimit",
161 [ST_F_PID] = "pid",
162 [ST_F_IID] = "iid",
163 [ST_F_SID] = "sid",
164 [ST_F_THROTTLE] = "throttle",
165 [ST_F_LBTOT] = "lbtot",
166 [ST_F_TRACKED] = "tracked",
167 [ST_F_TYPE] = "type",
168 [ST_F_RATE] = "rate",
169 [ST_F_RATE_LIM] = "rate_lim",
170 [ST_F_RATE_MAX] = "rate_max",
171 [ST_F_CHECK_STATUS] = "check_status",
172 [ST_F_CHECK_CODE] = "check_code",
173 [ST_F_CHECK_DURATION] = "check_duration",
174 [ST_F_HRSP_1XX] = "hrsp_1xx",
175 [ST_F_HRSP_2XX] = "hrsp_2xx",
176 [ST_F_HRSP_3XX] = "hrsp_3xx",
177 [ST_F_HRSP_4XX] = "hrsp_4xx",
178 [ST_F_HRSP_5XX] = "hrsp_5xx",
179 [ST_F_HRSP_OTHER] = "hrsp_other",
180 [ST_F_HANAFAIL] = "hanafail",
181 [ST_F_REQ_RATE] = "req_rate",
182 [ST_F_REQ_RATE_MAX] = "req_rate_max",
183 [ST_F_REQ_TOT] = "req_tot",
184 [ST_F_CLI_ABRT] = "cli_abrt",
185 [ST_F_SRV_ABRT] = "srv_abrt",
186 [ST_F_COMP_IN] = "comp_in",
187 [ST_F_COMP_OUT] = "comp_out",
188 [ST_F_COMP_BYP] = "comp_byp",
189 [ST_F_COMP_RSP] = "comp_rsp",
190 [ST_F_LASTSESS] = "lastsess",
191 [ST_F_LAST_CHK] = "last_chk",
192 [ST_F_LAST_AGT] = "last_agt",
193 [ST_F_QTIME] = "qtime",
194 [ST_F_CTIME] = "ctime",
195 [ST_F_RTIME] = "rtime",
196 [ST_F_TTIME] = "ttime",
197 [ST_F_AGENT_STATUS] = "agent_status",
198 [ST_F_AGENT_CODE] = "agent_code",
199 [ST_F_AGENT_DURATION] = "agent_duration",
200 [ST_F_CHECK_DESC] = "check_desc",
201 [ST_F_AGENT_DESC] = "agent_desc",
202 [ST_F_CHECK_RISE] = "check_rise",
203 [ST_F_CHECK_FALL] = "check_fall",
204 [ST_F_CHECK_HEALTH] = "check_health",
205 [ST_F_AGENT_RISE] = "agent_rise",
206 [ST_F_AGENT_FALL] = "agent_fall",
207 [ST_F_AGENT_HEALTH] = "agent_health",
208 [ST_F_ADDR] = "addr",
209 [ST_F_COOKIE] = "cookie",
210 [ST_F_MODE] = "mode",
211 [ST_F_ALGO] = "algo",
212 [ST_F_CONN_RATE] = "conn_rate",
213 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
214 [ST_F_CONN_TOT] = "conn_tot",
215 [ST_F_INTERCEPTED] = "intercepted",
216 [ST_F_DCON] = "dcon",
217 [ST_F_DSES] = "dses",
218};
219
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100220/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100221static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100222/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100223static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100224
225
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100226
William Lallemand74c24fb2016-11-21 17:18:36 +0100227/*
228 * http_stats_io_handler()
229 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
230 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100231 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100232 * -> stats_dump_html_head() // emits the HTML headers
233 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
234 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
235 * -> stats_dump_html_px_hdr()
236 * -> stats_dump_fe_stats()
237 * -> stats_dump_li_stats()
238 * -> stats_dump_sv_stats()
239 * -> stats_dump_be_stats()
240 * -> stats_dump_html_px_end()
241 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100242 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100243 */
244
245
246extern const char *stat_status_codes[];
247
248/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
249 * for clearing it if needed.
250 * NOTE: Some tools happen to rely on the field position instead of its name,
251 * so please only append new fields at the end, never in the middle.
252 */
253static void stats_dump_csv_header()
254{
255 int field;
256
257 chunk_appendf(&trash, "# ");
258 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
259 chunk_appendf(&trash, "%s,", stat_field_names[field]);
260
261 chunk_appendf(&trash, "\n");
262}
263
264
265/* Emits a stats field without any surrounding element and properly encoded to
266 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
267 */
268int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
269{
270 switch (field_format(f, 0)) {
271 case FF_EMPTY: return 1;
272 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
273 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
274 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
275 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
276 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
277 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
278 }
279}
280
281/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
282 * output is supposed to be used on its own line. Returns non-zero on success, 0
283 * if the buffer is full.
284 */
285int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
286{
287 switch (field_format(f, 0)) {
288 case FF_EMPTY: return 1;
289 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
290 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
291 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
292 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
293 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
294 default: return chunk_appendf(out, "%08x:?", f->type);
295 }
296}
297
Simon Horman05ee2132017-01-04 09:37:25 +0100298/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
299 * the recommendation for interoperable integers in section 6 of RFC 7159.
300 */
301#define JSON_INT_MAX ((1ULL << 53) - 1)
302#define JSON_INT_MIN (0 - JSON_INT_MAX)
303
304/* Emits a stats field value and its type in JSON.
305 * Returns non-zero on success, 0 on error.
306 */
307int stats_emit_json_data_field(struct chunk *out, const struct field *f)
308{
309 int old_len;
310 char buf[20];
311 const char *type, *value = buf, *quote = "";
312
313 switch (field_format(f, 0)) {
314 case FF_EMPTY: return 1;
315 case FF_S32: type = "\"s32\"";
316 snprintf(buf, sizeof(buf), "%d", f->u.s32);
317 break;
318 case FF_U32: type = "\"u32\"";
319 snprintf(buf, sizeof(buf), "%u", f->u.u32);
320 break;
321 case FF_S64: type = "\"s64\"";
322 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
323 return 0;
324 type = "\"s64\"";
325 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
326 break;
327 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
328 return 0;
329 type = "\"u64\"";
330 snprintf(buf, sizeof(buf), "%llu",
331 (unsigned long long) f->u.u64);
332 break;
333 case FF_STR: type = "\"str\"";
334 value = field_str(f, 0);
335 quote = "\"";
336 break;
337 default: snprintf(buf, sizeof(buf), "%u", f->type);
338 type = buf;
339 value = "unknown";
340 quote = "\"";
341 break;
342 }
343
344 old_len = out->len;
345 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
346 type, quote, value, quote);
347 return !(old_len == out->len);
348}
349
William Lallemand74c24fb2016-11-21 17:18:36 +0100350/* Emits an encoding of the field type on 3 characters followed by a delimiter.
351 * Returns non-zero on success, 0 if the buffer is full.
352 */
353int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
354{
355 char origin, nature, scope;
356
357 switch (field_origin(f, 0)) {
358 case FO_METRIC: origin = 'M'; break;
359 case FO_STATUS: origin = 'S'; break;
360 case FO_KEY: origin = 'K'; break;
361 case FO_CONFIG: origin = 'C'; break;
362 case FO_PRODUCT: origin = 'P'; break;
363 default: origin = '?'; break;
364 }
365
366 switch (field_nature(f, 0)) {
367 case FN_GAUGE: nature = 'G'; break;
368 case FN_LIMIT: nature = 'L'; break;
369 case FN_MIN: nature = 'm'; break;
370 case FN_MAX: nature = 'M'; break;
371 case FN_RATE: nature = 'R'; break;
372 case FN_COUNTER: nature = 'C'; break;
373 case FN_DURATION: nature = 'D'; break;
374 case FN_AGE: nature = 'A'; break;
375 case FN_TIME: nature = 'T'; break;
376 case FN_NAME: nature = 'N'; break;
377 case FN_OUTPUT: nature = 'O'; break;
378 case FN_AVG: nature = 'a'; break;
379 default: nature = '?'; break;
380 }
381
382 switch (field_scope(f, 0)) {
383 case FS_PROCESS: scope = 'P'; break;
384 case FS_SERVICE: scope = 'S'; break;
385 case FS_SYSTEM: scope = 's'; break;
386 case FS_CLUSTER: scope = 'C'; break;
387 default: scope = '?'; break;
388 }
389
390 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
391}
392
Simon Horman05ee2132017-01-04 09:37:25 +0100393/* Emits an encoding of the field type as JSON.
394 * Returns non-zero on success, 0 if the buffer is full.
395 */
396int stats_emit_json_field_tags(struct chunk *out, const struct field *f)
397{
398 const char *origin, *nature, *scope;
399 int old_len;
400
401 switch (field_origin(f, 0)) {
402 case FO_METRIC: origin = "Metric"; break;
403 case FO_STATUS: origin = "Status"; break;
404 case FO_KEY: origin = "Key"; break;
405 case FO_CONFIG: origin = "Config"; break;
406 case FO_PRODUCT: origin = "Product"; break;
407 default: origin = "Unknown"; break;
408 }
409
410 switch (field_nature(f, 0)) {
411 case FN_GAUGE: nature = "Gauge"; break;
412 case FN_LIMIT: nature = "Limit"; break;
413 case FN_MIN: nature = "Min"; break;
414 case FN_MAX: nature = "Max"; break;
415 case FN_RATE: nature = "Rate"; break;
416 case FN_COUNTER: nature = "Counter"; break;
417 case FN_DURATION: nature = "Duration"; break;
418 case FN_AGE: nature = "Age"; break;
419 case FN_TIME: nature = "Time"; break;
420 case FN_NAME: nature = "Name"; break;
421 case FN_OUTPUT: nature = "Output"; break;
422 case FN_AVG: nature = "Avg"; break;
423 default: nature = "Unknown"; break;
424 }
425
426 switch (field_scope(f, 0)) {
427 case FS_PROCESS: scope = "Process"; break;
428 case FS_SERVICE: scope = "Service"; break;
429 case FS_SYSTEM: scope = "System"; break;
430 case FS_CLUSTER: scope = "Cluster"; break;
431 default: scope = "Unknown"; break;
432 }
433
434 old_len = out->len;
435 chunk_appendf(out, "\"tags\":{"
436 "\"origin\":\"%s\","
437 "\"nature\":\"%s\","
438 "\"scope\":\"%s\""
439 "}", origin, nature, scope);
440 return !(old_len == out->len);
441}
William Lallemand74c24fb2016-11-21 17:18:36 +0100442
443/* Dump all fields from <stats> into <out> using CSV format */
444static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
445{
446 int field;
447
448 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
449 if (!stats_emit_raw_data_field(out, &stats[field]))
450 return 0;
451 if (!chunk_strcat(out, ","))
452 return 0;
453 }
454 chunk_strcat(out, "\n");
455 return 1;
456}
457
458/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
459static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
460{
461 int field;
462
463 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
464 if (!stats[field].type)
465 continue;
466
467 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
468 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
469 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
470 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
471 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
472 '?',
473 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
474 field, stat_field_names[field], stats[ST_F_PID].u.u32);
475
476 if (!stats_emit_field_tags(out, &stats[field], ':'))
477 return 0;
478 if (!stats_emit_typed_data_field(out, &stats[field]))
479 return 0;
480 if (!chunk_strcat(out, "\n"))
481 return 0;
482 }
483 return 1;
484}
485
Simon Horman05ee2132017-01-04 09:37:25 +0100486/* Dump all fields from <stats> into <out> using the "show info json" format */
487static int stats_dump_json_info_fields(struct chunk *out,
488 const struct field *info)
489{
490 int field;
491 int started = 0;
492
493 if (!chunk_strcat(out, "["))
494 return 0;
495
496 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
497 int old_len;
498
499 if (!field_format(info, field))
500 continue;
501
502 if (started && !chunk_strcat(out, ","))
503 goto err;
504 started = 1;
505
506 old_len = out->len;
507 chunk_appendf(out,
508 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
509 "\"processNum\":%u,",
510 field, info_field_names[field],
511 info[INF_PROCESS_NUM].u.u32);
512 if (old_len == out->len)
513 goto err;
514
515 if (!stats_emit_json_field_tags(out, &info[field]))
516 goto err;
517
518 if (!stats_emit_json_data_field(out, &info[field]))
519 goto err;
520
521 if (!chunk_strcat(out, "}"))
522 goto err;
523 }
524
525 if (!chunk_strcat(out, "]"))
526 goto err;
527 return 1;
528
529err:
530 chunk_reset(out);
531 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
532 return 0;
533}
534
535/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
536static int stats_dump_fields_json(struct chunk *out, const struct field *stats,
537 int first_stat)
538{
539 int field;
540 int started = 0;
541
542 if (!first_stat && !chunk_strcat(out, ","))
543 return 0;
544 if (!chunk_strcat(out, "["))
545 return 0;
546
547 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
548 const char *obj_type;
549 int old_len;
550
551 if (!stats[field].type)
552 continue;
553
554 if (started && !chunk_strcat(out, ","))
555 goto err;
556 started = 1;
557
558 switch (stats[ST_F_TYPE].u.u32) {
559 case STATS_TYPE_FE: obj_type = "Frontend"; break;
560 case STATS_TYPE_BE: obj_type = "Backend"; break;
561 case STATS_TYPE_SO: obj_type = "Listener"; break;
562 case STATS_TYPE_SV: obj_type = "Server"; break;
563 default: obj_type = "Unknown"; break;
564 }
565
566 old_len = out->len;
567 chunk_appendf(out,
568 "{"
569 "\"objType\":\"%s\","
570 "\"proxyId\":%d,"
571 "\"id\":%d,"
572 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
573 "\"processNum\":%u,",
574 obj_type, stats[ST_F_IID].u.u32,
575 stats[ST_F_SID].u.u32, field,
576 stat_field_names[field], stats[ST_F_PID].u.u32);
577 if (old_len == out->len)
578 goto err;
579
580 if (!stats_emit_json_field_tags(out, &stats[field]))
581 goto err;
582
583 if (!stats_emit_json_data_field(out, &stats[field]))
584 goto err;
585
586 if (!chunk_strcat(out, "}"))
587 goto err;
588 }
589
590 if (!chunk_strcat(out, "]"))
591 goto err;
592
593 return 1;
594
595err:
596 chunk_reset(out);
597 if (!first_stat)
598 chunk_strcat(out, ",");
599 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
600 return 0;
601}
602
William Lallemand74c24fb2016-11-21 17:18:36 +0100603/* Dump all fields from <stats> into <out> using the HTML format. A column is
604 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
605 * are provided if ST_SHLGNDS is present in <flags>.
606 */
607static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
608{
609 struct chunk src;
610
611 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
612 chunk_appendf(out,
613 /* name, queue */
614 "<tr class=\"frontend\">");
615
616 if (flags & ST_SHOWADMIN) {
617 /* Column sub-heading for Enable or Disable server */
618 chunk_appendf(out, "<td></td>");
619 }
620
621 chunk_appendf(out,
622 "<td class=ac>"
623 "<a name=\"%s/Frontend\"></a>"
624 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
625 "<td colspan=3></td>"
626 "",
627 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
628
629 chunk_appendf(out,
630 /* sessions rate : current */
631 "<td><u>%s<div class=tips><table class=det>"
632 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
633 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
634 "",
635 U2H(stats[ST_F_RATE].u.u32),
636 U2H(stats[ST_F_CONN_RATE].u.u32),
637 U2H(stats[ST_F_RATE].u.u32));
638
639 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
640 chunk_appendf(out,
641 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
642 U2H(stats[ST_F_REQ_RATE].u.u32));
643
644 chunk_appendf(out,
645 "</table></div></u></td>"
646 /* sessions rate : max */
647 "<td><u>%s<div class=tips><table class=det>"
648 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
649 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
650 "",
651 U2H(stats[ST_F_RATE_MAX].u.u32),
652 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
653 U2H(stats[ST_F_RATE_MAX].u.u32));
654
655 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
656 chunk_appendf(out,
657 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
658 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
659
660 chunk_appendf(out,
661 "</table></div></u></td>"
662 /* sessions rate : limit */
663 "<td>%s</td>",
664 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
665
666 chunk_appendf(out,
667 /* sessions: current, max, limit, total */
668 "<td>%s</td><td>%s</td><td>%s</td>"
669 "<td><u>%s<div class=tips><table class=det>"
670 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
671 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
672 "",
673 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
674 U2H(stats[ST_F_STOT].u.u64),
675 U2H(stats[ST_F_CONN_TOT].u.u64),
676 U2H(stats[ST_F_STOT].u.u64));
677
678 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
679 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
680 chunk_appendf(out,
681 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
682 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
683 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
684 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
685 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
686 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
687 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
688 "<tr><th>- other responses:</th><td>%s</td></tr>"
689 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
690 "",
691 U2H(stats[ST_F_REQ_TOT].u.u64),
692 U2H(stats[ST_F_HRSP_1XX].u.u64),
693 U2H(stats[ST_F_HRSP_2XX].u.u64),
694 U2H(stats[ST_F_COMP_RSP].u.u64),
695 stats[ST_F_HRSP_2XX].u.u64 ?
696 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
697 U2H(stats[ST_F_HRSP_3XX].u.u64),
698 U2H(stats[ST_F_HRSP_4XX].u.u64),
699 U2H(stats[ST_F_HRSP_5XX].u.u64),
700 U2H(stats[ST_F_HRSP_OTHER].u.u64),
701 U2H(stats[ST_F_INTERCEPTED].u.u64));
702 }
703
704 chunk_appendf(out,
705 "</table></div></u></td>"
706 /* sessions: lbtot, lastsess */
707 "<td></td><td></td>"
708 /* bytes : in */
709 "<td>%s</td>"
710 "",
711 U2H(stats[ST_F_BIN].u.u64));
712
713 chunk_appendf(out,
714 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
715 "<td>%s%s<div class=tips><table class=det>"
716 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
717 "<tr><th>Compression in:</th><td>%s</td></tr>"
718 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
719 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
720 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
721 "</table></div>%s</td>",
722 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
723 U2H(stats[ST_F_BOUT].u.u64),
724 U2H(stats[ST_F_BOUT].u.u64),
725 U2H(stats[ST_F_COMP_IN].u.u64),
726 U2H(stats[ST_F_COMP_OUT].u.u64),
727 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
728 U2H(stats[ST_F_COMP_BYP].u.u64),
729 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
730 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
731 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
732
733 chunk_appendf(out,
734 /* denied: req, resp */
735 "<td>%s</td><td>%s</td>"
736 /* errors : request, connect, response */
737 "<td>%s</td><td></td><td></td>"
738 /* warnings: retries, redispatches */
739 "<td></td><td></td>"
740 /* server status : reflect frontend status */
741 "<td class=ac>%s</td>"
742 /* rest of server: nothing */
743 "<td class=ac colspan=8></td></tr>"
744 "",
745 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
746 U2H(stats[ST_F_EREQ].u.u64),
747 field_str(stats, ST_F_STATUS));
748 }
749 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
750 chunk_appendf(out, "<tr class=socket>");
751 if (flags & ST_SHOWADMIN) {
752 /* Column sub-heading for Enable or Disable server */
753 chunk_appendf(out, "<td></td>");
754 }
755
756 chunk_appendf(out,
757 /* frontend name, listener name */
758 "<td class=ac><a name=\"%s/+%s\"></a>%s"
759 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
760 "",
761 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
762 (flags & ST_SHLGNDS)?"<u>":"",
763 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
764
765 if (flags & ST_SHLGNDS) {
766 chunk_appendf(out, "<div class=tips>");
767
768 if (isdigit(*field_str(stats, ST_F_ADDR)))
769 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
770 else if (*field_str(stats, ST_F_ADDR) == '[')
771 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
772 else if (*field_str(stats, ST_F_ADDR))
773 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
774
775 /* id */
776 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
777 }
778
779 chunk_appendf(out,
780 /* queue */
781 "%s</td><td colspan=3></td>"
782 /* sessions rate: current, max, limit */
783 "<td colspan=3>&nbsp;</td>"
784 /* sessions: current, max, limit, total, lbtot, lastsess */
785 "<td>%s</td><td>%s</td><td>%s</td>"
786 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
787 /* bytes: in, out */
788 "<td>%s</td><td>%s</td>"
789 "",
790 (flags & ST_SHLGNDS)?"</u>":"",
791 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
792 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
793
794 chunk_appendf(out,
795 /* denied: req, resp */
796 "<td>%s</td><td>%s</td>"
797 /* errors: request, connect, response */
798 "<td>%s</td><td></td><td></td>"
799 /* warnings: retries, redispatches */
800 "<td></td><td></td>"
801 /* server status: reflect listener status */
802 "<td class=ac>%s</td>"
803 /* rest of server: nothing */
804 "<td class=ac colspan=8></td></tr>"
805 "",
806 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
807 U2H(stats[ST_F_EREQ].u.u64),
808 field_str(stats, ST_F_STATUS));
809 }
810 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
811 const char *style;
812
813 /* determine the style to use depending on the server's state,
814 * its health and weight. There isn't a 1-to-1 mapping between
815 * state and styles for the cases where the server is (still)
816 * up. The reason is that we don't want to report nolb and
817 * drain with the same color.
818 */
819
820 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
821 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
822 style = "down";
823 }
824 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
825 style = "going_up";
826 }
827 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
828 style = "going_down";
829 }
830 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
831 style = "nolb";
832 }
833 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
834 style = "no_check";
835 }
836 else if (!stats[ST_F_CHKFAIL].type ||
837 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
838 /* no check or max health = UP */
839 if (stats[ST_F_WEIGHT].u.u32)
840 style = "up";
841 else
842 style = "draining";
843 }
844 else {
845 style = "going_down";
846 }
847
848 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
849 chunk_appendf(out, "<tr class=\"maintain\">");
850 else
851 chunk_appendf(out,
852 "<tr class=\"%s_%s\">",
853 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
854
855
856 if (flags & ST_SHOWADMIN)
857 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000858 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
859 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100860 field_str(stats, ST_F_SVNAME));
861
862 chunk_appendf(out,
863 "<td class=ac><a name=\"%s/%s\"></a>%s"
864 "<a class=lfsb href=\"#%s/%s\">%s</a>"
865 "",
866 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
867 (flags & ST_SHLGNDS) ? "<u>" : "",
868 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
869
870 if (flags & ST_SHLGNDS) {
871 chunk_appendf(out, "<div class=tips>");
872
873 if (isdigit(*field_str(stats, ST_F_ADDR)))
874 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
875 else if (*field_str(stats, ST_F_ADDR) == '[')
876 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
877 else if (*field_str(stats, ST_F_ADDR))
878 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
879
880 /* id */
881 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
882
883 /* cookie */
884 if (stats[ST_F_COOKIE].type) {
885 chunk_appendf(out, ", cookie: '");
886 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
887 chunk_htmlencode(out, &src);
888 chunk_appendf(out, "'");
889 }
890
891 chunk_appendf(out, "</div>");
892 }
893
894 chunk_appendf(out,
895 /* queue : current, max, limit */
896 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
897 /* sessions rate : current, max, limit */
898 "<td>%s</td><td>%s</td><td></td>"
899 "",
900 (flags & ST_SHLGNDS) ? "</u>" : "",
901 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
902 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
903
904 chunk_appendf(out,
905 /* sessions: current, max, limit, total */
906 "<td>%s</td><td>%s</td><td>%s</td>"
907 "<td><u>%s<div class=tips><table class=det>"
908 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
909 "",
910 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
911 U2H(stats[ST_F_STOT].u.u64),
912 U2H(stats[ST_F_STOT].u.u64));
913
914 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
915 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
916 unsigned long long tot;
917
918 tot = stats[ST_F_HRSP_OTHER].u.u64;
919 tot += stats[ST_F_HRSP_1XX].u.u64;
920 tot += stats[ST_F_HRSP_2XX].u.u64;
921 tot += stats[ST_F_HRSP_3XX].u.u64;
922 tot += stats[ST_F_HRSP_4XX].u.u64;
923 tot += stats[ST_F_HRSP_5XX].u.u64;
924
925 chunk_appendf(out,
926 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
927 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
928 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
929 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
930 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
931 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
932 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
933 "",
934 U2H(tot),
935 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
936 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
937 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
938 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
939 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
940 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0);
941 }
942
943 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
944 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
945 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
946 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
947 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
948 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
949
950 chunk_appendf(out,
951 "</table></div></u></td>"
952 /* sessions: lbtot, last */
953 "<td>%s</td><td>%s</td>",
954 U2H(stats[ST_F_LBTOT].u.u64),
955 human_time(stats[ST_F_LASTSESS].u.s32, 1));
956
957 chunk_appendf(out,
958 /* bytes : in, out */
959 "<td>%s</td><td>%s</td>"
960 /* denied: req, resp */
961 "<td></td><td>%s</td>"
962 /* errors : request, connect */
963 "<td></td><td>%s</td>"
964 /* errors : response */
965 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
966 /* warnings: retries, redispatches */
967 "<td>%lld</td><td>%lld</td>"
968 "",
969 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
970 U2H(stats[ST_F_DRESP].u.u64),
971 U2H(stats[ST_F_ECON].u.u64),
972 U2H(stats[ST_F_ERESP].u.u64),
973 (long long)stats[ST_F_CLI_ABRT].u.u64,
974 (long long)stats[ST_F_SRV_ABRT].u.u64,
975 (long long)stats[ST_F_WRETR].u.u64,
976 (long long)stats[ST_F_WREDIS].u.u64);
977
978 /* status, last change */
979 chunk_appendf(out, "<td class=ac>");
980
981 /* FIXME!!!!
982 * LASTCHG should contain the last change for *this* server and must be computed
983 * properly above, as was done below, ie: this server if maint, otherwise ref server
984 * if tracking. Note that ref is either local or remote depending on tracking.
985 */
986
987
988 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
989 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
990 }
991 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
992 chunk_strcat(out, "<i>no check</i>");
993 }
994 else {
995 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
996 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
997 if (stats[ST_F_CHECK_HEALTH].u.u32)
998 chunk_strcat(out, " &uarr;");
999 }
1000 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1001 chunk_strcat(out, " &darr;");
1002 }
1003
1004 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1005 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1006 chunk_appendf(out,
1007 "</td><td class=ac><u> %s",
1008 field_str(stats, ST_F_AGENT_STATUS));
1009
1010 if (stats[ST_F_AGENT_CODE].type)
1011 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1012
1013 if (stats[ST_F_AGENT_DURATION].type)
1014 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1015
1016 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1017
1018 if (*field_str(stats, ST_F_LAST_AGT)) {
1019 chunk_appendf(out, ": ");
1020 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1021 chunk_htmlencode(out, &src);
1022 }
1023 chunk_appendf(out, "</div></u>");
1024 }
1025 else if (stats[ST_F_CHECK_STATUS].type) {
1026 chunk_appendf(out,
1027 "</td><td class=ac><u> %s",
1028 field_str(stats, ST_F_CHECK_STATUS));
1029
1030 if (stats[ST_F_CHECK_CODE].type)
1031 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1032
1033 if (stats[ST_F_CHECK_DURATION].type)
1034 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1035
1036 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1037
1038 if (*field_str(stats, ST_F_LAST_CHK)) {
1039 chunk_appendf(out, ": ");
1040 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1041 chunk_htmlencode(out, &src);
1042 }
1043 chunk_appendf(out, "</div></u>");
1044 }
1045 else
1046 chunk_appendf(out, "</td><td>");
1047
1048 chunk_appendf(out,
1049 /* weight */
1050 "</td><td class=ac>%d</td>"
1051 /* act, bck */
1052 "<td class=ac>%s</td><td class=ac>%s</td>"
1053 "",
1054 stats[ST_F_WEIGHT].u.u32,
1055 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1056 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1057
1058 /* check failures: unique, fatal, down time */
1059 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1060 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1061 }
1062 else if (stats[ST_F_CHKFAIL].type) {
1063 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1064
1065 if (stats[ST_F_HANAFAIL].type)
1066 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1067
1068 chunk_appendf(out,
1069 "<div class=tips>Failed Health Checks%s</div></u></td>"
1070 "<td>%lld</td><td>%s</td>"
1071 "",
1072 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1073 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1074 }
1075 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1076 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1077 chunk_appendf(out,
1078 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1079 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1080 }
1081 else
1082 chunk_appendf(out, "<td colspan=3></td>");
1083
1084 /* throttle */
1085 if (stats[ST_F_THROTTLE].type)
1086 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1087 else
1088 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1089 }
1090 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1091 chunk_appendf(out, "<tr class=\"backend\">");
1092 if (flags & ST_SHOWADMIN) {
1093 /* Column sub-heading for Enable or Disable server */
1094 chunk_appendf(out, "<td></td>");
1095 }
1096 chunk_appendf(out,
1097 "<td class=ac>"
1098 /* name */
1099 "%s<a name=\"%s/Backend\"></a>"
1100 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1101 "",
1102 (flags & ST_SHLGNDS)?"<u>":"",
1103 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1104
1105 if (flags & ST_SHLGNDS) {
1106 /* balancing */
1107 chunk_appendf(out, "<div class=tips>balancing: %s",
1108 field_str(stats, ST_F_ALGO));
1109
1110 /* cookie */
1111 if (stats[ST_F_COOKIE].type) {
1112 chunk_appendf(out, ", cookie: '");
1113 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1114 chunk_htmlencode(out, &src);
1115 chunk_appendf(out, "'");
1116 }
1117 chunk_appendf(out, "</div>");
1118 }
1119
1120 chunk_appendf(out,
1121 "%s</td>"
1122 /* queue : current, max */
1123 "<td>%s</td><td>%s</td><td></td>"
1124 /* sessions rate : current, max, limit */
1125 "<td>%s</td><td>%s</td><td></td>"
1126 "",
1127 (flags & ST_SHLGNDS)?"</u>":"",
1128 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1129 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1130
1131 chunk_appendf(out,
1132 /* sessions: current, max, limit, total */
1133 "<td>%s</td><td>%s</td><td>%s</td>"
1134 "<td><u>%s<div class=tips><table class=det>"
1135 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1136 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001137 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
William Lallemand74c24fb2016-11-21 17:18:36 +01001138 U2H(stats[ST_F_STOT].u.u64),
1139 U2H(stats[ST_F_STOT].u.u64));
1140
1141 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1142 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1143 chunk_appendf(out,
1144 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1145 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1146 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1147 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1148 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1149 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1150 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1151 "<tr><th>- other responses:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001152 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1153 "",
1154 U2H(stats[ST_F_REQ_TOT].u.u64),
1155 U2H(stats[ST_F_HRSP_1XX].u.u64),
1156 U2H(stats[ST_F_HRSP_2XX].u.u64),
1157 U2H(stats[ST_F_COMP_RSP].u.u64),
1158 stats[ST_F_HRSP_2XX].u.u64 ?
1159 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1160 U2H(stats[ST_F_HRSP_3XX].u.u64),
1161 U2H(stats[ST_F_HRSP_4XX].u.u64),
1162 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreauae9bea02016-11-25 14:44:52 +01001163 U2H(stats[ST_F_HRSP_OTHER].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001164 }
1165
1166 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1167 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1168 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1169 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1170 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1171
1172 chunk_appendf(out,
1173 "</table></div></u></td>"
1174 /* sessions: lbtot, last */
1175 "<td>%s</td><td>%s</td>"
1176 /* bytes: in */
1177 "<td>%s</td>"
1178 "",
1179 U2H(stats[ST_F_LBTOT].u.u64),
1180 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1181 U2H(stats[ST_F_BIN].u.u64));
1182
1183 chunk_appendf(out,
1184 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1185 "<td>%s%s<div class=tips><table class=det>"
1186 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1187 "<tr><th>Compression in:</th><td>%s</td></tr>"
1188 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1189 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1190 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1191 "</table></div>%s</td>",
1192 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1193 U2H(stats[ST_F_BOUT].u.u64),
1194 U2H(stats[ST_F_BOUT].u.u64),
1195 U2H(stats[ST_F_COMP_IN].u.u64),
1196 U2H(stats[ST_F_COMP_OUT].u.u64),
1197 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1198 U2H(stats[ST_F_COMP_BYP].u.u64),
1199 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1200 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
1201 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1202
1203 chunk_appendf(out,
1204 /* denied: req, resp */
1205 "<td>%s</td><td>%s</td>"
1206 /* errors : request, connect */
1207 "<td></td><td>%s</td>"
1208 /* errors : response */
1209 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1210 /* warnings: retries, redispatches */
1211 "<td>%lld</td><td>%lld</td>"
1212 /* backend status: reflect backend status (up/down): we display UP
1213 * if the backend has known working servers or if it has no server at
1214 * all (eg: for stats). Then we display the total weight, number of
1215 * active and backups. */
1216 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1217 "<td class=ac>%d</td><td class=ac>%d</td>"
1218 "",
1219 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1220 U2H(stats[ST_F_ECON].u.u64),
1221 U2H(stats[ST_F_ERESP].u.u64),
1222 (long long)stats[ST_F_CLI_ABRT].u.u64,
1223 (long long)stats[ST_F_SRV_ABRT].u.u64,
1224 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1225 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1226 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1227 stats[ST_F_WEIGHT].u.u32,
1228 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1229
1230 chunk_appendf(out,
1231 /* rest of backend: nothing, down transitions, total downtime, throttle */
1232 "<td class=ac>&nbsp;</td><td>%d</td>"
1233 "<td>%s</td>"
1234 "<td></td>"
1235 "</tr>",
1236 stats[ST_F_CHKDOWN].u.u32,
1237 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1238 }
1239 return 1;
1240}
1241
1242int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1243{
Simon Horman05ee2132017-01-04 09:37:25 +01001244 int ret;
1245
William Lallemand74c24fb2016-11-21 17:18:36 +01001246 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1247 flags |= ST_SHOWADMIN;
1248
1249 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001250 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001251 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001252 ret = stats_dump_fields_typed(&trash, stats);
1253 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1254 ret = stats_dump_fields_json(&trash, stats,
1255 !(appctx->ctx.stats.flags &
1256 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001257 else
Simon Horman05ee2132017-01-04 09:37:25 +01001258 ret = stats_dump_fields_csv(&trash, stats);
1259
1260 if (ret)
1261 appctx->ctx.stats.flags |= STAT_STARTED;
1262
1263 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001264}
1265
1266/* Fill <stats> with the frontend statistics. <stats> is
1267 * preallocated array of length <len>. The length of the array
1268 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1269 * this value, the function returns 0, otherwise, it returns 1.
1270 */
1271int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1272{
1273 if (len < ST_F_TOTAL_FIELDS)
1274 return 0;
1275
1276 memset(stats, 0, sizeof(*stats) * len);
1277
1278 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1279 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1280 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1281 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1282 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1283 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1284 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1285 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1286 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1287 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1288 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1289 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1290 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1291 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1292 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1293 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1294 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1295 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1296 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1297 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1298 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1299 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1300
1301 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1302 if (px->mode == PR_MODE_HTTP) {
1303 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1304 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1305 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1306 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1307 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1308 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1309 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
1310 }
1311
1312 /* requests : req_rate, req_rate_max, req_tot, */
1313 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1314 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1315 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1316
1317 /* compression: in, out, bypassed, responses */
1318 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1319 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1320 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1321 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1322
1323 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1324 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1325 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1326 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1327
1328 return 1;
1329}
1330
1331/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1332 * the state from stream interface <si>. The caller is responsible for clearing
1333 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1334 */
1335static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1336{
1337 struct appctx *appctx = __objt_appctx(si->end);
1338
1339 if (!(px->cap & PR_CAP_FE))
1340 return 0;
1341
1342 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1343 return 0;
1344
1345 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1346 return 0;
1347
1348 return stats_dump_one_line(stats, 0, px, appctx);
1349}
1350
1351/* Fill <stats> with the listener statistics. <stats> is
1352 * preallocated array of length <len>. The length of the array
1353 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1354 * then this value, the function returns 0, otherwise, it
1355 * returns 1. <flags> can take the value ST_SHLGNDS.
1356 */
1357int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1358 struct field *stats, int len)
1359{
1360 struct chunk *out = get_trash_chunk();
1361
1362 if (len < ST_F_TOTAL_FIELDS)
1363 return 0;
1364
1365 if (!l->counters)
1366 return 0;
1367
1368 chunk_reset(out);
1369 memset(stats, 0, sizeof(*stats) * len);
1370
1371 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1372 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1373 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1374 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1375 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1376 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1377 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1378 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1379 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1380 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1381 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1382 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1383 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1384 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1385 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
1386 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1387 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1388 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1389 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1390
1391 if (flags & ST_SHLGNDS) {
1392 char str[INET6_ADDRSTRLEN];
1393 int port;
1394
1395 port = get_host_port(&l->addr);
1396 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1397 case AF_INET:
1398 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1399 chunk_appendf(out, "%s:%d", str, port);
1400 break;
1401 case AF_INET6:
1402 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1403 chunk_appendf(out, "[%s]:%d", str, port);
1404 break;
1405 case AF_UNIX:
1406 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1407 break;
1408 case -1:
1409 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1410 chunk_strcat(out, strerror(errno));
1411 break;
1412 default: /* address family not supported */
1413 break;
1414 }
1415 }
1416
1417 return 1;
1418}
1419
1420/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1421 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1422 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1423 * otherwise.
1424 */
1425static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1426{
1427 struct appctx *appctx = __objt_appctx(si->end);
1428
1429 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1430 return 0;
1431
1432 return stats_dump_one_line(stats, flags, px, appctx);
1433}
1434
1435enum srv_stats_state {
1436 SRV_STATS_STATE_DOWN = 0,
1437 SRV_STATS_STATE_DOWN_AGENT,
1438 SRV_STATS_STATE_GOING_UP,
1439 SRV_STATS_STATE_UP_GOING_DOWN,
1440 SRV_STATS_STATE_UP,
1441 SRV_STATS_STATE_NOLB_GOING_DOWN,
1442 SRV_STATS_STATE_NOLB,
1443 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1444 SRV_STATS_STATE_DRAIN,
1445 SRV_STATS_STATE_DRAIN_AGENT,
1446 SRV_STATS_STATE_NO_CHECK,
1447
1448 SRV_STATS_STATE_COUNT, /* Must be last */
1449};
1450
1451static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1452 [SRV_STATS_STATE_DOWN] = "DOWN",
1453 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1454 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1455 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1456 [SRV_STATS_STATE_UP] = "UP",
1457 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1458 [SRV_STATS_STATE_NOLB] = "NOLB",
1459 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1460 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1461 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1462 [SRV_STATS_STATE_NO_CHECK] = "no check"
1463};
1464
1465/* Fill <stats> with the server statistics. <stats> is
1466 * preallocated array of length <len>. The length of the array
1467 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1468 * then this value, the function returns 0, otherwise, it
1469 * returns 1. <flags> can take the value ST_SHLGNDS.
1470 */
1471int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1472 struct field *stats, int len)
1473{
1474 struct server *via, *ref;
1475 char str[INET6_ADDRSTRLEN];
1476 struct chunk *out = get_trash_chunk();
1477 enum srv_stats_state state;
1478 char *fld_status;
1479
1480 if (len < ST_F_TOTAL_FIELDS)
1481 return 0;
1482
1483 memset(stats, 0, sizeof(*stats) * len);
1484
1485 /* we have "via" which is the tracked server as described in the configuration,
1486 * and "ref" which is the checked server and the end of the chain.
1487 */
1488 via = sv->track ? sv->track : sv;
1489 ref = via;
1490 while (ref->track)
1491 ref = ref->track;
1492
Emeric Brun52a91d32017-08-31 14:41:55 +02001493 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001494 if ((ref->check.state & CHK_ST_ENABLED) &&
1495 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1496 state = SRV_STATS_STATE_UP_GOING_DOWN;
1497 } else {
1498 state = SRV_STATS_STATE_UP;
1499 }
1500
Emeric Brun52a91d32017-08-31 14:41:55 +02001501 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001502 if (ref->agent.state & CHK_ST_ENABLED)
1503 state = SRV_STATS_STATE_DRAIN_AGENT;
1504 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1505 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1506 else
1507 state = SRV_STATS_STATE_DRAIN;
1508 }
1509
1510 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1511 state = SRV_STATS_STATE_NO_CHECK;
1512 }
1513 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001514 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001515 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1516 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1517 state = SRV_STATS_STATE_NOLB;
1518 } else {
1519 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1520 }
1521 }
1522 else { /* stopped */
1523 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1524 state = SRV_STATS_STATE_DOWN_AGENT;
1525 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1526 state = SRV_STATS_STATE_DOWN; /* DOWN */
1527 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1528 state = SRV_STATS_STATE_GOING_UP;
1529 } else {
1530 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1531 }
1532 }
1533
1534 chunk_reset(out);
1535
1536 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1537 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1538 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1539 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1540 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1541 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1542 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1543
1544 if (sv->maxconn)
1545 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1546
1547 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1548 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1549 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1550 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1551 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1552 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1553 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1554 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
1555
1556 /* status */
1557 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001558 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001559 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001560 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001561 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001562 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001563 chunk_appendf(out, "MAINT");
1564 else
1565 chunk_appendf(out,
1566 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001567 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1568 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001569
1570 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1571 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001572 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
William Lallemand74c24fb2016-11-21 17:18:36 +01001573 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1574 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1575
1576 /* check failures: unique, fatal; last change, total downtime */
1577 if (sv->check.state & CHK_ST_ENABLED) {
1578 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1579 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1580 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1581 }
1582
1583 if (sv->maxqueue)
1584 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1585
1586 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1587 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1588 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1589
Emeric Brun52a91d32017-08-31 14:41:55 +02001590 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001591 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1592
1593 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1594
1595 if (sv->track) {
1596 char *fld_track = chunk_newstr(out);
1597
1598 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1599 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1600 }
1601
1602 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1603 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1604 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1605
1606 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1607 const char *fld_chksts;
1608
1609 fld_chksts = chunk_newstr(out);
1610 chunk_strcat(out, "* "); // for check in progress
1611 chunk_strcat(out, get_check_status_info(sv->check.status));
1612 if (!(sv->check.state & CHK_ST_INPROGRESS))
1613 fld_chksts += 2; // skip "* "
1614 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1615
1616 if (sv->check.status >= HCHK_STATUS_L57DATA)
1617 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1618
1619 if (sv->check.status >= HCHK_STATUS_CHECKED)
1620 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1621
1622 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1623 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1624 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1625 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1626 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1627 }
1628
1629 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1630 const char *fld_chksts;
1631
1632 fld_chksts = chunk_newstr(out);
1633 chunk_strcat(out, "* "); // for check in progress
1634 chunk_strcat(out, get_check_status_info(sv->agent.status));
1635 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1636 fld_chksts += 2; // skip "* "
1637 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1638
1639 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1640 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1641
1642 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1643 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1644
1645 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1646 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1647 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1648 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1649 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1650 }
1651
1652 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1653 if (px->mode == PR_MODE_HTTP) {
1654 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1655 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1656 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1657 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1658 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1659 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1660 }
1661
1662 if (ref->observe)
1663 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1664
1665 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1666 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1667 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1668
1669 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1670 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1671 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1672 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1673
1674 if (flags & ST_SHLGNDS) {
1675 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1676 case AF_INET:
1677 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001678 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001679 break;
1680 case AF_INET6:
1681 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001682 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001683 break;
1684 case AF_UNIX:
1685 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1686 break;
1687 case -1:
1688 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1689 chunk_strcat(out, strerror(errno));
1690 break;
1691 default: /* address family not supported */
1692 break;
1693 }
1694
1695 if (sv->cookie)
1696 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1697 }
1698
1699 return 1;
1700}
1701
1702/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1703 * from stream interface <si>, stats flags <flags>, and server state <state>.
1704 * The caller is responsible for clearing the trash if needed. Returns non-zero
1705 * if it emits anything, zero otherwise.
1706 */
1707static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1708{
1709 struct appctx *appctx = __objt_appctx(si->end);
1710
1711 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1712 return 0;
1713
1714 return stats_dump_one_line(stats, flags, px, appctx);
1715}
1716
1717/* Fill <stats> with the backend statistics. <stats> is
1718 * preallocated array of length <len>. The length of the array
1719 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1720 * then this value, the function returns 0, otherwise, it
1721 * returns 1. <flags> can take the value ST_SHLGNDS.
1722 */
1723int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1724{
1725 if (len < ST_F_TOTAL_FIELDS)
1726 return 0;
1727
1728 memset(stats, 0, sizeof(*stats) * len);
1729
1730 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1731 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1732 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1733 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1734 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001735 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001736 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1737 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1738 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1739 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1740 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1741 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1742 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1743 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1744 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1745 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1746 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
1747 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1748 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1749 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1750 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1751 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1752 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1753 if (px->srv)
1754 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1755
1756 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1757 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1758 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1759 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1760 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1761 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1762 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1763
1764 if (flags & ST_SHLGNDS) {
1765 if (px->cookie_name)
1766 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1767 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1768 }
1769
1770 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1771 if (px->mode == PR_MODE_HTTP) {
1772 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1773 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1774 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1775 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1776 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1777 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1778 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001779 }
1780
1781 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1782 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1783
1784 /* compression: in, out, bypassed, responses */
1785 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1786 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1787 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1788 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1789 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1790
1791 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1792 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1793 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1794 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1795
1796 return 1;
1797}
1798
1799/* Dumps a line for backend <px> to the trash for and uses the state from stream
1800 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1801 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1802 */
1803static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1804{
1805 struct appctx *appctx = __objt_appctx(si->end);
1806
1807 if (!(px->cap & PR_CAP_BE))
1808 return 0;
1809
1810 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1811 return 0;
1812
1813 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1814 return 0;
1815
1816 return stats_dump_one_line(stats, flags, px, appctx);
1817}
1818
1819/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1820 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1821 * for clearing the trash if needed.
1822 */
1823static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1824{
1825 struct appctx *appctx = __objt_appctx(si->end);
1826 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1827
1828 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1829 /* A form to enable/disable this proxy servers */
1830
1831 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1832 scope_txt[0] = 0;
1833 if (appctx->ctx.stats.scope_len) {
1834 strcpy(scope_txt, STAT_SCOPE_PATTERN);
1835 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
1836 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1837 }
1838
1839 chunk_appendf(&trash,
1840 "<form method=\"post\">");
1841 }
1842
1843 /* print a new table */
1844 chunk_appendf(&trash,
1845 "<table class=\"tbl\" width=\"100%%\">\n"
1846 "<tr class=\"titre\">"
1847 "<th class=\"pxname\" width=\"10%%\">");
1848
1849 chunk_appendf(&trash,
1850 "<a name=\"%s\"></a>%s"
1851 "<a class=px href=\"#%s\">%s</a>",
1852 px->id,
1853 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1854 px->id, px->id);
1855
1856 if (uri->flags & ST_SHLGNDS) {
1857 /* cap, mode, id */
1858 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1859 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1860 px->uuid);
1861 chunk_appendf(&trash, "</div>");
1862 }
1863
1864 chunk_appendf(&trash,
1865 "%s</th>"
1866 "<th class=\"%s\" width=\"90%%\">%s</th>"
1867 "</tr>\n"
1868 "</table>\n"
1869 "<table class=\"tbl\" width=\"100%%\">\n"
1870 "<tr class=\"titre\">",
1871 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1872 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1873
1874 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1875 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001876 chunk_appendf(&trash,
1877 "<th rowspan=2 width=1><input type=\"checkbox\" \
1878 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1879 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1880 px->id,
1881 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001882 }
1883
1884 chunk_appendf(&trash,
1885 "<th rowspan=2></th>"
1886 "<th colspan=3>Queue</th>"
1887 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1888 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1889 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1890 "<th colspan=9>Server</th>"
1891 "</tr>\n"
1892 "<tr class=\"titre\">"
1893 "<th>Cur</th><th>Max</th><th>Limit</th>"
1894 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1895 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1896 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1897 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1898 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
1899 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1900 "<th>Thrtle</th>\n"
1901 "</tr>");
1902}
1903
1904/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
1905 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1906 */
1907static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
1908{
1909 struct appctx *appctx = __objt_appctx(si->end);
1910 chunk_appendf(&trash, "</table>");
1911
1912 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1913 /* close the form used to enable/disable this proxy servers */
1914 chunk_appendf(&trash,
1915 "Choose the action to perform on the checked servers : "
1916 "<select name=action>"
1917 "<option value=\"\"></option>"
1918 "<option value=\"ready\">Set state to READY</option>"
1919 "<option value=\"drain\">Set state to DRAIN</option>"
1920 "<option value=\"maint\">Set state to MAINT</option>"
1921 "<option value=\"dhlth\">Health: disable checks</option>"
1922 "<option value=\"ehlth\">Health: enable checks</option>"
1923 "<option value=\"hrunn\">Health: force UP</option>"
1924 "<option value=\"hnolb\">Health: force NOLB</option>"
1925 "<option value=\"hdown\">Health: force DOWN</option>"
1926 "<option value=\"dagent\">Agent: disable checks</option>"
1927 "<option value=\"eagent\">Agent: enable checks</option>"
1928 "<option value=\"arunn\">Agent: force UP</option>"
1929 "<option value=\"adown\">Agent: force DOWN</option>"
1930 "<option value=\"shutdown\">Kill Sessions</option>"
1931 "</select>"
1932 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
1933 "&nbsp;<input type=\"submit\" value=\"Apply\">"
1934 "</form>",
1935 px->uuid);
1936 }
1937
1938 chunk_appendf(&trash, "<p>\n");
1939}
1940
1941/*
1942 * Dumps statistics for a proxy. The output is sent to the stream interface's
1943 * input buffer. Returns 0 if it had to stop dumping data because of lack of
1944 * buffer space, or non-zero if everything completed. This function is used
1945 * both by the CLI and the HTTP entry points, and is able to dump the output
1946 * in HTML or CSV formats. If the later, <uri> must be NULL.
1947 */
1948int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1949{
1950 struct appctx *appctx = __objt_appctx(si->end);
1951 struct stream *s = si_strm(si);
1952 struct channel *rep = si_ic(si);
1953 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
1954 struct listener *l;
1955 unsigned int flags;
1956
1957 if (uri)
1958 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02001959 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01001960 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
1961 else
1962 flags = ST_SHNODE | ST_SHDESC;
1963
1964 chunk_reset(&trash);
1965
1966 switch (appctx->ctx.stats.px_st) {
1967 case STAT_PX_ST_INIT:
1968 /* we are on a new proxy */
1969 if (uri && uri->scope) {
1970 /* we have a limited scope, we have to check the proxy name */
1971 struct stat_scope *scope;
1972 int len;
1973
1974 len = strlen(px->id);
1975 scope = uri->scope;
1976
1977 while (scope) {
1978 /* match exact proxy name */
1979 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1980 break;
1981
1982 /* match '.' which means 'self' proxy */
1983 if (!strcmp(scope->px_id, ".") && px == s->be)
1984 break;
1985 scope = scope->next;
1986 }
1987
1988 /* proxy name not found : don't dump anything */
1989 if (scope == NULL)
1990 return 1;
1991 }
1992
1993 /* if the user has requested a limited output and the proxy
1994 * name does not match, skip it.
1995 */
1996 if (appctx->ctx.stats.scope_len &&
1997 strnistr(px->id, strlen(px->id), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
1998 return 1;
1999
2000 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2001 (appctx->ctx.stats.iid != -1) &&
2002 (px->uuid != appctx->ctx.stats.iid))
2003 return 1;
2004
2005 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2006 /* fall through */
2007
2008 case STAT_PX_ST_TH:
2009 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2010 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002011 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002012 si_applet_cant_put(si);
2013 return 0;
2014 }
2015 }
2016
2017 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2018 /* fall through */
2019
2020 case STAT_PX_ST_FE:
2021 /* print the frontend */
2022 if (stats_dump_fe_stats(si, px)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002023 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002024 si_applet_cant_put(si);
2025 return 0;
2026 }
2027 }
2028
2029 appctx->ctx.stats.l = px->conf.listeners.n;
2030 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2031 /* fall through */
2032
2033 case STAT_PX_ST_LI:
2034 /* stats.l has been initialized above */
2035 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
2036 if (buffer_almost_full(rep->buf)) {
2037 si_applet_cant_put(si);
2038 return 0;
2039 }
2040
2041 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2042 if (!l->counters)
2043 continue;
2044
2045 if (appctx->ctx.stats.flags & STAT_BOUND) {
2046 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2047 break;
2048
2049 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2050 continue;
2051 }
2052
2053 /* print the frontend */
2054 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002055 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002056 si_applet_cant_put(si);
2057 return 0;
2058 }
2059 }
2060 }
2061
2062 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2063 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2064 /* fall through */
2065
2066 case STAT_PX_ST_SV:
2067 /* stats.sv has been initialized above */
2068 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
2069 if (buffer_almost_full(rep->buf)) {
2070 si_applet_cant_put(si);
2071 return 0;
2072 }
2073
2074 sv = appctx->ctx.stats.sv;
2075
2076 if (appctx->ctx.stats.flags & STAT_BOUND) {
2077 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2078 break;
2079
2080 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2081 continue;
2082 }
2083
2084 svs = sv;
2085 while (svs->track)
2086 svs = svs->track;
2087
2088 /* do not report servers which are DOWN and not changing state */
2089 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002090 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2091 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002092 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2093 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2094 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2095 continue;
2096 }
2097
2098 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002099 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002100 si_applet_cant_put(si);
2101 return 0;
2102 }
2103 }
2104 } /* for sv */
2105
2106 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2107 /* fall through */
2108
2109 case STAT_PX_ST_BE:
2110 /* print the backend */
2111 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002112 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002113 si_applet_cant_put(si);
2114 return 0;
2115 }
2116 }
2117
2118 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2119 /* fall through */
2120
2121 case STAT_PX_ST_END:
2122 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2123 stats_dump_html_px_end(si, px);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002124 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002125 si_applet_cant_put(si);
2126 return 0;
2127 }
2128 }
2129
2130 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2131 /* fall through */
2132
2133 case STAT_PX_ST_FIN:
2134 return 1;
2135
2136 default:
2137 /* unknown state, we should put an abort() here ! */
2138 return 1;
2139 }
2140}
2141
2142/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2143 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2144 */
2145static void stats_dump_html_head(struct uri_auth *uri)
2146{
2147 /* WARNING! This must fit in the first buffer !!! */
2148 chunk_appendf(&trash,
2149 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2150 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2151 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2152 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2153 "<style type=\"text/css\"><!--\n"
2154 "body {"
2155 " font-family: arial, helvetica, sans-serif;"
2156 " font-size: 12px;"
2157 " font-weight: normal;"
2158 " color: black;"
2159 " background: white;"
2160 "}\n"
2161 "th,td {"
2162 " font-size: 10px;"
2163 "}\n"
2164 "h1 {"
2165 " font-size: x-large;"
2166 " margin-bottom: 0.5em;"
2167 "}\n"
2168 "h2 {"
2169 " font-family: helvetica, arial;"
2170 " font-size: x-large;"
2171 " font-weight: bold;"
2172 " font-style: italic;"
2173 " color: #6020a0;"
2174 " margin-top: 0em;"
2175 " margin-bottom: 0em;"
2176 "}\n"
2177 "h3 {"
2178 " font-family: helvetica, arial;"
2179 " font-size: 16px;"
2180 " font-weight: bold;"
2181 " color: #b00040;"
2182 " background: #e8e8d0;"
2183 " margin-top: 0em;"
2184 " margin-bottom: 0em;"
2185 "}\n"
2186 "li {"
2187 " margin-top: 0.25em;"
2188 " margin-right: 2em;"
2189 "}\n"
2190 ".hr {margin-top: 0.25em;"
2191 " border-color: black;"
2192 " border-bottom-style: solid;"
2193 "}\n"
2194 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2195 ".total {background: #20D0D0;color: #ffff80;}\n"
2196 ".frontend {background: #e8e8d0;}\n"
2197 ".socket {background: #d0d0d0;}\n"
2198 ".backend {background: #e8e8d0;}\n"
2199 ".active_down {background: #ff9090;}\n"
2200 ".active_going_up {background: #ffd020;}\n"
2201 ".active_going_down {background: #ffffa0;}\n"
2202 ".active_up {background: #c0ffc0;}\n"
2203 ".active_nolb {background: #20a0ff;}\n"
2204 ".active_draining {background: #20a0FF;}\n"
2205 ".active_no_check {background: #e0e0e0;}\n"
2206 ".backup_down {background: #ff9090;}\n"
2207 ".backup_going_up {background: #ff80ff;}\n"
2208 ".backup_going_down {background: #c060ff;}\n"
2209 ".backup_up {background: #b0d0ff;}\n"
2210 ".backup_nolb {background: #90b0e0;}\n"
2211 ".backup_draining {background: #cc9900;}\n"
2212 ".backup_no_check {background: #e0e0e0;}\n"
2213 ".maintain {background: #c07820;}\n"
2214 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2215 "\n"
2216 "a.px:link {color: #ffff40; text-decoration: none;}"
2217 "a.px:visited {color: #ffff40; text-decoration: none;}"
2218 "a.px:hover {color: #ffffff; text-decoration: none;}"
2219 "a.lfsb:link {color: #000000; text-decoration: none;}"
2220 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2221 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2222 "\n"
2223 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2224 "table.tbl td { text-align: right; border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray; white-space: nowrap;}\n"
2225 "table.tbl td.ac { text-align: center;}\n"
2226 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2227 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2228 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2229 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2230 "\n"
2231 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2232 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2233 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2234 "table.det { border-collapse: collapse; border-style: none; }\n"
2235 "table.det th { text-align: left; border-width: 0px; padding: 0px 1px 0px 0px; font-style:normal;font-size:11px;font-weight:bold;font-family: sans-serif;}\n"
2236 "table.det td { text-align: right; border-width: 0px; padding: 0px 0px 0px 4px; white-space: nowrap; font-style:normal;font-size:11px;font-weight:normal;}\n"
2237 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2238 "div.tips {\n"
2239 " display:block;\n"
2240 " visibility:hidden;\n"
2241 " z-index:2147483647;\n"
2242 " position:absolute;\n"
2243 " padding:2px 4px 3px;\n"
2244 " background:#f0f060; color:#000000;\n"
2245 " border:1px solid #7040c0;\n"
2246 " white-space:nowrap;\n"
2247 " font-style:normal;font-size:11px;font-weight:normal;\n"
2248 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2249 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2250 "}\n"
2251 "u:hover div.tips {visibility:visible;}\n"
2252 "-->\n"
2253 "</style></head>\n",
2254 (uri->flags & ST_SHNODE) ? " on " : "",
2255 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2256 );
2257}
2258
2259/* Dumps the HTML stats information block to the trash for and uses the state from
2260 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2261 * for clearing the trash if needed.
2262 */
2263static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2264{
2265 struct appctx *appctx = __objt_appctx(si->end);
2266 unsigned int up = (now.tv_sec - start_date.tv_sec);
2267 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2268
2269 /* WARNING! this has to fit the first packet too.
2270 * We are around 3.5 kB, add adding entries will
2271 * become tricky if we want to support 4kB buffers !
2272 */
2273 chunk_appendf(&trash,
2274 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2275 PRODUCT_NAME "%s</a></h1>\n"
2276 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2277 "<hr width=\"100%%\" class=\"hr\">\n"
2278 "<h3>&gt; General process information</h3>\n"
2279 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
2280 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
2281 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2282 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2283 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2284 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2285 "Running tasks: %d/%d; idle = %d %%<br>\n"
2286 "</td><td align=\"center\" nowrap>\n"
2287 "<table class=\"lgd\"><tr>\n"
2288 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2289 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2290 "</tr><tr>\n"
2291 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2292 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2293 "</tr><tr>\n"
2294 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2295 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2296 "</tr><tr>\n"
2297 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2298 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2299 "</tr><tr>\n"
2300 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2301 "</tr><tr>\n"
2302 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2303 "</tr></table>\n"
2304 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2305 "</td>"
2306 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2307 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2308 "",
2309 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
2310 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2311 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2312 (uri->flags & ST_SHDESC) ? ": " : "",
2313 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2314 pid, relative_pid, global.nbproc,
2315 up / 86400, (up % 86400) / 3600,
2316 (up % 3600) / 60, (up % 60),
2317 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2318 global.rlimit_memmax ? " MB" : "",
2319 global.rlimit_nofile,
2320 global.maxsock, global.maxconn, global.maxpipes,
2321 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002322 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002323 );
2324
2325 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2326 memcpy(scope_txt, bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2327 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2328
2329 chunk_appendf(&trash,
2330 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2331 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2332 STAT_SCOPE_TXT_MAXLEN);
2333
2334 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2335 scope_txt[0] = 0;
2336 if (appctx->ctx.stats.scope_len) {
2337 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2338 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2339 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2340 }
2341
2342 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2343 chunk_appendf(&trash,
2344 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2345 uri->uri_prefix,
2346 "",
2347 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2348 scope_txt);
2349 else
2350 chunk_appendf(&trash,
2351 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2352 uri->uri_prefix,
2353 ";up",
2354 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2355 scope_txt);
2356
2357 if (uri->refresh > 0) {
2358 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2359 chunk_appendf(&trash,
2360 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2361 uri->uri_prefix,
2362 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2363 "",
2364 scope_txt);
2365 else
2366 chunk_appendf(&trash,
2367 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2368 uri->uri_prefix,
2369 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2370 ";norefresh",
2371 scope_txt);
2372 }
2373
2374 chunk_appendf(&trash,
2375 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2376 uri->uri_prefix,
2377 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2378 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2379 scope_txt);
2380
2381 chunk_appendf(&trash,
2382 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2383 uri->uri_prefix,
2384 (uri->refresh > 0) ? ";norefresh" : "",
2385 scope_txt);
2386
2387 chunk_appendf(&trash,
2388 "</ul></td>"
2389 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2390 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2391 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2392 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2393 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2394 "</ul>"
2395 "</td>"
2396 "</tr></table>\n"
2397 ""
2398 );
2399
2400 if (appctx->ctx.stats.st_code) {
2401 switch (appctx->ctx.stats.st_code) {
2402 case STAT_STATUS_DONE:
2403 chunk_appendf(&trash,
2404 "<p><div class=active_up>"
2405 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2406 "Action processed successfully."
2407 "</div>\n", uri->uri_prefix,
2408 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2409 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2410 scope_txt);
2411 break;
2412 case STAT_STATUS_NONE:
2413 chunk_appendf(&trash,
2414 "<p><div class=active_going_down>"
2415 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2416 "Nothing has changed."
2417 "</div>\n", uri->uri_prefix,
2418 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2419 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2420 scope_txt);
2421 break;
2422 case STAT_STATUS_PART:
2423 chunk_appendf(&trash,
2424 "<p><div class=active_going_down>"
2425 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2426 "Action partially processed.<br>"
2427 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2428 "</div>\n", uri->uri_prefix,
2429 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2430 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2431 scope_txt);
2432 break;
2433 case STAT_STATUS_ERRP:
2434 chunk_appendf(&trash,
2435 "<p><div class=active_down>"
2436 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2437 "Action not processed because of invalid parameters."
2438 "<ul>"
2439 "<li>The action is maybe unknown.</li>"
2440 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2441 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2442 "</ul>"
2443 "</div>\n", uri->uri_prefix,
2444 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2445 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2446 scope_txt);
2447 break;
2448 case STAT_STATUS_EXCD:
2449 chunk_appendf(&trash,
2450 "<p><div class=active_down>"
2451 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2452 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2453 "You should retry with less servers at a time.</b>"
2454 "</div>\n", uri->uri_prefix,
2455 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2456 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2457 scope_txt);
2458 break;
2459 case STAT_STATUS_DENY:
2460 chunk_appendf(&trash,
2461 "<p><div class=active_down>"
2462 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2463 "<b>Action denied.</b>"
2464 "</div>\n", uri->uri_prefix,
2465 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2466 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2467 scope_txt);
2468 break;
2469 default:
2470 chunk_appendf(&trash,
2471 "<p><div class=active_no_check>"
2472 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2473 "Unexpected result."
2474 "</div>\n", uri->uri_prefix,
2475 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2476 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2477 scope_txt);
2478 }
2479 chunk_appendf(&trash, "<p>\n");
2480 }
2481}
2482
2483/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2484 * for clearing the trash if needed.
2485 */
2486static void stats_dump_html_end()
2487{
2488 chunk_appendf(&trash, "</body></html>\n");
2489}
2490
Simon Horman05ee2132017-01-04 09:37:25 +01002491/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2492 * for clearing it if needed.
2493 */
2494static void stats_dump_json_header()
2495{
2496 chunk_strcat(&trash, "[");
2497}
2498
2499
2500/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2501 * for clearing the trash if needed.
2502 */
2503static void stats_dump_json_end()
2504{
2505 chunk_strcat(&trash, "]");
2506}
2507
William Lallemand74c24fb2016-11-21 17:18:36 +01002508/* This function dumps statistics onto the stream interface's read buffer in
2509 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2510 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2511 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2512 * and the stream must be closed, or -1 in case of any error. This function is
2513 * used by both the CLI and the HTTP handlers.
2514 */
Willy Tarreau2b812e22016-11-22 16:18:05 +01002515static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002516{
2517 struct appctx *appctx = __objt_appctx(si->end);
2518 struct channel *rep = si_ic(si);
2519 struct proxy *px;
2520
2521 chunk_reset(&trash);
2522
2523 switch (appctx->st2) {
2524 case STAT_ST_INIT:
2525 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2526 /* fall through */
2527
2528 case STAT_ST_HEAD:
2529 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2530 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002531 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002532 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002533 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2534 stats_dump_csv_header();
2535
Willy Tarreau06d80a92017-10-19 14:32:15 +02002536 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002537 si_applet_cant_put(si);
2538 return 0;
2539 }
2540
2541 appctx->st2 = STAT_ST_INFO;
2542 /* fall through */
2543
2544 case STAT_ST_INFO:
2545 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2546 stats_dump_html_info(si, uri);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002547 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002548 si_applet_cant_put(si);
2549 return 0;
2550 }
2551 }
2552
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002553 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002554 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2555 appctx->st2 = STAT_ST_LIST;
2556 /* fall through */
2557
2558 case STAT_ST_LIST:
2559 /* dump proxies */
2560 while (appctx->ctx.stats.px) {
2561 if (buffer_almost_full(rep->buf)) {
2562 si_applet_cant_put(si);
2563 return 0;
2564 }
2565
2566 px = appctx->ctx.stats.px;
2567 /* skip the disabled proxies, global frontend and non-networked ones */
2568 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
2569 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
2570 return 0;
2571
2572 appctx->ctx.stats.px = px->next;
2573 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2574 }
2575 /* here, we just have reached the last proxy */
2576
2577 appctx->st2 = STAT_ST_END;
2578 /* fall through */
2579
2580 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002581 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2582 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2583 stats_dump_html_end();
2584 else
2585 stats_dump_json_end();
Willy Tarreau06d80a92017-10-19 14:32:15 +02002586 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002587 si_applet_cant_put(si);
2588 return 0;
2589 }
2590 }
2591
2592 appctx->st2 = STAT_ST_FIN;
2593 /* fall through */
2594
2595 case STAT_ST_FIN:
2596 return 1;
2597
2598 default:
2599 /* unknown state ! */
2600 appctx->st2 = STAT_ST_FIN;
2601 return -1;
2602 }
2603}
2604
2605/* We reached the stats page through a POST request. The appctx is
2606 * expected to have already been allocated by the caller.
2607 * Parse the posted data and enable/disable servers if necessary.
2608 * Returns 1 if request was parsed or zero if it needs more data.
2609 */
2610static int stats_process_http_post(struct stream_interface *si)
2611{
2612 struct stream *s = si_strm(si);
2613 struct appctx *appctx = objt_appctx(si->end);
2614
2615 struct proxy *px = NULL;
2616 struct server *sv = NULL;
2617
2618 char key[LINESIZE];
2619 int action = ST_ADM_ACTION_NONE;
2620 int reprocess = 0;
2621
2622 int total_servers = 0;
2623 int altered_servers = 0;
2624
2625 char *first_param, *cur_param, *next_param, *end_params;
2626 char *st_cur_param = NULL;
2627 char *st_next_param = NULL;
2628
2629 struct chunk *temp;
2630 int reql;
2631
2632 temp = get_trash_chunk();
2633 if (temp->size < s->txn->req.body_len) {
2634 /* too large request */
2635 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2636 goto out;
2637 }
2638
Willy Tarreau06d80a92017-10-19 14:32:15 +02002639 reql = co_getblk(si_oc(si), temp->str, s->txn->req.body_len, s->txn->req.eoh + 2);
William Lallemand74c24fb2016-11-21 17:18:36 +01002640 if (reql <= 0) {
2641 /* we need more data */
2642 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2643 return 0;
2644 }
2645
2646 first_param = temp->str;
2647 end_params = temp->str + reql;
2648 cur_param = next_param = end_params;
2649 *end_params = '\0';
2650
2651 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2652
2653 /*
2654 * Parse the parameters in reverse order to only store the last value.
2655 * From the html form, the backend and the action are at the end.
2656 */
2657 while (cur_param > first_param) {
2658 char *value;
2659 int poffset, plen;
2660
2661 cur_param--;
2662
2663 if ((*cur_param == '&') || (cur_param == first_param)) {
2664 reprocess_servers:
2665 /* Parse the key */
2666 poffset = (cur_param != first_param ? 1 : 0);
2667 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2668 if ((plen > 0) && (plen <= sizeof(key))) {
2669 strncpy(key, cur_param + poffset, plen);
2670 key[plen - 1] = '\0';
2671 } else {
2672 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2673 goto out;
2674 }
2675
2676 /* Parse the value */
2677 value = key;
2678 while (*value != '\0' && *value != '=') {
2679 value++;
2680 }
2681 if (*value == '=') {
2682 /* Ok, a value is found, we can mark the end of the key */
2683 *value++ = '\0';
2684 }
2685 if (url_decode(key) < 0 || url_decode(value) < 0)
2686 break;
2687
2688 /* Now we can check the key to see what to do */
2689 if (!px && (strcmp(key, "b") == 0)) {
2690 if ((px = proxy_be_by_name(value)) == NULL) {
2691 /* the backend name is unknown or ambiguous (duplicate names) */
2692 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2693 goto out;
2694 }
2695 }
2696 else if (!action && (strcmp(key, "action") == 0)) {
2697 if (strcmp(value, "ready") == 0) {
2698 action = ST_ADM_ACTION_READY;
2699 }
2700 else if (strcmp(value, "drain") == 0) {
2701 action = ST_ADM_ACTION_DRAIN;
2702 }
2703 else if (strcmp(value, "maint") == 0) {
2704 action = ST_ADM_ACTION_MAINT;
2705 }
2706 else if (strcmp(value, "shutdown") == 0) {
2707 action = ST_ADM_ACTION_SHUTDOWN;
2708 }
2709 else if (strcmp(value, "dhlth") == 0) {
2710 action = ST_ADM_ACTION_DHLTH;
2711 }
2712 else if (strcmp(value, "ehlth") == 0) {
2713 action = ST_ADM_ACTION_EHLTH;
2714 }
2715 else if (strcmp(value, "hrunn") == 0) {
2716 action = ST_ADM_ACTION_HRUNN;
2717 }
2718 else if (strcmp(value, "hnolb") == 0) {
2719 action = ST_ADM_ACTION_HNOLB;
2720 }
2721 else if (strcmp(value, "hdown") == 0) {
2722 action = ST_ADM_ACTION_HDOWN;
2723 }
2724 else if (strcmp(value, "dagent") == 0) {
2725 action = ST_ADM_ACTION_DAGENT;
2726 }
2727 else if (strcmp(value, "eagent") == 0) {
2728 action = ST_ADM_ACTION_EAGENT;
2729 }
2730 else if (strcmp(value, "arunn") == 0) {
2731 action = ST_ADM_ACTION_ARUNN;
2732 }
2733 else if (strcmp(value, "adown") == 0) {
2734 action = ST_ADM_ACTION_ADOWN;
2735 }
2736 /* else these are the old supported methods */
2737 else if (strcmp(value, "disable") == 0) {
2738 action = ST_ADM_ACTION_DISABLE;
2739 }
2740 else if (strcmp(value, "enable") == 0) {
2741 action = ST_ADM_ACTION_ENABLE;
2742 }
2743 else if (strcmp(value, "stop") == 0) {
2744 action = ST_ADM_ACTION_STOP;
2745 }
2746 else if (strcmp(value, "start") == 0) {
2747 action = ST_ADM_ACTION_START;
2748 }
2749 else {
2750 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2751 goto out;
2752 }
2753 }
2754 else if (strcmp(key, "s") == 0) {
2755 if (!(px && action)) {
2756 /*
2757 * Indicates that we'll need to reprocess the parameters
2758 * as soon as backend and action are known
2759 */
2760 if (!reprocess) {
2761 st_cur_param = cur_param;
2762 st_next_param = next_param;
2763 }
2764 reprocess = 1;
2765 }
2766 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002767 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002768 switch (action) {
2769 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002770 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002771 altered_servers++;
2772 total_servers++;
2773 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2774 }
2775 break;
2776 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002777 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002778 altered_servers++;
2779 total_servers++;
2780 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2781 }
2782 break;
2783 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002784 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002785 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2786 altered_servers++;
2787 total_servers++;
2788 }
2789 break;
2790 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002791 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002792 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2793 altered_servers++;
2794 total_servers++;
2795 }
2796 break;
2797 case ST_ADM_ACTION_DHLTH:
2798 if (sv->check.state & CHK_ST_CONFIGURED) {
2799 sv->check.state &= ~CHK_ST_ENABLED;
2800 altered_servers++;
2801 total_servers++;
2802 }
2803 break;
2804 case ST_ADM_ACTION_EHLTH:
2805 if (sv->check.state & CHK_ST_CONFIGURED) {
2806 sv->check.state |= CHK_ST_ENABLED;
2807 altered_servers++;
2808 total_servers++;
2809 }
2810 break;
2811 case ST_ADM_ACTION_HRUNN:
2812 if (!(sv->track)) {
2813 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002814 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002815 altered_servers++;
2816 total_servers++;
2817 }
2818 break;
2819 case ST_ADM_ACTION_HNOLB:
2820 if (!(sv->track)) {
2821 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002822 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002823 altered_servers++;
2824 total_servers++;
2825 }
2826 break;
2827 case ST_ADM_ACTION_HDOWN:
2828 if (!(sv->track)) {
2829 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002830 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002831 altered_servers++;
2832 total_servers++;
2833 }
2834 break;
2835 case ST_ADM_ACTION_DAGENT:
2836 if (sv->agent.state & CHK_ST_CONFIGURED) {
2837 sv->agent.state &= ~CHK_ST_ENABLED;
2838 altered_servers++;
2839 total_servers++;
2840 }
2841 break;
2842 case ST_ADM_ACTION_EAGENT:
2843 if (sv->agent.state & CHK_ST_CONFIGURED) {
2844 sv->agent.state |= CHK_ST_ENABLED;
2845 altered_servers++;
2846 total_servers++;
2847 }
2848 break;
2849 case ST_ADM_ACTION_ARUNN:
2850 if (sv->agent.state & CHK_ST_ENABLED) {
2851 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002852 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002853 altered_servers++;
2854 total_servers++;
2855 }
2856 break;
2857 case ST_ADM_ACTION_ADOWN:
2858 if (sv->agent.state & CHK_ST_ENABLED) {
2859 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002860 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002861 altered_servers++;
2862 total_servers++;
2863 }
2864 break;
2865 case ST_ADM_ACTION_READY:
2866 srv_adm_set_ready(sv);
2867 altered_servers++;
2868 total_servers++;
2869 break;
2870 case ST_ADM_ACTION_DRAIN:
2871 srv_adm_set_drain(sv);
2872 altered_servers++;
2873 total_servers++;
2874 break;
2875 case ST_ADM_ACTION_MAINT:
2876 srv_adm_set_maint(sv);
2877 altered_servers++;
2878 total_servers++;
2879 break;
2880 case ST_ADM_ACTION_SHUTDOWN:
2881 if (px->state != PR_STSTOPPED) {
2882 struct stream *sess, *sess_bck;
2883
2884 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2885 if (sess->srv_conn == sv)
2886 stream_shutdown(sess, SF_ERR_KILLED);
2887
2888 altered_servers++;
2889 total_servers++;
2890 }
2891 break;
2892 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002893 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002894 } else {
2895 /* the server name is unknown or ambiguous (duplicate names) */
2896 total_servers++;
2897 }
2898 }
2899 if (reprocess && px && action) {
2900 /* Now, we know the backend and the action chosen by the user.
2901 * We can safely restart from the first server parameter
2902 * to reprocess them
2903 */
2904 cur_param = st_cur_param;
2905 next_param = st_next_param;
2906 reprocess = 0;
2907 goto reprocess_servers;
2908 }
2909
2910 next_param = cur_param;
2911 }
2912 }
2913
2914 if (total_servers == 0) {
2915 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2916 }
2917 else if (altered_servers == 0) {
2918 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2919 }
2920 else if (altered_servers == total_servers) {
2921 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
2922 }
2923 else {
2924 appctx->ctx.stats.st_code = STAT_STATUS_PART;
2925 }
2926 out:
2927 return 1;
2928}
2929
2930
2931static int stats_send_http_headers(struct stream_interface *si)
2932{
2933 struct stream *s = si_strm(si);
2934 struct uri_auth *uri = s->be->uri_auth;
2935 struct appctx *appctx = objt_appctx(si->end);
2936
2937 chunk_printf(&trash,
2938 "HTTP/1.1 200 OK\r\n"
2939 "Cache-Control: no-cache\r\n"
2940 "Connection: close\r\n"
2941 "Content-Type: %s\r\n",
2942 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
2943
2944 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
2945 chunk_appendf(&trash, "Refresh: %d\r\n",
2946 uri->refresh);
2947
2948 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
2949
2950 if (appctx->ctx.stats.flags & STAT_CHUNKED)
2951 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
2952 else
2953 chunk_appendf(&trash, "\r\n");
2954
Willy Tarreau06d80a92017-10-19 14:32:15 +02002955 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002956 si_applet_cant_put(si);
2957 return 0;
2958 }
2959
2960 return 1;
2961}
2962
2963static int stats_send_http_redirect(struct stream_interface *si)
2964{
2965 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2966 struct stream *s = si_strm(si);
2967 struct uri_auth *uri = s->be->uri_auth;
2968 struct appctx *appctx = objt_appctx(si->end);
2969
2970 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2971 scope_txt[0] = 0;
2972 if (appctx->ctx.stats.scope_len) {
2973 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2974 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2975 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2976 }
2977
2978 /* We don't want to land on the posted stats page because a refresh will
2979 * repost the data. We don't want this to happen on accident so we redirect
2980 * the browse to the stats page with a GET.
2981 */
2982 chunk_printf(&trash,
2983 "HTTP/1.1 303 See Other\r\n"
2984 "Cache-Control: no-cache\r\n"
2985 "Content-Type: text/plain\r\n"
2986 "Connection: close\r\n"
2987 "Location: %s;st=%s%s%s%s\r\n"
2988 "Content-length: 0\r\n"
2989 "\r\n",
2990 uri->uri_prefix,
2991 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
2992 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
2993 stat_status_codes[appctx->ctx.stats.st_code]) ?
2994 stat_status_codes[appctx->ctx.stats.st_code] :
2995 stat_status_codes[STAT_STATUS_UNKN],
2996 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2997 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2998 scope_txt);
2999
Willy Tarreau06d80a92017-10-19 14:32:15 +02003000 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003001 si_applet_cant_put(si);
3002 return 0;
3003 }
3004
3005 return 1;
3006}
3007
Simon Horman05ee2132017-01-04 09:37:25 +01003008
William Lallemand74c24fb2016-11-21 17:18:36 +01003009/* This I/O handler runs as an applet embedded in a stream interface. It is
3010 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3011 * appctx->st0 contains the operation in progress (dump, done). The handler
3012 * automatically unregisters itself once transfer is complete.
3013 */
3014static void http_stats_io_handler(struct appctx *appctx)
3015{
3016 struct stream_interface *si = appctx->owner;
3017 struct stream *s = si_strm(si);
3018 struct channel *req = si_oc(si);
3019 struct channel *res = si_ic(si);
3020
3021 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3022 goto out;
3023
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003024 /* Check if the input buffer is avalaible. */
3025 if (res->buf->size == 0) {
3026 si_applet_cant_put(si);
3027 goto out;
3028 }
3029
William Lallemand74c24fb2016-11-21 17:18:36 +01003030 /* check that the output is not closed */
3031 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3032 appctx->st0 = STAT_HTTP_DONE;
3033
3034 /* all states are processed in sequence */
3035 if (appctx->st0 == STAT_HTTP_HEAD) {
3036 if (stats_send_http_headers(si)) {
3037 if (s->txn->meth == HTTP_METH_HEAD)
3038 appctx->st0 = STAT_HTTP_DONE;
3039 else
3040 appctx->st0 = STAT_HTTP_DUMP;
3041 }
3042 }
3043
3044 if (appctx->st0 == STAT_HTTP_DUMP) {
3045 unsigned int prev_len = si_ib(si)->i;
3046 unsigned int data_len;
3047 unsigned int last_len;
3048 unsigned int last_fwd = 0;
3049
3050 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3051 /* One difficulty we're facing is that we must prevent
3052 * the input data from being automatically forwarded to
3053 * the output area. For this, we temporarily disable
3054 * forwarding on the channel.
3055 */
3056 last_fwd = si_ic(si)->to_forward;
3057 si_ic(si)->to_forward = 0;
3058 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003059 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003060 si_applet_cant_put(si);
3061 si_ic(si)->to_forward = last_fwd;
3062 goto out;
3063 }
3064 }
3065
3066 data_len = si_ib(si)->i;
3067 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
3068 appctx->st0 = STAT_HTTP_DONE;
3069
3070 last_len = si_ib(si)->i;
3071
3072 /* Now we must either adjust or remove the chunk size. This is
3073 * not easy because the chunk size might wrap at the end of the
3074 * buffer, so we pretend we have nothing in the buffer, we write
3075 * the size, then restore the buffer's contents. Note that we can
3076 * only do that because no forwarding is scheduled on the stats
3077 * applet.
3078 */
3079 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3080 si_ic(si)->total -= (last_len - prev_len);
3081 si_ib(si)->i -= (last_len - prev_len);
3082
3083 if (last_len != data_len) {
3084 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003085 if (ci_putchk(si_ic(si), &trash) == -1)
William Lallemand74c24fb2016-11-21 17:18:36 +01003086 si_applet_cant_put(si);
3087
3088 si_ic(si)->total += (last_len - data_len);
3089 si_ib(si)->i += (last_len - data_len);
3090 }
3091 /* now re-enable forwarding */
3092 channel_forward(si_ic(si), last_fwd);
3093 }
3094 }
3095
3096 if (appctx->st0 == STAT_HTTP_POST) {
3097 if (stats_process_http_post(si))
3098 appctx->st0 = STAT_HTTP_LAST;
3099 else if (si_oc(si)->flags & CF_SHUTR)
3100 appctx->st0 = STAT_HTTP_DONE;
3101 }
3102
3103 if (appctx->st0 == STAT_HTTP_LAST) {
3104 if (stats_send_http_redirect(si))
3105 appctx->st0 = STAT_HTTP_DONE;
3106 }
3107
3108 if (appctx->st0 == STAT_HTTP_DONE) {
3109 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3110 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003111 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003112 si_applet_cant_put(si);
3113 goto out;
3114 }
3115 }
3116 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003117 co_skip(si_oc(si), si_ob(si)->o);
William Lallemand74c24fb2016-11-21 17:18:36 +01003118 res->flags |= CF_READ_NULL;
3119 si_shutr(si);
3120 }
3121
3122 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3123 si_shutw(si);
3124
3125 if (appctx->st0 == STAT_HTTP_DONE) {
3126 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3127 si_shutr(si);
3128 res->flags |= CF_READ_NULL;
3129 }
3130 }
3131 out:
3132 /* just to make gcc happy */ ;
3133}
3134
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003135/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
3136static int stats_dump_info_fields(struct chunk *out, const struct field *info)
3137{
3138 int field;
3139
3140 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3141 if (!field_format(info, field))
3142 continue;
3143
3144 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3145 return 0;
3146 if (!stats_emit_raw_data_field(out, &info[field]))
3147 return 0;
3148 if (!chunk_strcat(out, "\n"))
3149 return 0;
3150 }
3151 return 1;
3152}
3153
3154/* Dump all fields from <info> into <out> using the "show info typed" format */
3155static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
3156{
3157 int field;
3158
3159 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3160 if (!field_format(info, field))
3161 continue;
3162
3163 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3164 return 0;
3165 if (!stats_emit_field_tags(out, &info[field], ':'))
3166 return 0;
3167 if (!stats_emit_typed_data_field(out, &info[field]))
3168 return 0;
3169 if (!chunk_strcat(out, "\n"))
3170 return 0;
3171 }
3172 return 1;
3173}
3174
3175/* Fill <info> with HAProxy global info. <info> is preallocated
3176 * array of length <len>. The length of the aray must be
3177 * INF_TOTAL_FIELDS. If this length is less then this value, the
3178 * function returns 0, otherwise, it returns 1.
3179 */
3180int stats_fill_info(struct field *info, int len)
3181{
3182 unsigned int up = (now.tv_sec - start_date.tv_sec);
3183 struct chunk *out = get_trash_chunk();
3184
3185#ifdef USE_OPENSSL
3186 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3187 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3188 int ssl_reuse = 0;
3189
3190 if (ssl_key_rate < ssl_sess_rate) {
3191 /* count the ssl reuse ratio and avoid overflows in both directions */
3192 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3193 }
3194#endif
3195
3196 if (len < INF_TOTAL_FIELDS)
3197 return 0;
3198
3199 chunk_reset(out);
3200 memset(info, 0, sizeof(*info) * len);
3201
3202 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3203 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3204 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3205
3206 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3207 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3208 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3209
3210 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3211 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3212
3213 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3214 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3215 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3216 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3217 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3218 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3219 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3220 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3221 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3222 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3223 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3224 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3225#ifdef USE_OPENSSL
3226 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3227 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3228 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3229#endif
3230 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3231 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3232 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3233 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3234 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3235 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3236 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3237 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3238 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3239
3240#ifdef USE_OPENSSL
3241 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3242 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3243 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3244 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3245 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3246 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3247 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3248 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3249 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3250 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3251#endif
3252 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3253 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3254 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3255#ifdef USE_ZLIB
3256 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3257 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3258#endif
3259 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003260 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003261 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3262 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3263 if (global.desc)
3264 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3265
3266 return 1;
3267}
3268
3269/* This function dumps information onto the stream interface's read buffer.
3270 * It returns 0 as long as it does not complete, non-zero upon completion.
3271 * No state is used.
3272 */
3273static int stats_dump_info_to_buffer(struct stream_interface *si)
3274{
3275 struct appctx *appctx = __objt_appctx(si->end);
3276
3277 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3278 return 0;
3279
3280 chunk_reset(&trash);
3281
3282 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3283 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003284 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3285 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003286 else
3287 stats_dump_info_fields(&trash, info);
3288
Willy Tarreau06d80a92017-10-19 14:32:15 +02003289 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003290 si_applet_cant_put(si);
3291 return 0;
3292 }
3293
3294 return 1;
3295}
3296
Simon Horman6f6bb382017-01-04 09:37:26 +01003297/* This function dumps the schema onto the stream interface's read buffer.
3298 * It returns 0 as long as it does not complete, non-zero upon completion.
3299 * No state is used.
3300 *
3301 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3302 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3303 */
3304static void stats_dump_json_schema(struct chunk *out)
3305{
3306
3307 int old_len = out->len;
3308
3309 chunk_strcat(out,
3310 "{"
3311 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3312 "\"oneOf\":["
3313 "{"
3314 "\"title\":\"Info\","
3315 "\"type\":\"array\","
3316 "\"items\":{"
3317 "\"properties\":{"
3318 "\"title\":\"InfoItem\","
3319 "\"type\":\"object\","
3320 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3321 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3322 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3323 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3324 "},"
3325 "\"required\":[\"field\",\"processNum\",\"tags\","
3326 "\"value\"]"
3327 "}"
3328 "},"
3329 "{"
3330 "\"title\":\"Stat\","
3331 "\"type\":\"array\","
3332 "\"items\":{"
3333 "\"title\":\"InfoItem\","
3334 "\"type\":\"object\","
3335 "\"properties\":{"
3336 "\"objType\":{"
3337 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3338 "\"Server\",\"Unknown\"]"
3339 "},"
3340 "\"proxyId\":{"
3341 "\"type\":\"integer\","
3342 "\"minimum\":0"
3343 "},"
3344 "\"id\":{"
3345 "\"type\":\"integer\","
3346 "\"minimum\":0"
3347 "},"
3348 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3349 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3350 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3351 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3352 "},"
3353 "\"required\":[\"objType\",\"proxyId\",\"id\","
3354 "\"field\",\"processNum\",\"tags\","
3355 "\"value\"]"
3356 "}"
3357 "},"
3358 "{"
3359 "\"title\":\"Error\","
3360 "\"type\":\"object\","
3361 "\"properties\":{"
3362 "\"errorStr\":{"
3363 "\"type\":\"string\""
3364 "},"
3365 "\"required\":[\"errorStr\"]"
3366 "}"
3367 "}"
3368 "],"
3369 "\"definitions\":{"
3370 "\"field\":{"
3371 "\"type\":\"object\","
3372 "\"pos\":{"
3373 "\"type\":\"integer\","
3374 "\"minimum\":0"
3375 "},"
3376 "\"name\":{"
3377 "\"type\":\"string\""
3378 "},"
3379 "\"required\":[\"pos\",\"name\"]"
3380 "},"
3381 "\"processNum\":{"
3382 "\"type\":\"integer\","
3383 "\"minimum\":1"
3384 "},"
3385 "\"tags\":{"
3386 "\"type\":\"object\","
3387 "\"origin\":{"
3388 "\"type\":\"string\","
3389 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3390 "\"Config\",\"Product\",\"Unknown\"]"
3391 "},"
3392 "\"nature\":{"
3393 "\"type\":\"string\","
3394 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3395 "\"Rate\",\"Counter\",\"Duration\","
3396 "\"Age\",\"Time\",\"Name\",\"Output\","
3397 "\"Avg\", \"Unknown\"]"
3398 "},"
3399 "\"scope\":{"
3400 "\"type\":\"string\","
3401 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3402 "\"System\",\"Unknown\"]"
3403 "},"
3404 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3405 "},"
3406 "\"typedValue\":{"
3407 "\"type\":\"object\","
3408 "\"oneOf\":["
3409 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3410 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3411 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3412 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3413 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3414 "],"
3415 "\"definitions\":{"
3416 "\"s32Value\":{"
3417 "\"properties\":{"
3418 "\"type\":{"
3419 "\"type\":\"string\","
3420 "\"enum\":[\"s32\"]"
3421 "},"
3422 "\"value\":{"
3423 "\"type\":\"integer\","
3424 "\"minimum\":-2147483648,"
3425 "\"maximum\":2147483647"
3426 "}"
3427 "},"
3428 "\"required\":[\"type\",\"value\"]"
3429 "},"
3430 "\"s64Value\":{"
3431 "\"properties\":{"
3432 "\"type\":{"
3433 "\"type\":\"string\","
3434 "\"enum\":[\"s64\"]"
3435 "},"
3436 "\"value\":{"
3437 "\"type\":\"integer\","
3438 "\"minimum\":-9007199254740991,"
3439 "\"maximum\":9007199254740991"
3440 "}"
3441 "},"
3442 "\"required\":[\"type\",\"value\"]"
3443 "},"
3444 "\"u32Value\":{"
3445 "\"properties\":{"
3446 "\"type\":{"
3447 "\"type\":\"string\","
3448 "\"enum\":[\"u32\"]"
3449 "},"
3450 "\"value\":{"
3451 "\"type\":\"integer\","
3452 "\"minimum\":0,"
3453 "\"maximum\":4294967295"
3454 "}"
3455 "},"
3456 "\"required\":[\"type\",\"value\"]"
3457 "},"
3458 "\"u64Value\":{"
3459 "\"properties\":{"
3460 "\"type\":{"
3461 "\"type\":\"string\","
3462 "\"enum\":[\"u64\"]"
3463 "},"
3464 "\"value\":{"
3465 "\"type\":\"integer\","
3466 "\"minimum\":0,"
3467 "\"maximum\":9007199254740991"
3468 "}"
3469 "},"
3470 "\"required\":[\"type\",\"value\"]"
3471 "},"
3472 "\"strValue\":{"
3473 "\"properties\":{"
3474 "\"type\":{"
3475 "\"type\":\"string\","
3476 "\"enum\":[\"str\"]"
3477 "},"
3478 "\"value\":{\"type\":\"string\"}"
3479 "},"
3480 "\"required\":[\"type\",\"value\"]"
3481 "},"
3482 "\"unknownValue\":{"
3483 "\"properties\":{"
3484 "\"type\":{"
3485 "\"type\":\"integer\","
3486 "\"minimum\":0"
3487 "},"
3488 "\"value\":{"
3489 "\"type\":\"string\","
3490 "\"enum\":[\"unknown\"]"
3491 "}"
3492 "},"
3493 "\"required\":[\"type\",\"value\"]"
3494 "}"
3495 "}"
3496 "}"
3497 "}"
3498 "}");
3499
3500 if (old_len == out->len) {
3501 chunk_reset(out);
3502 chunk_appendf(out,
3503 "{\"errorStr\":\"output buffer too short\"}");
3504 }
3505}
3506
3507/* This function dumps the schema onto the stream interface's read buffer.
3508 * It returns 0 as long as it does not complete, non-zero upon completion.
3509 * No state is used.
3510 */
3511static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3512{
3513 chunk_reset(&trash);
3514
3515 stats_dump_json_schema(&trash);
3516
Willy Tarreau06d80a92017-10-19 14:32:15 +02003517 if (ci_putchk(si_ic(si), &trash) == -1) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003518 si_applet_cant_put(si);
3519 return 0;
3520 }
3521
3522 return 1;
3523}
3524
Willy Tarreau89d467c2016-11-23 11:02:40 +01003525static int cli_parse_clear_counters(char **args, struct appctx *appctx, void *private)
3526{
3527 struct proxy *px;
3528 struct server *sv;
3529 struct listener *li;
3530 int clrall = 0;
3531
3532 if (strcmp(args[2], "all") == 0)
3533 clrall = 1;
3534
3535 /* check permissions */
3536 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3537 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3538 return 1;
3539
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003540 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003541 if (clrall) {
3542 memset(&px->be_counters, 0, sizeof(px->be_counters));
3543 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3544 }
3545 else {
3546 px->be_counters.conn_max = 0;
3547 px->be_counters.p.http.rps_max = 0;
3548 px->be_counters.sps_max = 0;
3549 px->be_counters.cps_max = 0;
3550 px->be_counters.nbpend_max = 0;
3551
3552 px->fe_counters.conn_max = 0;
3553 px->fe_counters.p.http.rps_max = 0;
3554 px->fe_counters.sps_max = 0;
3555 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003556 }
3557
3558 for (sv = px->srv; sv; sv = sv->next)
3559 if (clrall)
3560 memset(&sv->counters, 0, sizeof(sv->counters));
3561 else {
3562 sv->counters.cur_sess_max = 0;
3563 sv->counters.nbpend_max = 0;
3564 sv->counters.sps_max = 0;
3565 }
3566
3567 list_for_each_entry(li, &px->conf.listeners, by_fe)
3568 if (li->counters) {
3569 if (clrall)
3570 memset(li->counters, 0, sizeof(*li->counters));
3571 else
3572 li->counters->conn_max = 0;
3573 }
3574 }
3575
3576 global.cps_max = 0;
3577 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003578 global.ssl_max = 0;
3579 global.ssl_fe_keys_max = 0;
3580 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003581
3582 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003583 return 1;
3584}
3585
3586
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003587static int cli_parse_show_info(char **args, struct appctx *appctx, void *private)
3588{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003589 appctx->ctx.stats.scope_str = 0;
3590 appctx->ctx.stats.scope_len = 0;
3591 appctx->ctx.stats.flags = 0;
3592
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003593 if (strcmp(args[2], "typed") == 0)
3594 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003595 else if (strcmp(args[2], "json") == 0)
3596 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003597 return 0;
3598}
3599
3600
Willy Tarreau2b812e22016-11-22 16:18:05 +01003601static int cli_parse_show_stat(char **args, struct appctx *appctx, void *private)
3602{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003603 appctx->ctx.stats.scope_str = 0;
3604 appctx->ctx.stats.scope_len = 0;
3605 appctx->ctx.stats.flags = 0;
3606
Willy Tarreau2b812e22016-11-22 16:18:05 +01003607 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003608 struct proxy *px;
3609
3610 px = proxy_find_by_name(args[2], 0, 0);
3611 if (px)
3612 appctx->ctx.stats.iid = px->uuid;
3613 else
3614 appctx->ctx.stats.iid = atoi(args[2]);
3615
3616 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003617 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003618 appctx->ctx.cli.msg = "No such proxy.\n";
3619 appctx->st0 = CLI_ST_PRINT;
3620 return 1;
3621 }
3622
Willy Tarreau2b812e22016-11-22 16:18:05 +01003623 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003624 appctx->ctx.stats.type = atoi(args[3]);
3625 appctx->ctx.stats.sid = atoi(args[4]);
3626 if (strcmp(args[5], "typed") == 0)
3627 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003628 else if (strcmp(args[5], "json") == 0)
3629 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003630 }
3631 else if (strcmp(args[2], "typed") == 0)
3632 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003633 else if (strcmp(args[2], "json") == 0)
3634 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003635
Willy Tarreau2b812e22016-11-22 16:18:05 +01003636 return 0;
3637}
3638
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003639static int cli_io_handler_dump_info(struct appctx *appctx)
3640{
3641 return stats_dump_info_to_buffer(appctx->owner);
3642}
3643
Willy Tarreau2b812e22016-11-22 16:18:05 +01003644/* This I/O handler runs as an applet embedded in a stream interface. It is
3645 * used to send raw stats over a socket.
3646 */
3647static int cli_io_handler_dump_stat(struct appctx *appctx)
3648{
3649 return stats_dump_stat_to_buffer(appctx->owner, NULL);
3650}
3651
Simon Horman6f6bb382017-01-04 09:37:26 +01003652static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3653{
3654 return stats_dump_json_schema_to_buffer(appctx->owner);
3655}
3656
Willy Tarreau2b812e22016-11-22 16:18:05 +01003657/* register cli keywords */
3658static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003659 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003660 { { "show", "info", NULL }, "show info : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreau2b812e22016-11-22 16:18:05 +01003661 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01003662 { { "show", "schema", "json", NULL }, "show schema json : report schema used for stats", NULL, cli_io_handler_dump_json_schema, NULL },
Willy Tarreau2b812e22016-11-22 16:18:05 +01003663 {{},}
3664}};
3665
William Lallemand74c24fb2016-11-21 17:18:36 +01003666struct applet http_stats_applet = {
3667 .obj_type = OBJ_TYPE_APPLET,
3668 .name = "<STATS>", /* used for logging */
3669 .fct = http_stats_io_handler,
3670 .release = NULL,
3671};
3672
Willy Tarreau2b812e22016-11-22 16:18:05 +01003673__attribute__((constructor))
3674static void __stat_init(void)
3675{
3676 cli_register_kw(&cli_kws);
3677}
3678
William Lallemand74c24fb2016-11-21 17:18:36 +01003679/*
3680 * Local variables:
3681 * c-indent-level: 8
3682 * c-basic-offset: 8
3683 * End:
3684 */