blob: 56266eba7964c649fdf26cbdd344edf3976bbb50 [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>
61#include <proto/proto_uxst.h>
62#include <proto/proxy.h>
63#include <proto/sample.h>
64#include <proto/session.h>
65#include <proto/stream.h>
66#include <proto/server.h>
67#include <proto/raw_sock.h>
68#include <proto/stream_interface.h>
69#include <proto/task.h>
70
71#ifdef USE_OPENSSL
72#include <proto/ssl_sock.h>
73#include <types/ssl_sock.h>
74#endif
75
76
Willy Tarreau0baac8c2016-11-22 16:36:53 +010077/* These are the field names for each INF_* field position. Please pay attention
78 * to always use the exact same name except that the strings for new names must
79 * be lower case or CamelCase while the enum entries must be upper case.
80 */
81const char *info_field_names[INF_TOTAL_FIELDS] = {
82 [INF_NAME] = "Name",
83 [INF_VERSION] = "Version",
84 [INF_RELEASE_DATE] = "Release_date",
85 [INF_NBPROC] = "Nbproc",
86 [INF_PROCESS_NUM] = "Process_num",
87 [INF_PID] = "Pid",
88 [INF_UPTIME] = "Uptime",
89 [INF_UPTIME_SEC] = "Uptime_sec",
90 [INF_MEMMAX_MB] = "Memmax_MB",
91 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
92 [INF_POOL_USED_MB] = "PoolUsed_MB",
93 [INF_POOL_FAILED] = "PoolFailed",
94 [INF_ULIMIT_N] = "Ulimit-n",
95 [INF_MAXSOCK] = "Maxsock",
96 [INF_MAXCONN] = "Maxconn",
97 [INF_HARD_MAXCONN] = "Hard_maxconn",
98 [INF_CURR_CONN] = "CurrConns",
99 [INF_CUM_CONN] = "CumConns",
100 [INF_CUM_REQ] = "CumReq",
101 [INF_MAX_SSL_CONNS] = "MaxSslConns",
102 [INF_CURR_SSL_CONNS] = "CurrSslConns",
103 [INF_CUM_SSL_CONNS] = "CumSslConns",
104 [INF_MAXPIPES] = "Maxpipes",
105 [INF_PIPES_USED] = "PipesUsed",
106 [INF_PIPES_FREE] = "PipesFree",
107 [INF_CONN_RATE] = "ConnRate",
108 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
109 [INF_MAX_CONN_RATE] = "MaxConnRate",
110 [INF_SESS_RATE] = "SessRate",
111 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
112 [INF_MAX_SESS_RATE] = "MaxSessRate",
113 [INF_SSL_RATE] = "SslRate",
114 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
115 [INF_MAX_SSL_RATE] = "MaxSslRate",
116 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
117 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
118 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
119 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
120 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
121 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
122 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
123 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
124 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
125 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
126 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
127 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
128 [INF_TASKS] = "Tasks",
129 [INF_RUN_QUEUE] = "Run_queue",
130 [INF_IDLE_PCT] = "Idle_pct",
131 [INF_NODE] = "node",
132 [INF_DESCRIPTION] = "description",
133};
134
William Lallemand74c24fb2016-11-21 17:18:36 +0100135const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
136 [ST_F_PXNAME] = "pxname",
137 [ST_F_SVNAME] = "svname",
138 [ST_F_QCUR] = "qcur",
139 [ST_F_QMAX] = "qmax",
140 [ST_F_SCUR] = "scur",
141 [ST_F_SMAX] = "smax",
142 [ST_F_SLIM] = "slim",
143 [ST_F_STOT] = "stot",
144 [ST_F_BIN] = "bin",
145 [ST_F_BOUT] = "bout",
146 [ST_F_DREQ] = "dreq",
147 [ST_F_DRESP] = "dresp",
148 [ST_F_EREQ] = "ereq",
149 [ST_F_ECON] = "econ",
150 [ST_F_ERESP] = "eresp",
151 [ST_F_WRETR] = "wretr",
152 [ST_F_WREDIS] = "wredis",
153 [ST_F_STATUS] = "status",
154 [ST_F_WEIGHT] = "weight",
155 [ST_F_ACT] = "act",
156 [ST_F_BCK] = "bck",
157 [ST_F_CHKFAIL] = "chkfail",
158 [ST_F_CHKDOWN] = "chkdown",
159 [ST_F_LASTCHG] = "lastchg",
160 [ST_F_DOWNTIME] = "downtime",
161 [ST_F_QLIMIT] = "qlimit",
162 [ST_F_PID] = "pid",
163 [ST_F_IID] = "iid",
164 [ST_F_SID] = "sid",
165 [ST_F_THROTTLE] = "throttle",
166 [ST_F_LBTOT] = "lbtot",
167 [ST_F_TRACKED] = "tracked",
168 [ST_F_TYPE] = "type",
169 [ST_F_RATE] = "rate",
170 [ST_F_RATE_LIM] = "rate_lim",
171 [ST_F_RATE_MAX] = "rate_max",
172 [ST_F_CHECK_STATUS] = "check_status",
173 [ST_F_CHECK_CODE] = "check_code",
174 [ST_F_CHECK_DURATION] = "check_duration",
175 [ST_F_HRSP_1XX] = "hrsp_1xx",
176 [ST_F_HRSP_2XX] = "hrsp_2xx",
177 [ST_F_HRSP_3XX] = "hrsp_3xx",
178 [ST_F_HRSP_4XX] = "hrsp_4xx",
179 [ST_F_HRSP_5XX] = "hrsp_5xx",
180 [ST_F_HRSP_OTHER] = "hrsp_other",
181 [ST_F_HANAFAIL] = "hanafail",
182 [ST_F_REQ_RATE] = "req_rate",
183 [ST_F_REQ_RATE_MAX] = "req_rate_max",
184 [ST_F_REQ_TOT] = "req_tot",
185 [ST_F_CLI_ABRT] = "cli_abrt",
186 [ST_F_SRV_ABRT] = "srv_abrt",
187 [ST_F_COMP_IN] = "comp_in",
188 [ST_F_COMP_OUT] = "comp_out",
189 [ST_F_COMP_BYP] = "comp_byp",
190 [ST_F_COMP_RSP] = "comp_rsp",
191 [ST_F_LASTSESS] = "lastsess",
192 [ST_F_LAST_CHK] = "last_chk",
193 [ST_F_LAST_AGT] = "last_agt",
194 [ST_F_QTIME] = "qtime",
195 [ST_F_CTIME] = "ctime",
196 [ST_F_RTIME] = "rtime",
197 [ST_F_TTIME] = "ttime",
198 [ST_F_AGENT_STATUS] = "agent_status",
199 [ST_F_AGENT_CODE] = "agent_code",
200 [ST_F_AGENT_DURATION] = "agent_duration",
201 [ST_F_CHECK_DESC] = "check_desc",
202 [ST_F_AGENT_DESC] = "agent_desc",
203 [ST_F_CHECK_RISE] = "check_rise",
204 [ST_F_CHECK_FALL] = "check_fall",
205 [ST_F_CHECK_HEALTH] = "check_health",
206 [ST_F_AGENT_RISE] = "agent_rise",
207 [ST_F_AGENT_FALL] = "agent_fall",
208 [ST_F_AGENT_HEALTH] = "agent_health",
209 [ST_F_ADDR] = "addr",
210 [ST_F_COOKIE] = "cookie",
211 [ST_F_MODE] = "mode",
212 [ST_F_ALGO] = "algo",
213 [ST_F_CONN_RATE] = "conn_rate",
214 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
215 [ST_F_CONN_TOT] = "conn_tot",
216 [ST_F_INTERCEPTED] = "intercepted",
217 [ST_F_DCON] = "dcon",
218 [ST_F_DSES] = "dses",
219};
220
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100221/* one line of info */
222static struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100223/* one line of stats */
224static struct field stats[ST_F_TOTAL_FIELDS];
225
226
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100227
William Lallemand74c24fb2016-11-21 17:18:36 +0100228/*
229 * http_stats_io_handler()
230 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
231 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
232 * -> 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
242 */
243
244
245extern const char *stat_status_codes[];
246
247/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
248 * for clearing it if needed.
249 * NOTE: Some tools happen to rely on the field position instead of its name,
250 * so please only append new fields at the end, never in the middle.
251 */
252static void stats_dump_csv_header()
253{
254 int field;
255
256 chunk_appendf(&trash, "# ");
257 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
258 chunk_appendf(&trash, "%s,", stat_field_names[field]);
259
260 chunk_appendf(&trash, "\n");
261}
262
263
264/* Emits a stats field without any surrounding element and properly encoded to
265 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
266 */
267int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
268{
269 switch (field_format(f, 0)) {
270 case FF_EMPTY: return 1;
271 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
272 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
273 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
274 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
275 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
276 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
277 }
278}
279
280/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
281 * output is supposed to be used on its own line. Returns non-zero on success, 0
282 * if the buffer is full.
283 */
284int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
285{
286 switch (field_format(f, 0)) {
287 case FF_EMPTY: return 1;
288 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
289 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
290 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
291 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
292 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
293 default: return chunk_appendf(out, "%08x:?", f->type);
294 }
295}
296
297/* Emits an encoding of the field type on 3 characters followed by a delimiter.
298 * Returns non-zero on success, 0 if the buffer is full.
299 */
300int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
301{
302 char origin, nature, scope;
303
304 switch (field_origin(f, 0)) {
305 case FO_METRIC: origin = 'M'; break;
306 case FO_STATUS: origin = 'S'; break;
307 case FO_KEY: origin = 'K'; break;
308 case FO_CONFIG: origin = 'C'; break;
309 case FO_PRODUCT: origin = 'P'; break;
310 default: origin = '?'; break;
311 }
312
313 switch (field_nature(f, 0)) {
314 case FN_GAUGE: nature = 'G'; break;
315 case FN_LIMIT: nature = 'L'; break;
316 case FN_MIN: nature = 'm'; break;
317 case FN_MAX: nature = 'M'; break;
318 case FN_RATE: nature = 'R'; break;
319 case FN_COUNTER: nature = 'C'; break;
320 case FN_DURATION: nature = 'D'; break;
321 case FN_AGE: nature = 'A'; break;
322 case FN_TIME: nature = 'T'; break;
323 case FN_NAME: nature = 'N'; break;
324 case FN_OUTPUT: nature = 'O'; break;
325 case FN_AVG: nature = 'a'; break;
326 default: nature = '?'; break;
327 }
328
329 switch (field_scope(f, 0)) {
330 case FS_PROCESS: scope = 'P'; break;
331 case FS_SERVICE: scope = 'S'; break;
332 case FS_SYSTEM: scope = 's'; break;
333 case FS_CLUSTER: scope = 'C'; break;
334 default: scope = '?'; break;
335 }
336
337 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
338}
339
340
341/* Dump all fields from <stats> into <out> using CSV format */
342static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
343{
344 int field;
345
346 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
347 if (!stats_emit_raw_data_field(out, &stats[field]))
348 return 0;
349 if (!chunk_strcat(out, ","))
350 return 0;
351 }
352 chunk_strcat(out, "\n");
353 return 1;
354}
355
356/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
357static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
358{
359 int field;
360
361 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
362 if (!stats[field].type)
363 continue;
364
365 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
366 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
367 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
368 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
369 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
370 '?',
371 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
372 field, stat_field_names[field], stats[ST_F_PID].u.u32);
373
374 if (!stats_emit_field_tags(out, &stats[field], ':'))
375 return 0;
376 if (!stats_emit_typed_data_field(out, &stats[field]))
377 return 0;
378 if (!chunk_strcat(out, "\n"))
379 return 0;
380 }
381 return 1;
382}
383
384/* Dump all fields from <stats> into <out> using the HTML format. A column is
385 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
386 * are provided if ST_SHLGNDS is present in <flags>.
387 */
388static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
389{
390 struct chunk src;
391
392 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
393 chunk_appendf(out,
394 /* name, queue */
395 "<tr class=\"frontend\">");
396
397 if (flags & ST_SHOWADMIN) {
398 /* Column sub-heading for Enable or Disable server */
399 chunk_appendf(out, "<td></td>");
400 }
401
402 chunk_appendf(out,
403 "<td class=ac>"
404 "<a name=\"%s/Frontend\"></a>"
405 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
406 "<td colspan=3></td>"
407 "",
408 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
409
410 chunk_appendf(out,
411 /* sessions rate : current */
412 "<td><u>%s<div class=tips><table class=det>"
413 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
414 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
415 "",
416 U2H(stats[ST_F_RATE].u.u32),
417 U2H(stats[ST_F_CONN_RATE].u.u32),
418 U2H(stats[ST_F_RATE].u.u32));
419
420 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
421 chunk_appendf(out,
422 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
423 U2H(stats[ST_F_REQ_RATE].u.u32));
424
425 chunk_appendf(out,
426 "</table></div></u></td>"
427 /* sessions rate : max */
428 "<td><u>%s<div class=tips><table class=det>"
429 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
430 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
431 "",
432 U2H(stats[ST_F_RATE_MAX].u.u32),
433 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
434 U2H(stats[ST_F_RATE_MAX].u.u32));
435
436 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
437 chunk_appendf(out,
438 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
439 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
440
441 chunk_appendf(out,
442 "</table></div></u></td>"
443 /* sessions rate : limit */
444 "<td>%s</td>",
445 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
446
447 chunk_appendf(out,
448 /* sessions: current, max, limit, total */
449 "<td>%s</td><td>%s</td><td>%s</td>"
450 "<td><u>%s<div class=tips><table class=det>"
451 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
452 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
453 "",
454 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
455 U2H(stats[ST_F_STOT].u.u64),
456 U2H(stats[ST_F_CONN_TOT].u.u64),
457 U2H(stats[ST_F_STOT].u.u64));
458
459 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
460 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
461 chunk_appendf(out,
462 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
463 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
464 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
465 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
466 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
467 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
468 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
469 "<tr><th>- other responses:</th><td>%s</td></tr>"
470 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
471 "",
472 U2H(stats[ST_F_REQ_TOT].u.u64),
473 U2H(stats[ST_F_HRSP_1XX].u.u64),
474 U2H(stats[ST_F_HRSP_2XX].u.u64),
475 U2H(stats[ST_F_COMP_RSP].u.u64),
476 stats[ST_F_HRSP_2XX].u.u64 ?
477 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
478 U2H(stats[ST_F_HRSP_3XX].u.u64),
479 U2H(stats[ST_F_HRSP_4XX].u.u64),
480 U2H(stats[ST_F_HRSP_5XX].u.u64),
481 U2H(stats[ST_F_HRSP_OTHER].u.u64),
482 U2H(stats[ST_F_INTERCEPTED].u.u64));
483 }
484
485 chunk_appendf(out,
486 "</table></div></u></td>"
487 /* sessions: lbtot, lastsess */
488 "<td></td><td></td>"
489 /* bytes : in */
490 "<td>%s</td>"
491 "",
492 U2H(stats[ST_F_BIN].u.u64));
493
494 chunk_appendf(out,
495 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
496 "<td>%s%s<div class=tips><table class=det>"
497 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
498 "<tr><th>Compression in:</th><td>%s</td></tr>"
499 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
500 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
501 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
502 "</table></div>%s</td>",
503 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
504 U2H(stats[ST_F_BOUT].u.u64),
505 U2H(stats[ST_F_BOUT].u.u64),
506 U2H(stats[ST_F_COMP_IN].u.u64),
507 U2H(stats[ST_F_COMP_OUT].u.u64),
508 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
509 U2H(stats[ST_F_COMP_BYP].u.u64),
510 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
511 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,
512 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
513
514 chunk_appendf(out,
515 /* denied: req, resp */
516 "<td>%s</td><td>%s</td>"
517 /* errors : request, connect, response */
518 "<td>%s</td><td></td><td></td>"
519 /* warnings: retries, redispatches */
520 "<td></td><td></td>"
521 /* server status : reflect frontend status */
522 "<td class=ac>%s</td>"
523 /* rest of server: nothing */
524 "<td class=ac colspan=8></td></tr>"
525 "",
526 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
527 U2H(stats[ST_F_EREQ].u.u64),
528 field_str(stats, ST_F_STATUS));
529 }
530 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
531 chunk_appendf(out, "<tr class=socket>");
532 if (flags & ST_SHOWADMIN) {
533 /* Column sub-heading for Enable or Disable server */
534 chunk_appendf(out, "<td></td>");
535 }
536
537 chunk_appendf(out,
538 /* frontend name, listener name */
539 "<td class=ac><a name=\"%s/+%s\"></a>%s"
540 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
541 "",
542 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
543 (flags & ST_SHLGNDS)?"<u>":"",
544 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
545
546 if (flags & ST_SHLGNDS) {
547 chunk_appendf(out, "<div class=tips>");
548
549 if (isdigit(*field_str(stats, ST_F_ADDR)))
550 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
551 else if (*field_str(stats, ST_F_ADDR) == '[')
552 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
553 else if (*field_str(stats, ST_F_ADDR))
554 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
555
556 /* id */
557 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
558 }
559
560 chunk_appendf(out,
561 /* queue */
562 "%s</td><td colspan=3></td>"
563 /* sessions rate: current, max, limit */
564 "<td colspan=3>&nbsp;</td>"
565 /* sessions: current, max, limit, total, lbtot, lastsess */
566 "<td>%s</td><td>%s</td><td>%s</td>"
567 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
568 /* bytes: in, out */
569 "<td>%s</td><td>%s</td>"
570 "",
571 (flags & ST_SHLGNDS)?"</u>":"",
572 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
573 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
574
575 chunk_appendf(out,
576 /* denied: req, resp */
577 "<td>%s</td><td>%s</td>"
578 /* errors: request, connect, response */
579 "<td>%s</td><td></td><td></td>"
580 /* warnings: retries, redispatches */
581 "<td></td><td></td>"
582 /* server status: reflect listener status */
583 "<td class=ac>%s</td>"
584 /* rest of server: nothing */
585 "<td class=ac colspan=8></td></tr>"
586 "",
587 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
588 U2H(stats[ST_F_EREQ].u.u64),
589 field_str(stats, ST_F_STATUS));
590 }
591 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
592 const char *style;
593
594 /* determine the style to use depending on the server's state,
595 * its health and weight. There isn't a 1-to-1 mapping between
596 * state and styles for the cases where the server is (still)
597 * up. The reason is that we don't want to report nolb and
598 * drain with the same color.
599 */
600
601 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
602 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
603 style = "down";
604 }
605 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
606 style = "going_up";
607 }
608 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
609 style = "going_down";
610 }
611 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
612 style = "nolb";
613 }
614 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
615 style = "no_check";
616 }
617 else if (!stats[ST_F_CHKFAIL].type ||
618 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
619 /* no check or max health = UP */
620 if (stats[ST_F_WEIGHT].u.u32)
621 style = "up";
622 else
623 style = "draining";
624 }
625 else {
626 style = "going_down";
627 }
628
629 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
630 chunk_appendf(out, "<tr class=\"maintain\">");
631 else
632 chunk_appendf(out,
633 "<tr class=\"%s_%s\">",
634 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
635
636
637 if (flags & ST_SHOWADMIN)
638 chunk_appendf(out,
639 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
640 field_str(stats, ST_F_SVNAME));
641
642 chunk_appendf(out,
643 "<td class=ac><a name=\"%s/%s\"></a>%s"
644 "<a class=lfsb href=\"#%s/%s\">%s</a>"
645 "",
646 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
647 (flags & ST_SHLGNDS) ? "<u>" : "",
648 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
649
650 if (flags & ST_SHLGNDS) {
651 chunk_appendf(out, "<div class=tips>");
652
653 if (isdigit(*field_str(stats, ST_F_ADDR)))
654 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
655 else if (*field_str(stats, ST_F_ADDR) == '[')
656 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
657 else if (*field_str(stats, ST_F_ADDR))
658 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
659
660 /* id */
661 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
662
663 /* cookie */
664 if (stats[ST_F_COOKIE].type) {
665 chunk_appendf(out, ", cookie: '");
666 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
667 chunk_htmlencode(out, &src);
668 chunk_appendf(out, "'");
669 }
670
671 chunk_appendf(out, "</div>");
672 }
673
674 chunk_appendf(out,
675 /* queue : current, max, limit */
676 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
677 /* sessions rate : current, max, limit */
678 "<td>%s</td><td>%s</td><td></td>"
679 "",
680 (flags & ST_SHLGNDS) ? "</u>" : "",
681 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
682 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
683
684 chunk_appendf(out,
685 /* sessions: current, max, limit, total */
686 "<td>%s</td><td>%s</td><td>%s</td>"
687 "<td><u>%s<div class=tips><table class=det>"
688 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
689 "",
690 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
691 U2H(stats[ST_F_STOT].u.u64),
692 U2H(stats[ST_F_STOT].u.u64));
693
694 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
695 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
696 unsigned long long tot;
697
698 tot = stats[ST_F_HRSP_OTHER].u.u64;
699 tot += stats[ST_F_HRSP_1XX].u.u64;
700 tot += stats[ST_F_HRSP_2XX].u.u64;
701 tot += stats[ST_F_HRSP_3XX].u.u64;
702 tot += stats[ST_F_HRSP_4XX].u.u64;
703 tot += stats[ST_F_HRSP_5XX].u.u64;
704
705 chunk_appendf(out,
706 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
707 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
708 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
709 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
710 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
711 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
712 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
713 "",
714 U2H(tot),
715 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
716 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
717 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
718 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
719 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
720 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0);
721 }
722
723 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
724 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
725 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
726 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
727 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
728 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
729
730 chunk_appendf(out,
731 "</table></div></u></td>"
732 /* sessions: lbtot, last */
733 "<td>%s</td><td>%s</td>",
734 U2H(stats[ST_F_LBTOT].u.u64),
735 human_time(stats[ST_F_LASTSESS].u.s32, 1));
736
737 chunk_appendf(out,
738 /* bytes : in, out */
739 "<td>%s</td><td>%s</td>"
740 /* denied: req, resp */
741 "<td></td><td>%s</td>"
742 /* errors : request, connect */
743 "<td></td><td>%s</td>"
744 /* errors : response */
745 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
746 /* warnings: retries, redispatches */
747 "<td>%lld</td><td>%lld</td>"
748 "",
749 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
750 U2H(stats[ST_F_DRESP].u.u64),
751 U2H(stats[ST_F_ECON].u.u64),
752 U2H(stats[ST_F_ERESP].u.u64),
753 (long long)stats[ST_F_CLI_ABRT].u.u64,
754 (long long)stats[ST_F_SRV_ABRT].u.u64,
755 (long long)stats[ST_F_WRETR].u.u64,
756 (long long)stats[ST_F_WREDIS].u.u64);
757
758 /* status, last change */
759 chunk_appendf(out, "<td class=ac>");
760
761 /* FIXME!!!!
762 * LASTCHG should contain the last change for *this* server and must be computed
763 * properly above, as was done below, ie: this server if maint, otherwise ref server
764 * if tracking. Note that ref is either local or remote depending on tracking.
765 */
766
767
768 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
769 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
770 }
771 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
772 chunk_strcat(out, "<i>no check</i>");
773 }
774 else {
775 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
776 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
777 if (stats[ST_F_CHECK_HEALTH].u.u32)
778 chunk_strcat(out, " &uarr;");
779 }
780 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
781 chunk_strcat(out, " &darr;");
782 }
783
784 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
785 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
786 chunk_appendf(out,
787 "</td><td class=ac><u> %s",
788 field_str(stats, ST_F_AGENT_STATUS));
789
790 if (stats[ST_F_AGENT_CODE].type)
791 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
792
793 if (stats[ST_F_AGENT_DURATION].type)
794 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
795
796 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
797
798 if (*field_str(stats, ST_F_LAST_AGT)) {
799 chunk_appendf(out, ": ");
800 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
801 chunk_htmlencode(out, &src);
802 }
803 chunk_appendf(out, "</div></u>");
804 }
805 else if (stats[ST_F_CHECK_STATUS].type) {
806 chunk_appendf(out,
807 "</td><td class=ac><u> %s",
808 field_str(stats, ST_F_CHECK_STATUS));
809
810 if (stats[ST_F_CHECK_CODE].type)
811 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
812
813 if (stats[ST_F_CHECK_DURATION].type)
814 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
815
816 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
817
818 if (*field_str(stats, ST_F_LAST_CHK)) {
819 chunk_appendf(out, ": ");
820 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
821 chunk_htmlencode(out, &src);
822 }
823 chunk_appendf(out, "</div></u>");
824 }
825 else
826 chunk_appendf(out, "</td><td>");
827
828 chunk_appendf(out,
829 /* weight */
830 "</td><td class=ac>%d</td>"
831 /* act, bck */
832 "<td class=ac>%s</td><td class=ac>%s</td>"
833 "",
834 stats[ST_F_WEIGHT].u.u32,
835 stats[ST_F_BCK].u.u32 ? "-" : "Y",
836 stats[ST_F_BCK].u.u32 ? "Y" : "-");
837
838 /* check failures: unique, fatal, down time */
839 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
840 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
841 }
842 else if (stats[ST_F_CHKFAIL].type) {
843 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
844
845 if (stats[ST_F_HANAFAIL].type)
846 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
847
848 chunk_appendf(out,
849 "<div class=tips>Failed Health Checks%s</div></u></td>"
850 "<td>%lld</td><td>%s</td>"
851 "",
852 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
853 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
854 }
855 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
856 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
857 chunk_appendf(out,
858 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
859 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
860 }
861 else
862 chunk_appendf(out, "<td colspan=3></td>");
863
864 /* throttle */
865 if (stats[ST_F_THROTTLE].type)
866 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
867 else
868 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
869 }
870 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
871 chunk_appendf(out, "<tr class=\"backend\">");
872 if (flags & ST_SHOWADMIN) {
873 /* Column sub-heading for Enable or Disable server */
874 chunk_appendf(out, "<td></td>");
875 }
876 chunk_appendf(out,
877 "<td class=ac>"
878 /* name */
879 "%s<a name=\"%s/Backend\"></a>"
880 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
881 "",
882 (flags & ST_SHLGNDS)?"<u>":"",
883 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
884
885 if (flags & ST_SHLGNDS) {
886 /* balancing */
887 chunk_appendf(out, "<div class=tips>balancing: %s",
888 field_str(stats, ST_F_ALGO));
889
890 /* cookie */
891 if (stats[ST_F_COOKIE].type) {
892 chunk_appendf(out, ", cookie: '");
893 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
894 chunk_htmlencode(out, &src);
895 chunk_appendf(out, "'");
896 }
897 chunk_appendf(out, "</div>");
898 }
899
900 chunk_appendf(out,
901 "%s</td>"
902 /* queue : current, max */
903 "<td>%s</td><td>%s</td><td></td>"
904 /* sessions rate : current, max, limit */
905 "<td>%s</td><td>%s</td><td></td>"
906 "",
907 (flags & ST_SHLGNDS)?"</u>":"",
908 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
909 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
910
911 chunk_appendf(out,
912 /* sessions: current, max, limit, total */
913 "<td>%s</td><td>%s</td><td>%s</td>"
914 "<td><u>%s<div class=tips><table class=det>"
915 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
916 "",
917 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
918 U2H(stats[ST_F_STOT].u.u64),
919 U2H(stats[ST_F_STOT].u.u64));
920
921 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
922 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
923 chunk_appendf(out,
924 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
925 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
926 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
927 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
928 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
929 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
930 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
931 "<tr><th>- other responses:</th><td>%s</td></tr>"
932 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
933 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
934 "",
935 U2H(stats[ST_F_REQ_TOT].u.u64),
936 U2H(stats[ST_F_HRSP_1XX].u.u64),
937 U2H(stats[ST_F_HRSP_2XX].u.u64),
938 U2H(stats[ST_F_COMP_RSP].u.u64),
939 stats[ST_F_HRSP_2XX].u.u64 ?
940 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
941 U2H(stats[ST_F_HRSP_3XX].u.u64),
942 U2H(stats[ST_F_HRSP_4XX].u.u64),
943 U2H(stats[ST_F_HRSP_5XX].u.u64),
944 U2H(stats[ST_F_HRSP_OTHER].u.u64),
945 U2H(stats[ST_F_INTERCEPTED].u.u64));
946 }
947
948 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
949 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
950 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
951 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
952 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
953
954 chunk_appendf(out,
955 "</table></div></u></td>"
956 /* sessions: lbtot, last */
957 "<td>%s</td><td>%s</td>"
958 /* bytes: in */
959 "<td>%s</td>"
960 "",
961 U2H(stats[ST_F_LBTOT].u.u64),
962 human_time(stats[ST_F_LASTSESS].u.s32, 1),
963 U2H(stats[ST_F_BIN].u.u64));
964
965 chunk_appendf(out,
966 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
967 "<td>%s%s<div class=tips><table class=det>"
968 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
969 "<tr><th>Compression in:</th><td>%s</td></tr>"
970 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
971 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
972 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
973 "</table></div>%s</td>",
974 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
975 U2H(stats[ST_F_BOUT].u.u64),
976 U2H(stats[ST_F_BOUT].u.u64),
977 U2H(stats[ST_F_COMP_IN].u.u64),
978 U2H(stats[ST_F_COMP_OUT].u.u64),
979 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
980 U2H(stats[ST_F_COMP_BYP].u.u64),
981 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
982 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,
983 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
984
985 chunk_appendf(out,
986 /* denied: req, resp */
987 "<td>%s</td><td>%s</td>"
988 /* errors : request, connect */
989 "<td></td><td>%s</td>"
990 /* errors : response */
991 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
992 /* warnings: retries, redispatches */
993 "<td>%lld</td><td>%lld</td>"
994 /* backend status: reflect backend status (up/down): we display UP
995 * if the backend has known working servers or if it has no server at
996 * all (eg: for stats). Then we display the total weight, number of
997 * active and backups. */
998 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
999 "<td class=ac>%d</td><td class=ac>%d</td>"
1000 "",
1001 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1002 U2H(stats[ST_F_ECON].u.u64),
1003 U2H(stats[ST_F_ERESP].u.u64),
1004 (long long)stats[ST_F_CLI_ABRT].u.u64,
1005 (long long)stats[ST_F_SRV_ABRT].u.u64,
1006 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1007 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1008 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1009 stats[ST_F_WEIGHT].u.u32,
1010 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1011
1012 chunk_appendf(out,
1013 /* rest of backend: nothing, down transitions, total downtime, throttle */
1014 "<td class=ac>&nbsp;</td><td>%d</td>"
1015 "<td>%s</td>"
1016 "<td></td>"
1017 "</tr>",
1018 stats[ST_F_CHKDOWN].u.u32,
1019 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1020 }
1021 return 1;
1022}
1023
1024int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1025{
1026 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1027 flags |= ST_SHOWADMIN;
1028
1029 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
1030 return stats_dump_fields_html(&trash, stats, flags);
1031 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
1032 return stats_dump_fields_typed(&trash, stats);
1033 else
1034 return stats_dump_fields_csv(&trash, stats);
1035}
1036
1037/* Fill <stats> with the frontend statistics. <stats> is
1038 * preallocated array of length <len>. The length of the array
1039 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1040 * this value, the function returns 0, otherwise, it returns 1.
1041 */
1042int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1043{
1044 if (len < ST_F_TOTAL_FIELDS)
1045 return 0;
1046
1047 memset(stats, 0, sizeof(*stats) * len);
1048
1049 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1050 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1051 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1052 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1053 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1054 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1055 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1056 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1057 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1058 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1059 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1060 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1061 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1062 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1063 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1064 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1065 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1066 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1067 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1068 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1069 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1070 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1071
1072 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1073 if (px->mode == PR_MODE_HTTP) {
1074 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1075 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1076 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1077 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1078 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1079 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1080 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
1081 }
1082
1083 /* requests : req_rate, req_rate_max, req_tot, */
1084 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1085 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1086 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1087
1088 /* compression: in, out, bypassed, responses */
1089 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1090 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1091 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1092 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1093
1094 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1095 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1096 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1097 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1098
1099 return 1;
1100}
1101
1102/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1103 * the state from stream interface <si>. The caller is responsible for clearing
1104 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1105 */
1106static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1107{
1108 struct appctx *appctx = __objt_appctx(si->end);
1109
1110 if (!(px->cap & PR_CAP_FE))
1111 return 0;
1112
1113 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1114 return 0;
1115
1116 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1117 return 0;
1118
1119 return stats_dump_one_line(stats, 0, px, appctx);
1120}
1121
1122/* Fill <stats> with the listener statistics. <stats> is
1123 * preallocated array of length <len>. The length of the array
1124 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1125 * then this value, the function returns 0, otherwise, it
1126 * returns 1. <flags> can take the value ST_SHLGNDS.
1127 */
1128int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1129 struct field *stats, int len)
1130{
1131 struct chunk *out = get_trash_chunk();
1132
1133 if (len < ST_F_TOTAL_FIELDS)
1134 return 0;
1135
1136 if (!l->counters)
1137 return 0;
1138
1139 chunk_reset(out);
1140 memset(stats, 0, sizeof(*stats) * len);
1141
1142 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1143 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1144 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1145 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1146 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1147 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1148 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1149 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1150 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1151 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1152 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1153 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1154 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1155 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1156 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
1157 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1158 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1159 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1160 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1161
1162 if (flags & ST_SHLGNDS) {
1163 char str[INET6_ADDRSTRLEN];
1164 int port;
1165
1166 port = get_host_port(&l->addr);
1167 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1168 case AF_INET:
1169 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1170 chunk_appendf(out, "%s:%d", str, port);
1171 break;
1172 case AF_INET6:
1173 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1174 chunk_appendf(out, "[%s]:%d", str, port);
1175 break;
1176 case AF_UNIX:
1177 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1178 break;
1179 case -1:
1180 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1181 chunk_strcat(out, strerror(errno));
1182 break;
1183 default: /* address family not supported */
1184 break;
1185 }
1186 }
1187
1188 return 1;
1189}
1190
1191/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1192 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1193 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1194 * otherwise.
1195 */
1196static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1197{
1198 struct appctx *appctx = __objt_appctx(si->end);
1199
1200 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1201 return 0;
1202
1203 return stats_dump_one_line(stats, flags, px, appctx);
1204}
1205
1206enum srv_stats_state {
1207 SRV_STATS_STATE_DOWN = 0,
1208 SRV_STATS_STATE_DOWN_AGENT,
1209 SRV_STATS_STATE_GOING_UP,
1210 SRV_STATS_STATE_UP_GOING_DOWN,
1211 SRV_STATS_STATE_UP,
1212 SRV_STATS_STATE_NOLB_GOING_DOWN,
1213 SRV_STATS_STATE_NOLB,
1214 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1215 SRV_STATS_STATE_DRAIN,
1216 SRV_STATS_STATE_DRAIN_AGENT,
1217 SRV_STATS_STATE_NO_CHECK,
1218
1219 SRV_STATS_STATE_COUNT, /* Must be last */
1220};
1221
1222static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1223 [SRV_STATS_STATE_DOWN] = "DOWN",
1224 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1225 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1226 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1227 [SRV_STATS_STATE_UP] = "UP",
1228 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1229 [SRV_STATS_STATE_NOLB] = "NOLB",
1230 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1231 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1232 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1233 [SRV_STATS_STATE_NO_CHECK] = "no check"
1234};
1235
1236/* Fill <stats> with the server statistics. <stats> is
1237 * preallocated array of length <len>. The length of the array
1238 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1239 * then this value, the function returns 0, otherwise, it
1240 * returns 1. <flags> can take the value ST_SHLGNDS.
1241 */
1242int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1243 struct field *stats, int len)
1244{
1245 struct server *via, *ref;
1246 char str[INET6_ADDRSTRLEN];
1247 struct chunk *out = get_trash_chunk();
1248 enum srv_stats_state state;
1249 char *fld_status;
1250
1251 if (len < ST_F_TOTAL_FIELDS)
1252 return 0;
1253
1254 memset(stats, 0, sizeof(*stats) * len);
1255
1256 /* we have "via" which is the tracked server as described in the configuration,
1257 * and "ref" which is the checked server and the end of the chain.
1258 */
1259 via = sv->track ? sv->track : sv;
1260 ref = via;
1261 while (ref->track)
1262 ref = ref->track;
1263
1264 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
1265 if ((ref->check.state & CHK_ST_ENABLED) &&
1266 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1267 state = SRV_STATS_STATE_UP_GOING_DOWN;
1268 } else {
1269 state = SRV_STATS_STATE_UP;
1270 }
1271
1272 if (sv->admin & SRV_ADMF_DRAIN) {
1273 if (ref->agent.state & CHK_ST_ENABLED)
1274 state = SRV_STATS_STATE_DRAIN_AGENT;
1275 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1276 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1277 else
1278 state = SRV_STATS_STATE_DRAIN;
1279 }
1280
1281 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1282 state = SRV_STATS_STATE_NO_CHECK;
1283 }
1284 }
1285 else if (sv->state == SRV_ST_STOPPING) {
1286 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1287 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1288 state = SRV_STATS_STATE_NOLB;
1289 } else {
1290 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1291 }
1292 }
1293 else { /* stopped */
1294 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1295 state = SRV_STATS_STATE_DOWN_AGENT;
1296 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1297 state = SRV_STATS_STATE_DOWN; /* DOWN */
1298 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1299 state = SRV_STATS_STATE_GOING_UP;
1300 } else {
1301 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1302 }
1303 }
1304
1305 chunk_reset(out);
1306
1307 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1308 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1309 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1310 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1311 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1312 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1313 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1314
1315 if (sv->maxconn)
1316 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1317
1318 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1319 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1320 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1321 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1322 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1323 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1324 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1325 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
1326
1327 /* status */
1328 fld_status = chunk_newstr(out);
1329 if (sv->admin & SRV_ADMF_RMAINT)
1330 chunk_appendf(out, "MAINT (resolution)");
1331 else if (sv->admin & SRV_ADMF_IMAINT)
1332 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
1333 else if (sv->admin & SRV_ADMF_MAINT)
1334 chunk_appendf(out, "MAINT");
1335 else
1336 chunk_appendf(out,
1337 srv_hlt_st[state],
1338 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1339 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
1340
1341 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1342 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
1343 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1344 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1345 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1346
1347 /* check failures: unique, fatal; last change, total downtime */
1348 if (sv->check.state & CHK_ST_ENABLED) {
1349 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1350 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1351 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1352 }
1353
1354 if (sv->maxqueue)
1355 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1356
1357 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1358 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1359 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1360
1361 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
1362 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1363
1364 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1365
1366 if (sv->track) {
1367 char *fld_track = chunk_newstr(out);
1368
1369 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1370 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1371 }
1372
1373 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1374 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1375 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1376
1377 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1378 const char *fld_chksts;
1379
1380 fld_chksts = chunk_newstr(out);
1381 chunk_strcat(out, "* "); // for check in progress
1382 chunk_strcat(out, get_check_status_info(sv->check.status));
1383 if (!(sv->check.state & CHK_ST_INPROGRESS))
1384 fld_chksts += 2; // skip "* "
1385 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1386
1387 if (sv->check.status >= HCHK_STATUS_L57DATA)
1388 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1389
1390 if (sv->check.status >= HCHK_STATUS_CHECKED)
1391 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1392
1393 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1394 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1395 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1396 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1397 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1398 }
1399
1400 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1401 const char *fld_chksts;
1402
1403 fld_chksts = chunk_newstr(out);
1404 chunk_strcat(out, "* "); // for check in progress
1405 chunk_strcat(out, get_check_status_info(sv->agent.status));
1406 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1407 fld_chksts += 2; // skip "* "
1408 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1409
1410 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1411 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1412
1413 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1414 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1415
1416 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1417 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1418 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1419 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1420 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1421 }
1422
1423 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1424 if (px->mode == PR_MODE_HTTP) {
1425 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1426 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1427 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1428 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1429 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1430 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1431 }
1432
1433 if (ref->observe)
1434 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1435
1436 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1437 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1438 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1439
1440 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1441 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1442 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1443 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1444
1445 if (flags & ST_SHLGNDS) {
1446 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1447 case AF_INET:
1448 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1449 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
1450 break;
1451 case AF_INET6:
1452 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1453 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
1454 break;
1455 case AF_UNIX:
1456 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1457 break;
1458 case -1:
1459 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1460 chunk_strcat(out, strerror(errno));
1461 break;
1462 default: /* address family not supported */
1463 break;
1464 }
1465
1466 if (sv->cookie)
1467 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1468 }
1469
1470 return 1;
1471}
1472
1473/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1474 * from stream interface <si>, stats flags <flags>, and server state <state>.
1475 * The caller is responsible for clearing the trash if needed. Returns non-zero
1476 * if it emits anything, zero otherwise.
1477 */
1478static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1479{
1480 struct appctx *appctx = __objt_appctx(si->end);
1481
1482 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1483 return 0;
1484
1485 return stats_dump_one_line(stats, flags, px, appctx);
1486}
1487
1488/* Fill <stats> with the backend statistics. <stats> is
1489 * preallocated array of length <len>. The length of the array
1490 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1491 * then this value, the function returns 0, otherwise, it
1492 * returns 1. <flags> can take the value ST_SHLGNDS.
1493 */
1494int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1495{
1496 if (len < ST_F_TOTAL_FIELDS)
1497 return 0;
1498
1499 memset(stats, 0, sizeof(*stats) * len);
1500
1501 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1502 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1503 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1504 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1505 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
1506 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
1507 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1508 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1509 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1510 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1511 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1512 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1513 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1514 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1515 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1516 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1517 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
1518 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1519 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1520 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1521 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1522 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1523 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1524 if (px->srv)
1525 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1526
1527 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1528 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1529 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1530 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1531 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1532 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1533 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1534
1535 if (flags & ST_SHLGNDS) {
1536 if (px->cookie_name)
1537 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1538 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1539 }
1540
1541 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1542 if (px->mode == PR_MODE_HTTP) {
1543 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1544 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1545 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1546 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1547 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1548 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1549 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
1550 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
1551 }
1552
1553 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1554 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1555
1556 /* compression: in, out, bypassed, responses */
1557 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1558 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1559 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1560 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1561 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1562
1563 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1564 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1565 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1566 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1567
1568 return 1;
1569}
1570
1571/* Dumps a line for backend <px> to the trash for and uses the state from stream
1572 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1573 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1574 */
1575static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1576{
1577 struct appctx *appctx = __objt_appctx(si->end);
1578
1579 if (!(px->cap & PR_CAP_BE))
1580 return 0;
1581
1582 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1583 return 0;
1584
1585 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1586 return 0;
1587
1588 return stats_dump_one_line(stats, flags, px, appctx);
1589}
1590
1591/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1592 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1593 * for clearing the trash if needed.
1594 */
1595static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1596{
1597 struct appctx *appctx = __objt_appctx(si->end);
1598 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1599
1600 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1601 /* A form to enable/disable this proxy servers */
1602
1603 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1604 scope_txt[0] = 0;
1605 if (appctx->ctx.stats.scope_len) {
1606 strcpy(scope_txt, STAT_SCOPE_PATTERN);
1607 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
1608 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1609 }
1610
1611 chunk_appendf(&trash,
1612 "<form method=\"post\">");
1613 }
1614
1615 /* print a new table */
1616 chunk_appendf(&trash,
1617 "<table class=\"tbl\" width=\"100%%\">\n"
1618 "<tr class=\"titre\">"
1619 "<th class=\"pxname\" width=\"10%%\">");
1620
1621 chunk_appendf(&trash,
1622 "<a name=\"%s\"></a>%s"
1623 "<a class=px href=\"#%s\">%s</a>",
1624 px->id,
1625 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1626 px->id, px->id);
1627
1628 if (uri->flags & ST_SHLGNDS) {
1629 /* cap, mode, id */
1630 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1631 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1632 px->uuid);
1633 chunk_appendf(&trash, "</div>");
1634 }
1635
1636 chunk_appendf(&trash,
1637 "%s</th>"
1638 "<th class=\"%s\" width=\"90%%\">%s</th>"
1639 "</tr>\n"
1640 "</table>\n"
1641 "<table class=\"tbl\" width=\"100%%\">\n"
1642 "<tr class=\"titre\">",
1643 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1644 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1645
1646 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1647 /* Column heading for Enable or Disable server */
1648 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
1649 }
1650
1651 chunk_appendf(&trash,
1652 "<th rowspan=2></th>"
1653 "<th colspan=3>Queue</th>"
1654 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1655 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1656 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1657 "<th colspan=9>Server</th>"
1658 "</tr>\n"
1659 "<tr class=\"titre\">"
1660 "<th>Cur</th><th>Max</th><th>Limit</th>"
1661 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1662 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1663 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1664 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1665 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
1666 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1667 "<th>Thrtle</th>\n"
1668 "</tr>");
1669}
1670
1671/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
1672 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1673 */
1674static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
1675{
1676 struct appctx *appctx = __objt_appctx(si->end);
1677 chunk_appendf(&trash, "</table>");
1678
1679 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1680 /* close the form used to enable/disable this proxy servers */
1681 chunk_appendf(&trash,
1682 "Choose the action to perform on the checked servers : "
1683 "<select name=action>"
1684 "<option value=\"\"></option>"
1685 "<option value=\"ready\">Set state to READY</option>"
1686 "<option value=\"drain\">Set state to DRAIN</option>"
1687 "<option value=\"maint\">Set state to MAINT</option>"
1688 "<option value=\"dhlth\">Health: disable checks</option>"
1689 "<option value=\"ehlth\">Health: enable checks</option>"
1690 "<option value=\"hrunn\">Health: force UP</option>"
1691 "<option value=\"hnolb\">Health: force NOLB</option>"
1692 "<option value=\"hdown\">Health: force DOWN</option>"
1693 "<option value=\"dagent\">Agent: disable checks</option>"
1694 "<option value=\"eagent\">Agent: enable checks</option>"
1695 "<option value=\"arunn\">Agent: force UP</option>"
1696 "<option value=\"adown\">Agent: force DOWN</option>"
1697 "<option value=\"shutdown\">Kill Sessions</option>"
1698 "</select>"
1699 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
1700 "&nbsp;<input type=\"submit\" value=\"Apply\">"
1701 "</form>",
1702 px->uuid);
1703 }
1704
1705 chunk_appendf(&trash, "<p>\n");
1706}
1707
1708/*
1709 * Dumps statistics for a proxy. The output is sent to the stream interface's
1710 * input buffer. Returns 0 if it had to stop dumping data because of lack of
1711 * buffer space, or non-zero if everything completed. This function is used
1712 * both by the CLI and the HTTP entry points, and is able to dump the output
1713 * in HTML or CSV formats. If the later, <uri> must be NULL.
1714 */
1715int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1716{
1717 struct appctx *appctx = __objt_appctx(si->end);
1718 struct stream *s = si_strm(si);
1719 struct channel *rep = si_ic(si);
1720 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
1721 struct listener *l;
1722 unsigned int flags;
1723
1724 if (uri)
1725 flags = uri->flags;
1726 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
1727 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
1728 else
1729 flags = ST_SHNODE | ST_SHDESC;
1730
1731 chunk_reset(&trash);
1732
1733 switch (appctx->ctx.stats.px_st) {
1734 case STAT_PX_ST_INIT:
1735 /* we are on a new proxy */
1736 if (uri && uri->scope) {
1737 /* we have a limited scope, we have to check the proxy name */
1738 struct stat_scope *scope;
1739 int len;
1740
1741 len = strlen(px->id);
1742 scope = uri->scope;
1743
1744 while (scope) {
1745 /* match exact proxy name */
1746 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1747 break;
1748
1749 /* match '.' which means 'self' proxy */
1750 if (!strcmp(scope->px_id, ".") && px == s->be)
1751 break;
1752 scope = scope->next;
1753 }
1754
1755 /* proxy name not found : don't dump anything */
1756 if (scope == NULL)
1757 return 1;
1758 }
1759
1760 /* if the user has requested a limited output and the proxy
1761 * name does not match, skip it.
1762 */
1763 if (appctx->ctx.stats.scope_len &&
1764 strnistr(px->id, strlen(px->id), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
1765 return 1;
1766
1767 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
1768 (appctx->ctx.stats.iid != -1) &&
1769 (px->uuid != appctx->ctx.stats.iid))
1770 return 1;
1771
1772 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
1773 /* fall through */
1774
1775 case STAT_PX_ST_TH:
1776 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
1777 stats_dump_html_px_hdr(si, px, uri);
1778 if (bi_putchk(rep, &trash) == -1) {
1779 si_applet_cant_put(si);
1780 return 0;
1781 }
1782 }
1783
1784 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
1785 /* fall through */
1786
1787 case STAT_PX_ST_FE:
1788 /* print the frontend */
1789 if (stats_dump_fe_stats(si, px)) {
1790 if (bi_putchk(rep, &trash) == -1) {
1791 si_applet_cant_put(si);
1792 return 0;
1793 }
1794 }
1795
1796 appctx->ctx.stats.l = px->conf.listeners.n;
1797 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
1798 /* fall through */
1799
1800 case STAT_PX_ST_LI:
1801 /* stats.l has been initialized above */
1802 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
1803 if (buffer_almost_full(rep->buf)) {
1804 si_applet_cant_put(si);
1805 return 0;
1806 }
1807
1808 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
1809 if (!l->counters)
1810 continue;
1811
1812 if (appctx->ctx.stats.flags & STAT_BOUND) {
1813 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
1814 break;
1815
1816 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
1817 continue;
1818 }
1819
1820 /* print the frontend */
1821 if (stats_dump_li_stats(si, px, l, flags)) {
1822 if (bi_putchk(rep, &trash) == -1) {
1823 si_applet_cant_put(si);
1824 return 0;
1825 }
1826 }
1827 }
1828
1829 appctx->ctx.stats.sv = px->srv; /* may be NULL */
1830 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
1831 /* fall through */
1832
1833 case STAT_PX_ST_SV:
1834 /* stats.sv has been initialized above */
1835 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
1836 if (buffer_almost_full(rep->buf)) {
1837 si_applet_cant_put(si);
1838 return 0;
1839 }
1840
1841 sv = appctx->ctx.stats.sv;
1842
1843 if (appctx->ctx.stats.flags & STAT_BOUND) {
1844 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
1845 break;
1846
1847 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
1848 continue;
1849 }
1850
1851 svs = sv;
1852 while (svs->track)
1853 svs = svs->track;
1854
1855 /* do not report servers which are DOWN and not changing state */
1856 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
1857 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
1858 (sv->state == SRV_ST_STOPPED && /* server is down */
1859 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
1860 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
1861 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
1862 continue;
1863 }
1864
1865 if (stats_dump_sv_stats(si, px, flags, sv)) {
1866 if (bi_putchk(rep, &trash) == -1) {
1867 si_applet_cant_put(si);
1868 return 0;
1869 }
1870 }
1871 } /* for sv */
1872
1873 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
1874 /* fall through */
1875
1876 case STAT_PX_ST_BE:
1877 /* print the backend */
1878 if (stats_dump_be_stats(si, px, flags)) {
1879 if (bi_putchk(rep, &trash) == -1) {
1880 si_applet_cant_put(si);
1881 return 0;
1882 }
1883 }
1884
1885 appctx->ctx.stats.px_st = STAT_PX_ST_END;
1886 /* fall through */
1887
1888 case STAT_PX_ST_END:
1889 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
1890 stats_dump_html_px_end(si, px);
1891 if (bi_putchk(rep, &trash) == -1) {
1892 si_applet_cant_put(si);
1893 return 0;
1894 }
1895 }
1896
1897 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
1898 /* fall through */
1899
1900 case STAT_PX_ST_FIN:
1901 return 1;
1902
1903 default:
1904 /* unknown state, we should put an abort() here ! */
1905 return 1;
1906 }
1907}
1908
1909/* Dumps the HTTP stats head block to the trash for and uses the per-uri
1910 * parameters <uri>. The caller is responsible for clearing the trash if needed.
1911 */
1912static void stats_dump_html_head(struct uri_auth *uri)
1913{
1914 /* WARNING! This must fit in the first buffer !!! */
1915 chunk_appendf(&trash,
1916 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1917 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
1918 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
1919 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1920 "<style type=\"text/css\"><!--\n"
1921 "body {"
1922 " font-family: arial, helvetica, sans-serif;"
1923 " font-size: 12px;"
1924 " font-weight: normal;"
1925 " color: black;"
1926 " background: white;"
1927 "}\n"
1928 "th,td {"
1929 " font-size: 10px;"
1930 "}\n"
1931 "h1 {"
1932 " font-size: x-large;"
1933 " margin-bottom: 0.5em;"
1934 "}\n"
1935 "h2 {"
1936 " font-family: helvetica, arial;"
1937 " font-size: x-large;"
1938 " font-weight: bold;"
1939 " font-style: italic;"
1940 " color: #6020a0;"
1941 " margin-top: 0em;"
1942 " margin-bottom: 0em;"
1943 "}\n"
1944 "h3 {"
1945 " font-family: helvetica, arial;"
1946 " font-size: 16px;"
1947 " font-weight: bold;"
1948 " color: #b00040;"
1949 " background: #e8e8d0;"
1950 " margin-top: 0em;"
1951 " margin-bottom: 0em;"
1952 "}\n"
1953 "li {"
1954 " margin-top: 0.25em;"
1955 " margin-right: 2em;"
1956 "}\n"
1957 ".hr {margin-top: 0.25em;"
1958 " border-color: black;"
1959 " border-bottom-style: solid;"
1960 "}\n"
1961 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
1962 ".total {background: #20D0D0;color: #ffff80;}\n"
1963 ".frontend {background: #e8e8d0;}\n"
1964 ".socket {background: #d0d0d0;}\n"
1965 ".backend {background: #e8e8d0;}\n"
1966 ".active_down {background: #ff9090;}\n"
1967 ".active_going_up {background: #ffd020;}\n"
1968 ".active_going_down {background: #ffffa0;}\n"
1969 ".active_up {background: #c0ffc0;}\n"
1970 ".active_nolb {background: #20a0ff;}\n"
1971 ".active_draining {background: #20a0FF;}\n"
1972 ".active_no_check {background: #e0e0e0;}\n"
1973 ".backup_down {background: #ff9090;}\n"
1974 ".backup_going_up {background: #ff80ff;}\n"
1975 ".backup_going_down {background: #c060ff;}\n"
1976 ".backup_up {background: #b0d0ff;}\n"
1977 ".backup_nolb {background: #90b0e0;}\n"
1978 ".backup_draining {background: #cc9900;}\n"
1979 ".backup_no_check {background: #e0e0e0;}\n"
1980 ".maintain {background: #c07820;}\n"
1981 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
1982 "\n"
1983 "a.px:link {color: #ffff40; text-decoration: none;}"
1984 "a.px:visited {color: #ffff40; text-decoration: none;}"
1985 "a.px:hover {color: #ffffff; text-decoration: none;}"
1986 "a.lfsb:link {color: #000000; text-decoration: none;}"
1987 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1988 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1989 "\n"
1990 "table.tbl { border-collapse: collapse; border-style: none;}\n"
1991 "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"
1992 "table.tbl td.ac { text-align: center;}\n"
1993 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
1994 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
1995 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
1996 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
1997 "\n"
1998 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1999 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2000 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2001 "table.det { border-collapse: collapse; border-style: none; }\n"
2002 "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"
2003 "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"
2004 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2005 "div.tips {\n"
2006 " display:block;\n"
2007 " visibility:hidden;\n"
2008 " z-index:2147483647;\n"
2009 " position:absolute;\n"
2010 " padding:2px 4px 3px;\n"
2011 " background:#f0f060; color:#000000;\n"
2012 " border:1px solid #7040c0;\n"
2013 " white-space:nowrap;\n"
2014 " font-style:normal;font-size:11px;font-weight:normal;\n"
2015 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2016 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2017 "}\n"
2018 "u:hover div.tips {visibility:visible;}\n"
2019 "-->\n"
2020 "</style></head>\n",
2021 (uri->flags & ST_SHNODE) ? " on " : "",
2022 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2023 );
2024}
2025
2026/* Dumps the HTML stats information block to the trash for and uses the state from
2027 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2028 * for clearing the trash if needed.
2029 */
2030static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2031{
2032 struct appctx *appctx = __objt_appctx(si->end);
2033 unsigned int up = (now.tv_sec - start_date.tv_sec);
2034 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2035
2036 /* WARNING! this has to fit the first packet too.
2037 * We are around 3.5 kB, add adding entries will
2038 * become tricky if we want to support 4kB buffers !
2039 */
2040 chunk_appendf(&trash,
2041 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2042 PRODUCT_NAME "%s</a></h1>\n"
2043 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2044 "<hr width=\"100%%\" class=\"hr\">\n"
2045 "<h3>&gt; General process information</h3>\n"
2046 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
2047 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
2048 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2049 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2050 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2051 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2052 "Running tasks: %d/%d; idle = %d %%<br>\n"
2053 "</td><td align=\"center\" nowrap>\n"
2054 "<table class=\"lgd\"><tr>\n"
2055 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2056 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2057 "</tr><tr>\n"
2058 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2059 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2060 "</tr><tr>\n"
2061 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2062 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2063 "</tr><tr>\n"
2064 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2065 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2066 "</tr><tr>\n"
2067 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2068 "</tr><tr>\n"
2069 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2070 "</tr></table>\n"
2071 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2072 "</td>"
2073 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2074 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2075 "",
2076 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
2077 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2078 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2079 (uri->flags & ST_SHDESC) ? ": " : "",
2080 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2081 pid, relative_pid, global.nbproc,
2082 up / 86400, (up % 86400) / 3600,
2083 (up % 3600) / 60, (up % 60),
2084 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2085 global.rlimit_memmax ? " MB" : "",
2086 global.rlimit_nofile,
2087 global.maxsock, global.maxconn, global.maxpipes,
2088 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
2089 run_queue_cur, nb_tasks_cur, idle_pct
2090 );
2091
2092 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2093 memcpy(scope_txt, bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2094 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2095
2096 chunk_appendf(&trash,
2097 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2098 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2099 STAT_SCOPE_TXT_MAXLEN);
2100
2101 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2102 scope_txt[0] = 0;
2103 if (appctx->ctx.stats.scope_len) {
2104 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2105 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2106 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2107 }
2108
2109 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2110 chunk_appendf(&trash,
2111 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2112 uri->uri_prefix,
2113 "",
2114 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2115 scope_txt);
2116 else
2117 chunk_appendf(&trash,
2118 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2119 uri->uri_prefix,
2120 ";up",
2121 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2122 scope_txt);
2123
2124 if (uri->refresh > 0) {
2125 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2126 chunk_appendf(&trash,
2127 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2128 uri->uri_prefix,
2129 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2130 "",
2131 scope_txt);
2132 else
2133 chunk_appendf(&trash,
2134 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2135 uri->uri_prefix,
2136 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2137 ";norefresh",
2138 scope_txt);
2139 }
2140
2141 chunk_appendf(&trash,
2142 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2143 uri->uri_prefix,
2144 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2145 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2146 scope_txt);
2147
2148 chunk_appendf(&trash,
2149 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2150 uri->uri_prefix,
2151 (uri->refresh > 0) ? ";norefresh" : "",
2152 scope_txt);
2153
2154 chunk_appendf(&trash,
2155 "</ul></td>"
2156 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2157 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2158 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2159 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2160 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2161 "</ul>"
2162 "</td>"
2163 "</tr></table>\n"
2164 ""
2165 );
2166
2167 if (appctx->ctx.stats.st_code) {
2168 switch (appctx->ctx.stats.st_code) {
2169 case STAT_STATUS_DONE:
2170 chunk_appendf(&trash,
2171 "<p><div class=active_up>"
2172 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2173 "Action processed successfully."
2174 "</div>\n", uri->uri_prefix,
2175 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2176 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2177 scope_txt);
2178 break;
2179 case STAT_STATUS_NONE:
2180 chunk_appendf(&trash,
2181 "<p><div class=active_going_down>"
2182 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2183 "Nothing has changed."
2184 "</div>\n", uri->uri_prefix,
2185 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2186 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2187 scope_txt);
2188 break;
2189 case STAT_STATUS_PART:
2190 chunk_appendf(&trash,
2191 "<p><div class=active_going_down>"
2192 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2193 "Action partially processed.<br>"
2194 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2195 "</div>\n", uri->uri_prefix,
2196 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2197 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2198 scope_txt);
2199 break;
2200 case STAT_STATUS_ERRP:
2201 chunk_appendf(&trash,
2202 "<p><div class=active_down>"
2203 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2204 "Action not processed because of invalid parameters."
2205 "<ul>"
2206 "<li>The action is maybe unknown.</li>"
2207 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2208 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2209 "</ul>"
2210 "</div>\n", uri->uri_prefix,
2211 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2212 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2213 scope_txt);
2214 break;
2215 case STAT_STATUS_EXCD:
2216 chunk_appendf(&trash,
2217 "<p><div class=active_down>"
2218 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2219 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2220 "You should retry with less servers at a time.</b>"
2221 "</div>\n", uri->uri_prefix,
2222 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2223 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2224 scope_txt);
2225 break;
2226 case STAT_STATUS_DENY:
2227 chunk_appendf(&trash,
2228 "<p><div class=active_down>"
2229 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2230 "<b>Action denied.</b>"
2231 "</div>\n", uri->uri_prefix,
2232 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2233 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2234 scope_txt);
2235 break;
2236 default:
2237 chunk_appendf(&trash,
2238 "<p><div class=active_no_check>"
2239 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2240 "Unexpected result."
2241 "</div>\n", uri->uri_prefix,
2242 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2243 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2244 scope_txt);
2245 }
2246 chunk_appendf(&trash, "<p>\n");
2247 }
2248}
2249
2250/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2251 * for clearing the trash if needed.
2252 */
2253static void stats_dump_html_end()
2254{
2255 chunk_appendf(&trash, "</body></html>\n");
2256}
2257
2258/* This function dumps statistics onto the stream interface's read buffer in
2259 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2260 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2261 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2262 * and the stream must be closed, or -1 in case of any error. This function is
2263 * used by both the CLI and the HTTP handlers.
2264 */
Willy Tarreau2b812e22016-11-22 16:18:05 +01002265static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002266{
2267 struct appctx *appctx = __objt_appctx(si->end);
2268 struct channel *rep = si_ic(si);
2269 struct proxy *px;
2270
2271 chunk_reset(&trash);
2272
2273 switch (appctx->st2) {
2274 case STAT_ST_INIT:
2275 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2276 /* fall through */
2277
2278 case STAT_ST_HEAD:
2279 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2280 stats_dump_html_head(uri);
2281 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2282 stats_dump_csv_header();
2283
2284 if (bi_putchk(rep, &trash) == -1) {
2285 si_applet_cant_put(si);
2286 return 0;
2287 }
2288
2289 appctx->st2 = STAT_ST_INFO;
2290 /* fall through */
2291
2292 case STAT_ST_INFO:
2293 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2294 stats_dump_html_info(si, uri);
2295 if (bi_putchk(rep, &trash) == -1) {
2296 si_applet_cant_put(si);
2297 return 0;
2298 }
2299 }
2300
2301 appctx->ctx.stats.px = proxy;
2302 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2303 appctx->st2 = STAT_ST_LIST;
2304 /* fall through */
2305
2306 case STAT_ST_LIST:
2307 /* dump proxies */
2308 while (appctx->ctx.stats.px) {
2309 if (buffer_almost_full(rep->buf)) {
2310 si_applet_cant_put(si);
2311 return 0;
2312 }
2313
2314 px = appctx->ctx.stats.px;
2315 /* skip the disabled proxies, global frontend and non-networked ones */
2316 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
2317 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
2318 return 0;
2319
2320 appctx->ctx.stats.px = px->next;
2321 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2322 }
2323 /* here, we just have reached the last proxy */
2324
2325 appctx->st2 = STAT_ST_END;
2326 /* fall through */
2327
2328 case STAT_ST_END:
2329 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2330 stats_dump_html_end();
2331 if (bi_putchk(rep, &trash) == -1) {
2332 si_applet_cant_put(si);
2333 return 0;
2334 }
2335 }
2336
2337 appctx->st2 = STAT_ST_FIN;
2338 /* fall through */
2339
2340 case STAT_ST_FIN:
2341 return 1;
2342
2343 default:
2344 /* unknown state ! */
2345 appctx->st2 = STAT_ST_FIN;
2346 return -1;
2347 }
2348}
2349
2350/* We reached the stats page through a POST request. The appctx is
2351 * expected to have already been allocated by the caller.
2352 * Parse the posted data and enable/disable servers if necessary.
2353 * Returns 1 if request was parsed or zero if it needs more data.
2354 */
2355static int stats_process_http_post(struct stream_interface *si)
2356{
2357 struct stream *s = si_strm(si);
2358 struct appctx *appctx = objt_appctx(si->end);
2359
2360 struct proxy *px = NULL;
2361 struct server *sv = NULL;
2362
2363 char key[LINESIZE];
2364 int action = ST_ADM_ACTION_NONE;
2365 int reprocess = 0;
2366
2367 int total_servers = 0;
2368 int altered_servers = 0;
2369
2370 char *first_param, *cur_param, *next_param, *end_params;
2371 char *st_cur_param = NULL;
2372 char *st_next_param = NULL;
2373
2374 struct chunk *temp;
2375 int reql;
2376
2377 temp = get_trash_chunk();
2378 if (temp->size < s->txn->req.body_len) {
2379 /* too large request */
2380 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2381 goto out;
2382 }
2383
2384 reql = bo_getblk(si_oc(si), temp->str, s->txn->req.body_len, s->txn->req.eoh + 2);
2385 if (reql <= 0) {
2386 /* we need more data */
2387 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2388 return 0;
2389 }
2390
2391 first_param = temp->str;
2392 end_params = temp->str + reql;
2393 cur_param = next_param = end_params;
2394 *end_params = '\0';
2395
2396 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2397
2398 /*
2399 * Parse the parameters in reverse order to only store the last value.
2400 * From the html form, the backend and the action are at the end.
2401 */
2402 while (cur_param > first_param) {
2403 char *value;
2404 int poffset, plen;
2405
2406 cur_param--;
2407
2408 if ((*cur_param == '&') || (cur_param == first_param)) {
2409 reprocess_servers:
2410 /* Parse the key */
2411 poffset = (cur_param != first_param ? 1 : 0);
2412 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2413 if ((plen > 0) && (plen <= sizeof(key))) {
2414 strncpy(key, cur_param + poffset, plen);
2415 key[plen - 1] = '\0';
2416 } else {
2417 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2418 goto out;
2419 }
2420
2421 /* Parse the value */
2422 value = key;
2423 while (*value != '\0' && *value != '=') {
2424 value++;
2425 }
2426 if (*value == '=') {
2427 /* Ok, a value is found, we can mark the end of the key */
2428 *value++ = '\0';
2429 }
2430 if (url_decode(key) < 0 || url_decode(value) < 0)
2431 break;
2432
2433 /* Now we can check the key to see what to do */
2434 if (!px && (strcmp(key, "b") == 0)) {
2435 if ((px = proxy_be_by_name(value)) == NULL) {
2436 /* the backend name is unknown or ambiguous (duplicate names) */
2437 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2438 goto out;
2439 }
2440 }
2441 else if (!action && (strcmp(key, "action") == 0)) {
2442 if (strcmp(value, "ready") == 0) {
2443 action = ST_ADM_ACTION_READY;
2444 }
2445 else if (strcmp(value, "drain") == 0) {
2446 action = ST_ADM_ACTION_DRAIN;
2447 }
2448 else if (strcmp(value, "maint") == 0) {
2449 action = ST_ADM_ACTION_MAINT;
2450 }
2451 else if (strcmp(value, "shutdown") == 0) {
2452 action = ST_ADM_ACTION_SHUTDOWN;
2453 }
2454 else if (strcmp(value, "dhlth") == 0) {
2455 action = ST_ADM_ACTION_DHLTH;
2456 }
2457 else if (strcmp(value, "ehlth") == 0) {
2458 action = ST_ADM_ACTION_EHLTH;
2459 }
2460 else if (strcmp(value, "hrunn") == 0) {
2461 action = ST_ADM_ACTION_HRUNN;
2462 }
2463 else if (strcmp(value, "hnolb") == 0) {
2464 action = ST_ADM_ACTION_HNOLB;
2465 }
2466 else if (strcmp(value, "hdown") == 0) {
2467 action = ST_ADM_ACTION_HDOWN;
2468 }
2469 else if (strcmp(value, "dagent") == 0) {
2470 action = ST_ADM_ACTION_DAGENT;
2471 }
2472 else if (strcmp(value, "eagent") == 0) {
2473 action = ST_ADM_ACTION_EAGENT;
2474 }
2475 else if (strcmp(value, "arunn") == 0) {
2476 action = ST_ADM_ACTION_ARUNN;
2477 }
2478 else if (strcmp(value, "adown") == 0) {
2479 action = ST_ADM_ACTION_ADOWN;
2480 }
2481 /* else these are the old supported methods */
2482 else if (strcmp(value, "disable") == 0) {
2483 action = ST_ADM_ACTION_DISABLE;
2484 }
2485 else if (strcmp(value, "enable") == 0) {
2486 action = ST_ADM_ACTION_ENABLE;
2487 }
2488 else if (strcmp(value, "stop") == 0) {
2489 action = ST_ADM_ACTION_STOP;
2490 }
2491 else if (strcmp(value, "start") == 0) {
2492 action = ST_ADM_ACTION_START;
2493 }
2494 else {
2495 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2496 goto out;
2497 }
2498 }
2499 else if (strcmp(key, "s") == 0) {
2500 if (!(px && action)) {
2501 /*
2502 * Indicates that we'll need to reprocess the parameters
2503 * as soon as backend and action are known
2504 */
2505 if (!reprocess) {
2506 st_cur_param = cur_param;
2507 st_next_param = next_param;
2508 }
2509 reprocess = 1;
2510 }
2511 else if ((sv = findserver(px, value)) != NULL) {
2512 switch (action) {
2513 case ST_ADM_ACTION_DISABLE:
2514 if (!(sv->admin & SRV_ADMF_FMAINT)) {
2515 altered_servers++;
2516 total_servers++;
2517 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2518 }
2519 break;
2520 case ST_ADM_ACTION_ENABLE:
2521 if (sv->admin & SRV_ADMF_FMAINT) {
2522 altered_servers++;
2523 total_servers++;
2524 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2525 }
2526 break;
2527 case ST_ADM_ACTION_STOP:
2528 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
2529 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2530 altered_servers++;
2531 total_servers++;
2532 }
2533 break;
2534 case ST_ADM_ACTION_START:
2535 if (sv->admin & SRV_ADMF_FDRAIN) {
2536 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2537 altered_servers++;
2538 total_servers++;
2539 }
2540 break;
2541 case ST_ADM_ACTION_DHLTH:
2542 if (sv->check.state & CHK_ST_CONFIGURED) {
2543 sv->check.state &= ~CHK_ST_ENABLED;
2544 altered_servers++;
2545 total_servers++;
2546 }
2547 break;
2548 case ST_ADM_ACTION_EHLTH:
2549 if (sv->check.state & CHK_ST_CONFIGURED) {
2550 sv->check.state |= CHK_ST_ENABLED;
2551 altered_servers++;
2552 total_servers++;
2553 }
2554 break;
2555 case ST_ADM_ACTION_HRUNN:
2556 if (!(sv->track)) {
2557 sv->check.health = sv->check.rise + sv->check.fall - 1;
2558 srv_set_running(sv, "changed from Web interface");
2559 altered_servers++;
2560 total_servers++;
2561 }
2562 break;
2563 case ST_ADM_ACTION_HNOLB:
2564 if (!(sv->track)) {
2565 sv->check.health = sv->check.rise + sv->check.fall - 1;
2566 srv_set_stopping(sv, "changed from Web interface");
2567 altered_servers++;
2568 total_servers++;
2569 }
2570 break;
2571 case ST_ADM_ACTION_HDOWN:
2572 if (!(sv->track)) {
2573 sv->check.health = 0;
2574 srv_set_stopped(sv, "changed from Web interface");
2575 altered_servers++;
2576 total_servers++;
2577 }
2578 break;
2579 case ST_ADM_ACTION_DAGENT:
2580 if (sv->agent.state & CHK_ST_CONFIGURED) {
2581 sv->agent.state &= ~CHK_ST_ENABLED;
2582 altered_servers++;
2583 total_servers++;
2584 }
2585 break;
2586 case ST_ADM_ACTION_EAGENT:
2587 if (sv->agent.state & CHK_ST_CONFIGURED) {
2588 sv->agent.state |= CHK_ST_ENABLED;
2589 altered_servers++;
2590 total_servers++;
2591 }
2592 break;
2593 case ST_ADM_ACTION_ARUNN:
2594 if (sv->agent.state & CHK_ST_ENABLED) {
2595 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
2596 srv_set_running(sv, "changed from Web interface");
2597 altered_servers++;
2598 total_servers++;
2599 }
2600 break;
2601 case ST_ADM_ACTION_ADOWN:
2602 if (sv->agent.state & CHK_ST_ENABLED) {
2603 sv->agent.health = 0;
2604 srv_set_stopped(sv, "changed from Web interface");
2605 altered_servers++;
2606 total_servers++;
2607 }
2608 break;
2609 case ST_ADM_ACTION_READY:
2610 srv_adm_set_ready(sv);
2611 altered_servers++;
2612 total_servers++;
2613 break;
2614 case ST_ADM_ACTION_DRAIN:
2615 srv_adm_set_drain(sv);
2616 altered_servers++;
2617 total_servers++;
2618 break;
2619 case ST_ADM_ACTION_MAINT:
2620 srv_adm_set_maint(sv);
2621 altered_servers++;
2622 total_servers++;
2623 break;
2624 case ST_ADM_ACTION_SHUTDOWN:
2625 if (px->state != PR_STSTOPPED) {
2626 struct stream *sess, *sess_bck;
2627
2628 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2629 if (sess->srv_conn == sv)
2630 stream_shutdown(sess, SF_ERR_KILLED);
2631
2632 altered_servers++;
2633 total_servers++;
2634 }
2635 break;
2636 }
2637 } else {
2638 /* the server name is unknown or ambiguous (duplicate names) */
2639 total_servers++;
2640 }
2641 }
2642 if (reprocess && px && action) {
2643 /* Now, we know the backend and the action chosen by the user.
2644 * We can safely restart from the first server parameter
2645 * to reprocess them
2646 */
2647 cur_param = st_cur_param;
2648 next_param = st_next_param;
2649 reprocess = 0;
2650 goto reprocess_servers;
2651 }
2652
2653 next_param = cur_param;
2654 }
2655 }
2656
2657 if (total_servers == 0) {
2658 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2659 }
2660 else if (altered_servers == 0) {
2661 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2662 }
2663 else if (altered_servers == total_servers) {
2664 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
2665 }
2666 else {
2667 appctx->ctx.stats.st_code = STAT_STATUS_PART;
2668 }
2669 out:
2670 return 1;
2671}
2672
2673
2674static int stats_send_http_headers(struct stream_interface *si)
2675{
2676 struct stream *s = si_strm(si);
2677 struct uri_auth *uri = s->be->uri_auth;
2678 struct appctx *appctx = objt_appctx(si->end);
2679
2680 chunk_printf(&trash,
2681 "HTTP/1.1 200 OK\r\n"
2682 "Cache-Control: no-cache\r\n"
2683 "Connection: close\r\n"
2684 "Content-Type: %s\r\n",
2685 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
2686
2687 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
2688 chunk_appendf(&trash, "Refresh: %d\r\n",
2689 uri->refresh);
2690
2691 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
2692
2693 if (appctx->ctx.stats.flags & STAT_CHUNKED)
2694 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
2695 else
2696 chunk_appendf(&trash, "\r\n");
2697
2698 s->txn->status = 200;
2699 s->logs.tv_request = now;
2700
2701 if (bi_putchk(si_ic(si), &trash) == -1) {
2702 si_applet_cant_put(si);
2703 return 0;
2704 }
2705
2706 return 1;
2707}
2708
2709static int stats_send_http_redirect(struct stream_interface *si)
2710{
2711 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2712 struct stream *s = si_strm(si);
2713 struct uri_auth *uri = s->be->uri_auth;
2714 struct appctx *appctx = objt_appctx(si->end);
2715
2716 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2717 scope_txt[0] = 0;
2718 if (appctx->ctx.stats.scope_len) {
2719 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2720 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2721 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2722 }
2723
2724 /* We don't want to land on the posted stats page because a refresh will
2725 * repost the data. We don't want this to happen on accident so we redirect
2726 * the browse to the stats page with a GET.
2727 */
2728 chunk_printf(&trash,
2729 "HTTP/1.1 303 See Other\r\n"
2730 "Cache-Control: no-cache\r\n"
2731 "Content-Type: text/plain\r\n"
2732 "Connection: close\r\n"
2733 "Location: %s;st=%s%s%s%s\r\n"
2734 "Content-length: 0\r\n"
2735 "\r\n",
2736 uri->uri_prefix,
2737 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
2738 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
2739 stat_status_codes[appctx->ctx.stats.st_code]) ?
2740 stat_status_codes[appctx->ctx.stats.st_code] :
2741 stat_status_codes[STAT_STATUS_UNKN],
2742 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2743 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2744 scope_txt);
2745
2746 s->txn->status = 303;
2747 s->logs.tv_request = now;
2748
2749 if (bi_putchk(si_ic(si), &trash) == -1) {
2750 si_applet_cant_put(si);
2751 return 0;
2752 }
2753
2754 return 1;
2755}
2756
2757/* This I/O handler runs as an applet embedded in a stream interface. It is
2758 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
2759 * appctx->st0 contains the operation in progress (dump, done). The handler
2760 * automatically unregisters itself once transfer is complete.
2761 */
2762static void http_stats_io_handler(struct appctx *appctx)
2763{
2764 struct stream_interface *si = appctx->owner;
2765 struct stream *s = si_strm(si);
2766 struct channel *req = si_oc(si);
2767 struct channel *res = si_ic(si);
2768
2769 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2770 goto out;
2771
2772 /* check that the output is not closed */
2773 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
2774 appctx->st0 = STAT_HTTP_DONE;
2775
2776 /* all states are processed in sequence */
2777 if (appctx->st0 == STAT_HTTP_HEAD) {
2778 if (stats_send_http_headers(si)) {
2779 if (s->txn->meth == HTTP_METH_HEAD)
2780 appctx->st0 = STAT_HTTP_DONE;
2781 else
2782 appctx->st0 = STAT_HTTP_DUMP;
2783 }
2784 }
2785
2786 if (appctx->st0 == STAT_HTTP_DUMP) {
2787 unsigned int prev_len = si_ib(si)->i;
2788 unsigned int data_len;
2789 unsigned int last_len;
2790 unsigned int last_fwd = 0;
2791
2792 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
2793 /* One difficulty we're facing is that we must prevent
2794 * the input data from being automatically forwarded to
2795 * the output area. For this, we temporarily disable
2796 * forwarding on the channel.
2797 */
2798 last_fwd = si_ic(si)->to_forward;
2799 si_ic(si)->to_forward = 0;
2800 chunk_printf(&trash, "\r\n000000\r\n");
2801 if (bi_putchk(si_ic(si), &trash) == -1) {
2802 si_applet_cant_put(si);
2803 si_ic(si)->to_forward = last_fwd;
2804 goto out;
2805 }
2806 }
2807
2808 data_len = si_ib(si)->i;
2809 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
2810 appctx->st0 = STAT_HTTP_DONE;
2811
2812 last_len = si_ib(si)->i;
2813
2814 /* Now we must either adjust or remove the chunk size. This is
2815 * not easy because the chunk size might wrap at the end of the
2816 * buffer, so we pretend we have nothing in the buffer, we write
2817 * the size, then restore the buffer's contents. Note that we can
2818 * only do that because no forwarding is scheduled on the stats
2819 * applet.
2820 */
2821 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
2822 si_ic(si)->total -= (last_len - prev_len);
2823 si_ib(si)->i -= (last_len - prev_len);
2824
2825 if (last_len != data_len) {
2826 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
2827 if (bi_putchk(si_ic(si), &trash) == -1)
2828 si_applet_cant_put(si);
2829
2830 si_ic(si)->total += (last_len - data_len);
2831 si_ib(si)->i += (last_len - data_len);
2832 }
2833 /* now re-enable forwarding */
2834 channel_forward(si_ic(si), last_fwd);
2835 }
2836 }
2837
2838 if (appctx->st0 == STAT_HTTP_POST) {
2839 if (stats_process_http_post(si))
2840 appctx->st0 = STAT_HTTP_LAST;
2841 else if (si_oc(si)->flags & CF_SHUTR)
2842 appctx->st0 = STAT_HTTP_DONE;
2843 }
2844
2845 if (appctx->st0 == STAT_HTTP_LAST) {
2846 if (stats_send_http_redirect(si))
2847 appctx->st0 = STAT_HTTP_DONE;
2848 }
2849
2850 if (appctx->st0 == STAT_HTTP_DONE) {
2851 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
2852 chunk_printf(&trash, "\r\n0\r\n\r\n");
2853 if (bi_putchk(si_ic(si), &trash) == -1) {
2854 si_applet_cant_put(si);
2855 goto out;
2856 }
2857 }
2858 /* eat the whole request */
2859 bo_skip(si_oc(si), si_ob(si)->o);
2860 res->flags |= CF_READ_NULL;
2861 si_shutr(si);
2862 }
2863
2864 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
2865 si_shutw(si);
2866
2867 if (appctx->st0 == STAT_HTTP_DONE) {
2868 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
2869 si_shutr(si);
2870 res->flags |= CF_READ_NULL;
2871 }
2872 }
2873 out:
2874 /* just to make gcc happy */ ;
2875}
2876
Willy Tarreau0baac8c2016-11-22 16:36:53 +01002877/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2878static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2879{
2880 int field;
2881
2882 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2883 if (!field_format(info, field))
2884 continue;
2885
2886 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2887 return 0;
2888 if (!stats_emit_raw_data_field(out, &info[field]))
2889 return 0;
2890 if (!chunk_strcat(out, "\n"))
2891 return 0;
2892 }
2893 return 1;
2894}
2895
2896/* Dump all fields from <info> into <out> using the "show info typed" format */
2897static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
2898{
2899 int field;
2900
2901 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2902 if (!field_format(info, field))
2903 continue;
2904
2905 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
2906 return 0;
2907 if (!stats_emit_field_tags(out, &info[field], ':'))
2908 return 0;
2909 if (!stats_emit_typed_data_field(out, &info[field]))
2910 return 0;
2911 if (!chunk_strcat(out, "\n"))
2912 return 0;
2913 }
2914 return 1;
2915}
2916
2917/* Fill <info> with HAProxy global info. <info> is preallocated
2918 * array of length <len>. The length of the aray must be
2919 * INF_TOTAL_FIELDS. If this length is less then this value, the
2920 * function returns 0, otherwise, it returns 1.
2921 */
2922int stats_fill_info(struct field *info, int len)
2923{
2924 unsigned int up = (now.tv_sec - start_date.tv_sec);
2925 struct chunk *out = get_trash_chunk();
2926
2927#ifdef USE_OPENSSL
2928 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2929 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2930 int ssl_reuse = 0;
2931
2932 if (ssl_key_rate < ssl_sess_rate) {
2933 /* count the ssl reuse ratio and avoid overflows in both directions */
2934 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2935 }
2936#endif
2937
2938 if (len < INF_TOTAL_FIELDS)
2939 return 0;
2940
2941 chunk_reset(out);
2942 memset(info, 0, sizeof(*info) * len);
2943
2944 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
2945 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
2946 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
2947
2948 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
2949 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
2950 info[INF_PID] = mkf_u32(FO_STATUS, pid);
2951
2952 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
2953 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
2954
2955 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
2956 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
2957 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
2958 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
2959 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
2960 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
2961 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
2962 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
2963 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
2964 info[INF_CURR_CONN] = mkf_u32(0, actconn);
2965 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
2966 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
2967#ifdef USE_OPENSSL
2968 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
2969 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
2970 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
2971#endif
2972 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
2973 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
2974 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
2975 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
2976 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
2977 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
2978 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
2979 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
2980 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
2981
2982#ifdef USE_OPENSSL
2983 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
2984 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
2985 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
2986 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
2987 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
2988 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
2989 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
2990 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
2991 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
2992 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
2993#endif
2994 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
2995 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
2996 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
2997#ifdef USE_ZLIB
2998 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
2999 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3000#endif
3001 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3002 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3003 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3004 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3005 if (global.desc)
3006 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3007
3008 return 1;
3009}
3010
3011/* This function dumps information onto the stream interface's read buffer.
3012 * It returns 0 as long as it does not complete, non-zero upon completion.
3013 * No state is used.
3014 */
3015static int stats_dump_info_to_buffer(struct stream_interface *si)
3016{
3017 struct appctx *appctx = __objt_appctx(si->end);
3018
3019 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3020 return 0;
3021
3022 chunk_reset(&trash);
3023
3024 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3025 stats_dump_typed_info_fields(&trash, info);
3026 else
3027 stats_dump_info_fields(&trash, info);
3028
3029 if (bi_putchk(si_ic(si), &trash) == -1) {
3030 si_applet_cant_put(si);
3031 return 0;
3032 }
3033
3034 return 1;
3035}
3036
3037static int cli_parse_show_info(char **args, struct appctx *appctx, void *private)
3038{
3039 if (strcmp(args[2], "typed") == 0)
3040 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3041 appctx->st2 = STAT_ST_INIT;
3042 return 0;
3043}
3044
3045
Willy Tarreau2b812e22016-11-22 16:18:05 +01003046static int cli_parse_show_stat(char **args, struct appctx *appctx, void *private)
3047{
3048 if (*args[2] && *args[3] && *args[4]) {
3049 appctx->ctx.stats.flags |= STAT_BOUND;
3050 appctx->ctx.stats.iid = atoi(args[2]);
3051 appctx->ctx.stats.type = atoi(args[3]);
3052 appctx->ctx.stats.sid = atoi(args[4]);
3053 if (strcmp(args[5], "typed") == 0)
3054 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3055 }
3056 else if (strcmp(args[2], "typed") == 0)
3057 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3058
3059 appctx->st2 = STAT_ST_INIT;
3060 return 0;
3061}
3062
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003063static int cli_io_handler_dump_info(struct appctx *appctx)
3064{
3065 return stats_dump_info_to_buffer(appctx->owner);
3066}
3067
Willy Tarreau2b812e22016-11-22 16:18:05 +01003068/* This I/O handler runs as an applet embedded in a stream interface. It is
3069 * used to send raw stats over a socket.
3070 */
3071static int cli_io_handler_dump_stat(struct appctx *appctx)
3072{
3073 return stats_dump_stat_to_buffer(appctx->owner, NULL);
3074}
3075
3076/* register cli keywords */
3077static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003078 { { "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 +01003079 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
3080 {{},}
3081}};
3082
William Lallemand74c24fb2016-11-21 17:18:36 +01003083struct applet http_stats_applet = {
3084 .obj_type = OBJ_TYPE_APPLET,
3085 .name = "<STATS>", /* used for logging */
3086 .fct = http_stats_io_handler,
3087 .release = NULL,
3088};
3089
Willy Tarreau2b812e22016-11-22 16:18:05 +01003090__attribute__((constructor))
3091static void __stat_init(void)
3092{
3093 cli_register_kw(&cli_kws);
3094}
3095
William Lallemand74c24fb2016-11-21 17:18:36 +01003096/*
3097 * Local variables:
3098 * c-indent-level: 8
3099 * c-basic-offset: 8
3100 * End:
3101 */