blob: 0ba6d27ba3318941ab78f455972c9c701e79f6fd [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 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +0100917 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
William Lallemand74c24fb2016-11-21 17:18:36 +0100918 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>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100932 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
933 "",
934 U2H(stats[ST_F_REQ_TOT].u.u64),
935 U2H(stats[ST_F_HRSP_1XX].u.u64),
936 U2H(stats[ST_F_HRSP_2XX].u.u64),
937 U2H(stats[ST_F_COMP_RSP].u.u64),
938 stats[ST_F_HRSP_2XX].u.u64 ?
939 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
940 U2H(stats[ST_F_HRSP_3XX].u.u64),
941 U2H(stats[ST_F_HRSP_4XX].u.u64),
942 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreauae9bea02016-11-25 14:44:52 +0100943 U2H(stats[ST_F_HRSP_OTHER].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100944 }
945
946 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
947 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
948 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
949 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
950 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
951
952 chunk_appendf(out,
953 "</table></div></u></td>"
954 /* sessions: lbtot, last */
955 "<td>%s</td><td>%s</td>"
956 /* bytes: in */
957 "<td>%s</td>"
958 "",
959 U2H(stats[ST_F_LBTOT].u.u64),
960 human_time(stats[ST_F_LASTSESS].u.s32, 1),
961 U2H(stats[ST_F_BIN].u.u64));
962
963 chunk_appendf(out,
964 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
965 "<td>%s%s<div class=tips><table class=det>"
966 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
967 "<tr><th>Compression in:</th><td>%s</td></tr>"
968 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
969 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
970 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
971 "</table></div>%s</td>",
972 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
973 U2H(stats[ST_F_BOUT].u.u64),
974 U2H(stats[ST_F_BOUT].u.u64),
975 U2H(stats[ST_F_COMP_IN].u.u64),
976 U2H(stats[ST_F_COMP_OUT].u.u64),
977 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
978 U2H(stats[ST_F_COMP_BYP].u.u64),
979 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
980 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,
981 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
982
983 chunk_appendf(out,
984 /* denied: req, resp */
985 "<td>%s</td><td>%s</td>"
986 /* errors : request, connect */
987 "<td></td><td>%s</td>"
988 /* errors : response */
989 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
990 /* warnings: retries, redispatches */
991 "<td>%lld</td><td>%lld</td>"
992 /* backend status: reflect backend status (up/down): we display UP
993 * if the backend has known working servers or if it has no server at
994 * all (eg: for stats). Then we display the total weight, number of
995 * active and backups. */
996 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
997 "<td class=ac>%d</td><td class=ac>%d</td>"
998 "",
999 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1000 U2H(stats[ST_F_ECON].u.u64),
1001 U2H(stats[ST_F_ERESP].u.u64),
1002 (long long)stats[ST_F_CLI_ABRT].u.u64,
1003 (long long)stats[ST_F_SRV_ABRT].u.u64,
1004 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1005 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1006 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1007 stats[ST_F_WEIGHT].u.u32,
1008 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1009
1010 chunk_appendf(out,
1011 /* rest of backend: nothing, down transitions, total downtime, throttle */
1012 "<td class=ac>&nbsp;</td><td>%d</td>"
1013 "<td>%s</td>"
1014 "<td></td>"
1015 "</tr>",
1016 stats[ST_F_CHKDOWN].u.u32,
1017 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1018 }
1019 return 1;
1020}
1021
1022int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1023{
1024 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1025 flags |= ST_SHOWADMIN;
1026
1027 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
1028 return stats_dump_fields_html(&trash, stats, flags);
1029 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
1030 return stats_dump_fields_typed(&trash, stats);
1031 else
1032 return stats_dump_fields_csv(&trash, stats);
1033}
1034
1035/* Fill <stats> with the frontend statistics. <stats> is
1036 * preallocated array of length <len>. The length of the array
1037 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1038 * this value, the function returns 0, otherwise, it returns 1.
1039 */
1040int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1041{
1042 if (len < ST_F_TOTAL_FIELDS)
1043 return 0;
1044
1045 memset(stats, 0, sizeof(*stats) * len);
1046
1047 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1048 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1049 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1050 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1051 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1052 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1053 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1054 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1055 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1056 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1057 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1058 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1059 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1060 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1061 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1062 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1063 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1064 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1065 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1066 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1067 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1068 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1069
1070 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1071 if (px->mode == PR_MODE_HTTP) {
1072 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1073 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1074 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1075 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1076 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1077 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1078 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
1079 }
1080
1081 /* requests : req_rate, req_rate_max, req_tot, */
1082 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1083 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1084 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1085
1086 /* compression: in, out, bypassed, responses */
1087 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1088 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1089 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1090 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1091
1092 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1093 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1094 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1095 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1096
1097 return 1;
1098}
1099
1100/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1101 * the state from stream interface <si>. The caller is responsible for clearing
1102 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1103 */
1104static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1105{
1106 struct appctx *appctx = __objt_appctx(si->end);
1107
1108 if (!(px->cap & PR_CAP_FE))
1109 return 0;
1110
1111 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1112 return 0;
1113
1114 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1115 return 0;
1116
1117 return stats_dump_one_line(stats, 0, px, appctx);
1118}
1119
1120/* Fill <stats> with the listener statistics. <stats> is
1121 * preallocated array of length <len>. The length of the array
1122 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1123 * then this value, the function returns 0, otherwise, it
1124 * returns 1. <flags> can take the value ST_SHLGNDS.
1125 */
1126int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1127 struct field *stats, int len)
1128{
1129 struct chunk *out = get_trash_chunk();
1130
1131 if (len < ST_F_TOTAL_FIELDS)
1132 return 0;
1133
1134 if (!l->counters)
1135 return 0;
1136
1137 chunk_reset(out);
1138 memset(stats, 0, sizeof(*stats) * len);
1139
1140 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1141 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1142 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1143 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1144 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1145 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1146 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1147 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1148 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1149 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1150 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1151 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1152 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1153 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1154 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
1155 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1156 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1157 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1158 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1159
1160 if (flags & ST_SHLGNDS) {
1161 char str[INET6_ADDRSTRLEN];
1162 int port;
1163
1164 port = get_host_port(&l->addr);
1165 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1166 case AF_INET:
1167 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1168 chunk_appendf(out, "%s:%d", str, port);
1169 break;
1170 case AF_INET6:
1171 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1172 chunk_appendf(out, "[%s]:%d", str, port);
1173 break;
1174 case AF_UNIX:
1175 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1176 break;
1177 case -1:
1178 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1179 chunk_strcat(out, strerror(errno));
1180 break;
1181 default: /* address family not supported */
1182 break;
1183 }
1184 }
1185
1186 return 1;
1187}
1188
1189/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1190 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1191 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1192 * otherwise.
1193 */
1194static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1195{
1196 struct appctx *appctx = __objt_appctx(si->end);
1197
1198 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1199 return 0;
1200
1201 return stats_dump_one_line(stats, flags, px, appctx);
1202}
1203
1204enum srv_stats_state {
1205 SRV_STATS_STATE_DOWN = 0,
1206 SRV_STATS_STATE_DOWN_AGENT,
1207 SRV_STATS_STATE_GOING_UP,
1208 SRV_STATS_STATE_UP_GOING_DOWN,
1209 SRV_STATS_STATE_UP,
1210 SRV_STATS_STATE_NOLB_GOING_DOWN,
1211 SRV_STATS_STATE_NOLB,
1212 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1213 SRV_STATS_STATE_DRAIN,
1214 SRV_STATS_STATE_DRAIN_AGENT,
1215 SRV_STATS_STATE_NO_CHECK,
1216
1217 SRV_STATS_STATE_COUNT, /* Must be last */
1218};
1219
1220static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1221 [SRV_STATS_STATE_DOWN] = "DOWN",
1222 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1223 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1224 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1225 [SRV_STATS_STATE_UP] = "UP",
1226 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1227 [SRV_STATS_STATE_NOLB] = "NOLB",
1228 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1229 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1230 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1231 [SRV_STATS_STATE_NO_CHECK] = "no check"
1232};
1233
1234/* Fill <stats> with the server statistics. <stats> is
1235 * preallocated array of length <len>. The length of the array
1236 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1237 * then this value, the function returns 0, otherwise, it
1238 * returns 1. <flags> can take the value ST_SHLGNDS.
1239 */
1240int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1241 struct field *stats, int len)
1242{
1243 struct server *via, *ref;
1244 char str[INET6_ADDRSTRLEN];
1245 struct chunk *out = get_trash_chunk();
1246 enum srv_stats_state state;
1247 char *fld_status;
1248
1249 if (len < ST_F_TOTAL_FIELDS)
1250 return 0;
1251
1252 memset(stats, 0, sizeof(*stats) * len);
1253
1254 /* we have "via" which is the tracked server as described in the configuration,
1255 * and "ref" which is the checked server and the end of the chain.
1256 */
1257 via = sv->track ? sv->track : sv;
1258 ref = via;
1259 while (ref->track)
1260 ref = ref->track;
1261
1262 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
1263 if ((ref->check.state & CHK_ST_ENABLED) &&
1264 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1265 state = SRV_STATS_STATE_UP_GOING_DOWN;
1266 } else {
1267 state = SRV_STATS_STATE_UP;
1268 }
1269
1270 if (sv->admin & SRV_ADMF_DRAIN) {
1271 if (ref->agent.state & CHK_ST_ENABLED)
1272 state = SRV_STATS_STATE_DRAIN_AGENT;
1273 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1274 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1275 else
1276 state = SRV_STATS_STATE_DRAIN;
1277 }
1278
1279 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1280 state = SRV_STATS_STATE_NO_CHECK;
1281 }
1282 }
1283 else if (sv->state == SRV_ST_STOPPING) {
1284 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1285 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1286 state = SRV_STATS_STATE_NOLB;
1287 } else {
1288 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1289 }
1290 }
1291 else { /* stopped */
1292 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1293 state = SRV_STATS_STATE_DOWN_AGENT;
1294 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1295 state = SRV_STATS_STATE_DOWN; /* DOWN */
1296 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1297 state = SRV_STATS_STATE_GOING_UP;
1298 } else {
1299 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1300 }
1301 }
1302
1303 chunk_reset(out);
1304
1305 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1306 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1307 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1308 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1309 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1310 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1311 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1312
1313 if (sv->maxconn)
1314 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1315
1316 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1317 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1318 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1319 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1320 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1321 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1322 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1323 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
1324
1325 /* status */
1326 fld_status = chunk_newstr(out);
1327 if (sv->admin & SRV_ADMF_RMAINT)
1328 chunk_appendf(out, "MAINT (resolution)");
1329 else if (sv->admin & SRV_ADMF_IMAINT)
1330 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
1331 else if (sv->admin & SRV_ADMF_MAINT)
1332 chunk_appendf(out, "MAINT");
1333 else
1334 chunk_appendf(out,
1335 srv_hlt_st[state],
1336 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1337 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
1338
1339 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1340 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
1341 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1342 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1343 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1344
1345 /* check failures: unique, fatal; last change, total downtime */
1346 if (sv->check.state & CHK_ST_ENABLED) {
1347 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1348 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1349 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1350 }
1351
1352 if (sv->maxqueue)
1353 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1354
1355 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1356 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1357 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1358
1359 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
1360 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1361
1362 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1363
1364 if (sv->track) {
1365 char *fld_track = chunk_newstr(out);
1366
1367 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1368 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1369 }
1370
1371 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1372 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1373 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1374
1375 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1376 const char *fld_chksts;
1377
1378 fld_chksts = chunk_newstr(out);
1379 chunk_strcat(out, "* "); // for check in progress
1380 chunk_strcat(out, get_check_status_info(sv->check.status));
1381 if (!(sv->check.state & CHK_ST_INPROGRESS))
1382 fld_chksts += 2; // skip "* "
1383 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1384
1385 if (sv->check.status >= HCHK_STATUS_L57DATA)
1386 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1387
1388 if (sv->check.status >= HCHK_STATUS_CHECKED)
1389 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1390
1391 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1392 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1393 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1394 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1395 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1396 }
1397
1398 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1399 const char *fld_chksts;
1400
1401 fld_chksts = chunk_newstr(out);
1402 chunk_strcat(out, "* "); // for check in progress
1403 chunk_strcat(out, get_check_status_info(sv->agent.status));
1404 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1405 fld_chksts += 2; // skip "* "
1406 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1407
1408 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1409 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1410
1411 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1412 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1413
1414 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1415 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1416 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1417 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1418 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1419 }
1420
1421 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1422 if (px->mode == PR_MODE_HTTP) {
1423 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1424 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1425 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1426 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1427 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1428 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1429 }
1430
1431 if (ref->observe)
1432 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1433
1434 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1435 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1436 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1437
1438 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1439 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1440 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1441 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1442
1443 if (flags & ST_SHLGNDS) {
1444 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1445 case AF_INET:
1446 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1447 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
1448 break;
1449 case AF_INET6:
1450 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1451 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
1452 break;
1453 case AF_UNIX:
1454 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1455 break;
1456 case -1:
1457 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1458 chunk_strcat(out, strerror(errno));
1459 break;
1460 default: /* address family not supported */
1461 break;
1462 }
1463
1464 if (sv->cookie)
1465 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1466 }
1467
1468 return 1;
1469}
1470
1471/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1472 * from stream interface <si>, stats flags <flags>, and server state <state>.
1473 * The caller is responsible for clearing the trash if needed. Returns non-zero
1474 * if it emits anything, zero otherwise.
1475 */
1476static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1477{
1478 struct appctx *appctx = __objt_appctx(si->end);
1479
1480 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1481 return 0;
1482
1483 return stats_dump_one_line(stats, flags, px, appctx);
1484}
1485
1486/* Fill <stats> with the backend statistics. <stats> is
1487 * preallocated array of length <len>. The length of the array
1488 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1489 * then this value, the function returns 0, otherwise, it
1490 * returns 1. <flags> can take the value ST_SHLGNDS.
1491 */
1492int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1493{
1494 if (len < ST_F_TOTAL_FIELDS)
1495 return 0;
1496
1497 memset(stats, 0, sizeof(*stats) * len);
1498
1499 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1500 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1501 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1502 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1503 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
1504 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
1505 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1506 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1507 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1508 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1509 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1510 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1511 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1512 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1513 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1514 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1515 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
1516 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1517 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1518 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1519 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1520 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1521 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1522 if (px->srv)
1523 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1524
1525 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1526 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1527 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1528 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1529 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1530 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1531 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1532
1533 if (flags & ST_SHLGNDS) {
1534 if (px->cookie_name)
1535 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1536 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1537 }
1538
1539 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1540 if (px->mode == PR_MODE_HTTP) {
1541 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1542 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1543 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1544 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1545 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1546 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1547 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001548 }
1549
1550 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1551 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1552
1553 /* compression: in, out, bypassed, responses */
1554 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1555 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1556 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1557 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1558 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1559
1560 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1561 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1562 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1563 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1564
1565 return 1;
1566}
1567
1568/* Dumps a line for backend <px> to the trash for and uses the state from stream
1569 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1570 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1571 */
1572static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1573{
1574 struct appctx *appctx = __objt_appctx(si->end);
1575
1576 if (!(px->cap & PR_CAP_BE))
1577 return 0;
1578
1579 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1580 return 0;
1581
1582 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1583 return 0;
1584
1585 return stats_dump_one_line(stats, flags, px, appctx);
1586}
1587
1588/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1589 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1590 * for clearing the trash if needed.
1591 */
1592static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1593{
1594 struct appctx *appctx = __objt_appctx(si->end);
1595 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1596
1597 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1598 /* A form to enable/disable this proxy servers */
1599
1600 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1601 scope_txt[0] = 0;
1602 if (appctx->ctx.stats.scope_len) {
1603 strcpy(scope_txt, STAT_SCOPE_PATTERN);
1604 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
1605 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1606 }
1607
1608 chunk_appendf(&trash,
1609 "<form method=\"post\">");
1610 }
1611
1612 /* print a new table */
1613 chunk_appendf(&trash,
1614 "<table class=\"tbl\" width=\"100%%\">\n"
1615 "<tr class=\"titre\">"
1616 "<th class=\"pxname\" width=\"10%%\">");
1617
1618 chunk_appendf(&trash,
1619 "<a name=\"%s\"></a>%s"
1620 "<a class=px href=\"#%s\">%s</a>",
1621 px->id,
1622 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1623 px->id, px->id);
1624
1625 if (uri->flags & ST_SHLGNDS) {
1626 /* cap, mode, id */
1627 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1628 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1629 px->uuid);
1630 chunk_appendf(&trash, "</div>");
1631 }
1632
1633 chunk_appendf(&trash,
1634 "%s</th>"
1635 "<th class=\"%s\" width=\"90%%\">%s</th>"
1636 "</tr>\n"
1637 "</table>\n"
1638 "<table class=\"tbl\" width=\"100%%\">\n"
1639 "<tr class=\"titre\">",
1640 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1641 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1642
1643 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1644 /* Column heading for Enable or Disable server */
1645 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
1646 }
1647
1648 chunk_appendf(&trash,
1649 "<th rowspan=2></th>"
1650 "<th colspan=3>Queue</th>"
1651 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1652 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1653 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1654 "<th colspan=9>Server</th>"
1655 "</tr>\n"
1656 "<tr class=\"titre\">"
1657 "<th>Cur</th><th>Max</th><th>Limit</th>"
1658 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1659 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1660 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1661 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1662 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
1663 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1664 "<th>Thrtle</th>\n"
1665 "</tr>");
1666}
1667
1668/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
1669 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1670 */
1671static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
1672{
1673 struct appctx *appctx = __objt_appctx(si->end);
1674 chunk_appendf(&trash, "</table>");
1675
1676 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1677 /* close the form used to enable/disable this proxy servers */
1678 chunk_appendf(&trash,
1679 "Choose the action to perform on the checked servers : "
1680 "<select name=action>"
1681 "<option value=\"\"></option>"
1682 "<option value=\"ready\">Set state to READY</option>"
1683 "<option value=\"drain\">Set state to DRAIN</option>"
1684 "<option value=\"maint\">Set state to MAINT</option>"
1685 "<option value=\"dhlth\">Health: disable checks</option>"
1686 "<option value=\"ehlth\">Health: enable checks</option>"
1687 "<option value=\"hrunn\">Health: force UP</option>"
1688 "<option value=\"hnolb\">Health: force NOLB</option>"
1689 "<option value=\"hdown\">Health: force DOWN</option>"
1690 "<option value=\"dagent\">Agent: disable checks</option>"
1691 "<option value=\"eagent\">Agent: enable checks</option>"
1692 "<option value=\"arunn\">Agent: force UP</option>"
1693 "<option value=\"adown\">Agent: force DOWN</option>"
1694 "<option value=\"shutdown\">Kill Sessions</option>"
1695 "</select>"
1696 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
1697 "&nbsp;<input type=\"submit\" value=\"Apply\">"
1698 "</form>",
1699 px->uuid);
1700 }
1701
1702 chunk_appendf(&trash, "<p>\n");
1703}
1704
1705/*
1706 * Dumps statistics for a proxy. The output is sent to the stream interface's
1707 * input buffer. Returns 0 if it had to stop dumping data because of lack of
1708 * buffer space, or non-zero if everything completed. This function is used
1709 * both by the CLI and the HTTP entry points, and is able to dump the output
1710 * in HTML or CSV formats. If the later, <uri> must be NULL.
1711 */
1712int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1713{
1714 struct appctx *appctx = __objt_appctx(si->end);
1715 struct stream *s = si_strm(si);
1716 struct channel *rep = si_ic(si);
1717 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
1718 struct listener *l;
1719 unsigned int flags;
1720
1721 if (uri)
1722 flags = uri->flags;
1723 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
1724 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
1725 else
1726 flags = ST_SHNODE | ST_SHDESC;
1727
1728 chunk_reset(&trash);
1729
1730 switch (appctx->ctx.stats.px_st) {
1731 case STAT_PX_ST_INIT:
1732 /* we are on a new proxy */
1733 if (uri && uri->scope) {
1734 /* we have a limited scope, we have to check the proxy name */
1735 struct stat_scope *scope;
1736 int len;
1737
1738 len = strlen(px->id);
1739 scope = uri->scope;
1740
1741 while (scope) {
1742 /* match exact proxy name */
1743 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1744 break;
1745
1746 /* match '.' which means 'self' proxy */
1747 if (!strcmp(scope->px_id, ".") && px == s->be)
1748 break;
1749 scope = scope->next;
1750 }
1751
1752 /* proxy name not found : don't dump anything */
1753 if (scope == NULL)
1754 return 1;
1755 }
1756
1757 /* if the user has requested a limited output and the proxy
1758 * name does not match, skip it.
1759 */
1760 if (appctx->ctx.stats.scope_len &&
1761 strnistr(px->id, strlen(px->id), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
1762 return 1;
1763
1764 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
1765 (appctx->ctx.stats.iid != -1) &&
1766 (px->uuid != appctx->ctx.stats.iid))
1767 return 1;
1768
1769 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
1770 /* fall through */
1771
1772 case STAT_PX_ST_TH:
1773 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
1774 stats_dump_html_px_hdr(si, px, uri);
1775 if (bi_putchk(rep, &trash) == -1) {
1776 si_applet_cant_put(si);
1777 return 0;
1778 }
1779 }
1780
1781 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
1782 /* fall through */
1783
1784 case STAT_PX_ST_FE:
1785 /* print the frontend */
1786 if (stats_dump_fe_stats(si, px)) {
1787 if (bi_putchk(rep, &trash) == -1) {
1788 si_applet_cant_put(si);
1789 return 0;
1790 }
1791 }
1792
1793 appctx->ctx.stats.l = px->conf.listeners.n;
1794 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
1795 /* fall through */
1796
1797 case STAT_PX_ST_LI:
1798 /* stats.l has been initialized above */
1799 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
1800 if (buffer_almost_full(rep->buf)) {
1801 si_applet_cant_put(si);
1802 return 0;
1803 }
1804
1805 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
1806 if (!l->counters)
1807 continue;
1808
1809 if (appctx->ctx.stats.flags & STAT_BOUND) {
1810 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
1811 break;
1812
1813 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
1814 continue;
1815 }
1816
1817 /* print the frontend */
1818 if (stats_dump_li_stats(si, px, l, flags)) {
1819 if (bi_putchk(rep, &trash) == -1) {
1820 si_applet_cant_put(si);
1821 return 0;
1822 }
1823 }
1824 }
1825
1826 appctx->ctx.stats.sv = px->srv; /* may be NULL */
1827 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
1828 /* fall through */
1829
1830 case STAT_PX_ST_SV:
1831 /* stats.sv has been initialized above */
1832 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
1833 if (buffer_almost_full(rep->buf)) {
1834 si_applet_cant_put(si);
1835 return 0;
1836 }
1837
1838 sv = appctx->ctx.stats.sv;
1839
1840 if (appctx->ctx.stats.flags & STAT_BOUND) {
1841 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
1842 break;
1843
1844 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
1845 continue;
1846 }
1847
1848 svs = sv;
1849 while (svs->track)
1850 svs = svs->track;
1851
1852 /* do not report servers which are DOWN and not changing state */
1853 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
1854 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
1855 (sv->state == SRV_ST_STOPPED && /* server is down */
1856 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
1857 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
1858 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
1859 continue;
1860 }
1861
1862 if (stats_dump_sv_stats(si, px, flags, sv)) {
1863 if (bi_putchk(rep, &trash) == -1) {
1864 si_applet_cant_put(si);
1865 return 0;
1866 }
1867 }
1868 } /* for sv */
1869
1870 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
1871 /* fall through */
1872
1873 case STAT_PX_ST_BE:
1874 /* print the backend */
1875 if (stats_dump_be_stats(si, px, flags)) {
1876 if (bi_putchk(rep, &trash) == -1) {
1877 si_applet_cant_put(si);
1878 return 0;
1879 }
1880 }
1881
1882 appctx->ctx.stats.px_st = STAT_PX_ST_END;
1883 /* fall through */
1884
1885 case STAT_PX_ST_END:
1886 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
1887 stats_dump_html_px_end(si, px);
1888 if (bi_putchk(rep, &trash) == -1) {
1889 si_applet_cant_put(si);
1890 return 0;
1891 }
1892 }
1893
1894 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
1895 /* fall through */
1896
1897 case STAT_PX_ST_FIN:
1898 return 1;
1899
1900 default:
1901 /* unknown state, we should put an abort() here ! */
1902 return 1;
1903 }
1904}
1905
1906/* Dumps the HTTP stats head block to the trash for and uses the per-uri
1907 * parameters <uri>. The caller is responsible for clearing the trash if needed.
1908 */
1909static void stats_dump_html_head(struct uri_auth *uri)
1910{
1911 /* WARNING! This must fit in the first buffer !!! */
1912 chunk_appendf(&trash,
1913 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1914 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
1915 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
1916 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1917 "<style type=\"text/css\"><!--\n"
1918 "body {"
1919 " font-family: arial, helvetica, sans-serif;"
1920 " font-size: 12px;"
1921 " font-weight: normal;"
1922 " color: black;"
1923 " background: white;"
1924 "}\n"
1925 "th,td {"
1926 " font-size: 10px;"
1927 "}\n"
1928 "h1 {"
1929 " font-size: x-large;"
1930 " margin-bottom: 0.5em;"
1931 "}\n"
1932 "h2 {"
1933 " font-family: helvetica, arial;"
1934 " font-size: x-large;"
1935 " font-weight: bold;"
1936 " font-style: italic;"
1937 " color: #6020a0;"
1938 " margin-top: 0em;"
1939 " margin-bottom: 0em;"
1940 "}\n"
1941 "h3 {"
1942 " font-family: helvetica, arial;"
1943 " font-size: 16px;"
1944 " font-weight: bold;"
1945 " color: #b00040;"
1946 " background: #e8e8d0;"
1947 " margin-top: 0em;"
1948 " margin-bottom: 0em;"
1949 "}\n"
1950 "li {"
1951 " margin-top: 0.25em;"
1952 " margin-right: 2em;"
1953 "}\n"
1954 ".hr {margin-top: 0.25em;"
1955 " border-color: black;"
1956 " border-bottom-style: solid;"
1957 "}\n"
1958 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
1959 ".total {background: #20D0D0;color: #ffff80;}\n"
1960 ".frontend {background: #e8e8d0;}\n"
1961 ".socket {background: #d0d0d0;}\n"
1962 ".backend {background: #e8e8d0;}\n"
1963 ".active_down {background: #ff9090;}\n"
1964 ".active_going_up {background: #ffd020;}\n"
1965 ".active_going_down {background: #ffffa0;}\n"
1966 ".active_up {background: #c0ffc0;}\n"
1967 ".active_nolb {background: #20a0ff;}\n"
1968 ".active_draining {background: #20a0FF;}\n"
1969 ".active_no_check {background: #e0e0e0;}\n"
1970 ".backup_down {background: #ff9090;}\n"
1971 ".backup_going_up {background: #ff80ff;}\n"
1972 ".backup_going_down {background: #c060ff;}\n"
1973 ".backup_up {background: #b0d0ff;}\n"
1974 ".backup_nolb {background: #90b0e0;}\n"
1975 ".backup_draining {background: #cc9900;}\n"
1976 ".backup_no_check {background: #e0e0e0;}\n"
1977 ".maintain {background: #c07820;}\n"
1978 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
1979 "\n"
1980 "a.px:link {color: #ffff40; text-decoration: none;}"
1981 "a.px:visited {color: #ffff40; text-decoration: none;}"
1982 "a.px:hover {color: #ffffff; text-decoration: none;}"
1983 "a.lfsb:link {color: #000000; text-decoration: none;}"
1984 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1985 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1986 "\n"
1987 "table.tbl { border-collapse: collapse; border-style: none;}\n"
1988 "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"
1989 "table.tbl td.ac { text-align: center;}\n"
1990 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
1991 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
1992 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
1993 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
1994 "\n"
1995 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1996 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1997 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
1998 "table.det { border-collapse: collapse; border-style: none; }\n"
1999 "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"
2000 "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"
2001 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2002 "div.tips {\n"
2003 " display:block;\n"
2004 " visibility:hidden;\n"
2005 " z-index:2147483647;\n"
2006 " position:absolute;\n"
2007 " padding:2px 4px 3px;\n"
2008 " background:#f0f060; color:#000000;\n"
2009 " border:1px solid #7040c0;\n"
2010 " white-space:nowrap;\n"
2011 " font-style:normal;font-size:11px;font-weight:normal;\n"
2012 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2013 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2014 "}\n"
2015 "u:hover div.tips {visibility:visible;}\n"
2016 "-->\n"
2017 "</style></head>\n",
2018 (uri->flags & ST_SHNODE) ? " on " : "",
2019 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2020 );
2021}
2022
2023/* Dumps the HTML stats information block to the trash for and uses the state from
2024 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2025 * for clearing the trash if needed.
2026 */
2027static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2028{
2029 struct appctx *appctx = __objt_appctx(si->end);
2030 unsigned int up = (now.tv_sec - start_date.tv_sec);
2031 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2032
2033 /* WARNING! this has to fit the first packet too.
2034 * We are around 3.5 kB, add adding entries will
2035 * become tricky if we want to support 4kB buffers !
2036 */
2037 chunk_appendf(&trash,
2038 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2039 PRODUCT_NAME "%s</a></h1>\n"
2040 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2041 "<hr width=\"100%%\" class=\"hr\">\n"
2042 "<h3>&gt; General process information</h3>\n"
2043 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
2044 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
2045 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2046 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2047 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2048 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2049 "Running tasks: %d/%d; idle = %d %%<br>\n"
2050 "</td><td align=\"center\" nowrap>\n"
2051 "<table class=\"lgd\"><tr>\n"
2052 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2053 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2054 "</tr><tr>\n"
2055 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2056 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2057 "</tr><tr>\n"
2058 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2059 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2060 "</tr><tr>\n"
2061 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2062 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2063 "</tr><tr>\n"
2064 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2065 "</tr><tr>\n"
2066 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2067 "</tr></table>\n"
2068 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2069 "</td>"
2070 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2071 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2072 "",
2073 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
2074 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2075 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2076 (uri->flags & ST_SHDESC) ? ": " : "",
2077 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2078 pid, relative_pid, global.nbproc,
2079 up / 86400, (up % 86400) / 3600,
2080 (up % 3600) / 60, (up % 60),
2081 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2082 global.rlimit_memmax ? " MB" : "",
2083 global.rlimit_nofile,
2084 global.maxsock, global.maxconn, global.maxpipes,
2085 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
2086 run_queue_cur, nb_tasks_cur, idle_pct
2087 );
2088
2089 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2090 memcpy(scope_txt, bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2091 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2092
2093 chunk_appendf(&trash,
2094 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2095 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2096 STAT_SCOPE_TXT_MAXLEN);
2097
2098 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2099 scope_txt[0] = 0;
2100 if (appctx->ctx.stats.scope_len) {
2101 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2102 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2103 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2104 }
2105
2106 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2107 chunk_appendf(&trash,
2108 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2109 uri->uri_prefix,
2110 "",
2111 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2112 scope_txt);
2113 else
2114 chunk_appendf(&trash,
2115 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2116 uri->uri_prefix,
2117 ";up",
2118 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2119 scope_txt);
2120
2121 if (uri->refresh > 0) {
2122 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2123 chunk_appendf(&trash,
2124 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2125 uri->uri_prefix,
2126 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2127 "",
2128 scope_txt);
2129 else
2130 chunk_appendf(&trash,
2131 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2132 uri->uri_prefix,
2133 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2134 ";norefresh",
2135 scope_txt);
2136 }
2137
2138 chunk_appendf(&trash,
2139 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2140 uri->uri_prefix,
2141 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2142 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2143 scope_txt);
2144
2145 chunk_appendf(&trash,
2146 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2147 uri->uri_prefix,
2148 (uri->refresh > 0) ? ";norefresh" : "",
2149 scope_txt);
2150
2151 chunk_appendf(&trash,
2152 "</ul></td>"
2153 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2154 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2155 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2156 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2157 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2158 "</ul>"
2159 "</td>"
2160 "</tr></table>\n"
2161 ""
2162 );
2163
2164 if (appctx->ctx.stats.st_code) {
2165 switch (appctx->ctx.stats.st_code) {
2166 case STAT_STATUS_DONE:
2167 chunk_appendf(&trash,
2168 "<p><div class=active_up>"
2169 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2170 "Action processed successfully."
2171 "</div>\n", uri->uri_prefix,
2172 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2173 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2174 scope_txt);
2175 break;
2176 case STAT_STATUS_NONE:
2177 chunk_appendf(&trash,
2178 "<p><div class=active_going_down>"
2179 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2180 "Nothing has changed."
2181 "</div>\n", uri->uri_prefix,
2182 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2183 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2184 scope_txt);
2185 break;
2186 case STAT_STATUS_PART:
2187 chunk_appendf(&trash,
2188 "<p><div class=active_going_down>"
2189 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2190 "Action partially processed.<br>"
2191 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2192 "</div>\n", uri->uri_prefix,
2193 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2194 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2195 scope_txt);
2196 break;
2197 case STAT_STATUS_ERRP:
2198 chunk_appendf(&trash,
2199 "<p><div class=active_down>"
2200 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2201 "Action not processed because of invalid parameters."
2202 "<ul>"
2203 "<li>The action is maybe unknown.</li>"
2204 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2205 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2206 "</ul>"
2207 "</div>\n", uri->uri_prefix,
2208 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2209 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2210 scope_txt);
2211 break;
2212 case STAT_STATUS_EXCD:
2213 chunk_appendf(&trash,
2214 "<p><div class=active_down>"
2215 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2216 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2217 "You should retry with less servers at a time.</b>"
2218 "</div>\n", uri->uri_prefix,
2219 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2220 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2221 scope_txt);
2222 break;
2223 case STAT_STATUS_DENY:
2224 chunk_appendf(&trash,
2225 "<p><div class=active_down>"
2226 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2227 "<b>Action denied.</b>"
2228 "</div>\n", uri->uri_prefix,
2229 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2230 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2231 scope_txt);
2232 break;
2233 default:
2234 chunk_appendf(&trash,
2235 "<p><div class=active_no_check>"
2236 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2237 "Unexpected result."
2238 "</div>\n", uri->uri_prefix,
2239 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2240 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2241 scope_txt);
2242 }
2243 chunk_appendf(&trash, "<p>\n");
2244 }
2245}
2246
2247/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2248 * for clearing the trash if needed.
2249 */
2250static void stats_dump_html_end()
2251{
2252 chunk_appendf(&trash, "</body></html>\n");
2253}
2254
2255/* This function dumps statistics onto the stream interface's read buffer in
2256 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2257 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2258 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2259 * and the stream must be closed, or -1 in case of any error. This function is
2260 * used by both the CLI and the HTTP handlers.
2261 */
Willy Tarreau2b812e22016-11-22 16:18:05 +01002262static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002263{
2264 struct appctx *appctx = __objt_appctx(si->end);
2265 struct channel *rep = si_ic(si);
2266 struct proxy *px;
2267
2268 chunk_reset(&trash);
2269
2270 switch (appctx->st2) {
2271 case STAT_ST_INIT:
2272 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2273 /* fall through */
2274
2275 case STAT_ST_HEAD:
2276 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2277 stats_dump_html_head(uri);
2278 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2279 stats_dump_csv_header();
2280
2281 if (bi_putchk(rep, &trash) == -1) {
2282 si_applet_cant_put(si);
2283 return 0;
2284 }
2285
2286 appctx->st2 = STAT_ST_INFO;
2287 /* fall through */
2288
2289 case STAT_ST_INFO:
2290 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2291 stats_dump_html_info(si, uri);
2292 if (bi_putchk(rep, &trash) == -1) {
2293 si_applet_cant_put(si);
2294 return 0;
2295 }
2296 }
2297
2298 appctx->ctx.stats.px = proxy;
2299 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2300 appctx->st2 = STAT_ST_LIST;
2301 /* fall through */
2302
2303 case STAT_ST_LIST:
2304 /* dump proxies */
2305 while (appctx->ctx.stats.px) {
2306 if (buffer_almost_full(rep->buf)) {
2307 si_applet_cant_put(si);
2308 return 0;
2309 }
2310
2311 px = appctx->ctx.stats.px;
2312 /* skip the disabled proxies, global frontend and non-networked ones */
2313 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
2314 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
2315 return 0;
2316
2317 appctx->ctx.stats.px = px->next;
2318 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2319 }
2320 /* here, we just have reached the last proxy */
2321
2322 appctx->st2 = STAT_ST_END;
2323 /* fall through */
2324
2325 case STAT_ST_END:
2326 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2327 stats_dump_html_end();
2328 if (bi_putchk(rep, &trash) == -1) {
2329 si_applet_cant_put(si);
2330 return 0;
2331 }
2332 }
2333
2334 appctx->st2 = STAT_ST_FIN;
2335 /* fall through */
2336
2337 case STAT_ST_FIN:
2338 return 1;
2339
2340 default:
2341 /* unknown state ! */
2342 appctx->st2 = STAT_ST_FIN;
2343 return -1;
2344 }
2345}
2346
2347/* We reached the stats page through a POST request. The appctx is
2348 * expected to have already been allocated by the caller.
2349 * Parse the posted data and enable/disable servers if necessary.
2350 * Returns 1 if request was parsed or zero if it needs more data.
2351 */
2352static int stats_process_http_post(struct stream_interface *si)
2353{
2354 struct stream *s = si_strm(si);
2355 struct appctx *appctx = objt_appctx(si->end);
2356
2357 struct proxy *px = NULL;
2358 struct server *sv = NULL;
2359
2360 char key[LINESIZE];
2361 int action = ST_ADM_ACTION_NONE;
2362 int reprocess = 0;
2363
2364 int total_servers = 0;
2365 int altered_servers = 0;
2366
2367 char *first_param, *cur_param, *next_param, *end_params;
2368 char *st_cur_param = NULL;
2369 char *st_next_param = NULL;
2370
2371 struct chunk *temp;
2372 int reql;
2373
2374 temp = get_trash_chunk();
2375 if (temp->size < s->txn->req.body_len) {
2376 /* too large request */
2377 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2378 goto out;
2379 }
2380
2381 reql = bo_getblk(si_oc(si), temp->str, s->txn->req.body_len, s->txn->req.eoh + 2);
2382 if (reql <= 0) {
2383 /* we need more data */
2384 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2385 return 0;
2386 }
2387
2388 first_param = temp->str;
2389 end_params = temp->str + reql;
2390 cur_param = next_param = end_params;
2391 *end_params = '\0';
2392
2393 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2394
2395 /*
2396 * Parse the parameters in reverse order to only store the last value.
2397 * From the html form, the backend and the action are at the end.
2398 */
2399 while (cur_param > first_param) {
2400 char *value;
2401 int poffset, plen;
2402
2403 cur_param--;
2404
2405 if ((*cur_param == '&') || (cur_param == first_param)) {
2406 reprocess_servers:
2407 /* Parse the key */
2408 poffset = (cur_param != first_param ? 1 : 0);
2409 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2410 if ((plen > 0) && (plen <= sizeof(key))) {
2411 strncpy(key, cur_param + poffset, plen);
2412 key[plen - 1] = '\0';
2413 } else {
2414 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2415 goto out;
2416 }
2417
2418 /* Parse the value */
2419 value = key;
2420 while (*value != '\0' && *value != '=') {
2421 value++;
2422 }
2423 if (*value == '=') {
2424 /* Ok, a value is found, we can mark the end of the key */
2425 *value++ = '\0';
2426 }
2427 if (url_decode(key) < 0 || url_decode(value) < 0)
2428 break;
2429
2430 /* Now we can check the key to see what to do */
2431 if (!px && (strcmp(key, "b") == 0)) {
2432 if ((px = proxy_be_by_name(value)) == NULL) {
2433 /* the backend name is unknown or ambiguous (duplicate names) */
2434 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2435 goto out;
2436 }
2437 }
2438 else if (!action && (strcmp(key, "action") == 0)) {
2439 if (strcmp(value, "ready") == 0) {
2440 action = ST_ADM_ACTION_READY;
2441 }
2442 else if (strcmp(value, "drain") == 0) {
2443 action = ST_ADM_ACTION_DRAIN;
2444 }
2445 else if (strcmp(value, "maint") == 0) {
2446 action = ST_ADM_ACTION_MAINT;
2447 }
2448 else if (strcmp(value, "shutdown") == 0) {
2449 action = ST_ADM_ACTION_SHUTDOWN;
2450 }
2451 else if (strcmp(value, "dhlth") == 0) {
2452 action = ST_ADM_ACTION_DHLTH;
2453 }
2454 else if (strcmp(value, "ehlth") == 0) {
2455 action = ST_ADM_ACTION_EHLTH;
2456 }
2457 else if (strcmp(value, "hrunn") == 0) {
2458 action = ST_ADM_ACTION_HRUNN;
2459 }
2460 else if (strcmp(value, "hnolb") == 0) {
2461 action = ST_ADM_ACTION_HNOLB;
2462 }
2463 else if (strcmp(value, "hdown") == 0) {
2464 action = ST_ADM_ACTION_HDOWN;
2465 }
2466 else if (strcmp(value, "dagent") == 0) {
2467 action = ST_ADM_ACTION_DAGENT;
2468 }
2469 else if (strcmp(value, "eagent") == 0) {
2470 action = ST_ADM_ACTION_EAGENT;
2471 }
2472 else if (strcmp(value, "arunn") == 0) {
2473 action = ST_ADM_ACTION_ARUNN;
2474 }
2475 else if (strcmp(value, "adown") == 0) {
2476 action = ST_ADM_ACTION_ADOWN;
2477 }
2478 /* else these are the old supported methods */
2479 else if (strcmp(value, "disable") == 0) {
2480 action = ST_ADM_ACTION_DISABLE;
2481 }
2482 else if (strcmp(value, "enable") == 0) {
2483 action = ST_ADM_ACTION_ENABLE;
2484 }
2485 else if (strcmp(value, "stop") == 0) {
2486 action = ST_ADM_ACTION_STOP;
2487 }
2488 else if (strcmp(value, "start") == 0) {
2489 action = ST_ADM_ACTION_START;
2490 }
2491 else {
2492 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2493 goto out;
2494 }
2495 }
2496 else if (strcmp(key, "s") == 0) {
2497 if (!(px && action)) {
2498 /*
2499 * Indicates that we'll need to reprocess the parameters
2500 * as soon as backend and action are known
2501 */
2502 if (!reprocess) {
2503 st_cur_param = cur_param;
2504 st_next_param = next_param;
2505 }
2506 reprocess = 1;
2507 }
2508 else if ((sv = findserver(px, value)) != NULL) {
2509 switch (action) {
2510 case ST_ADM_ACTION_DISABLE:
2511 if (!(sv->admin & SRV_ADMF_FMAINT)) {
2512 altered_servers++;
2513 total_servers++;
2514 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2515 }
2516 break;
2517 case ST_ADM_ACTION_ENABLE:
2518 if (sv->admin & SRV_ADMF_FMAINT) {
2519 altered_servers++;
2520 total_servers++;
2521 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2522 }
2523 break;
2524 case ST_ADM_ACTION_STOP:
2525 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
2526 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2527 altered_servers++;
2528 total_servers++;
2529 }
2530 break;
2531 case ST_ADM_ACTION_START:
2532 if (sv->admin & SRV_ADMF_FDRAIN) {
2533 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2534 altered_servers++;
2535 total_servers++;
2536 }
2537 break;
2538 case ST_ADM_ACTION_DHLTH:
2539 if (sv->check.state & CHK_ST_CONFIGURED) {
2540 sv->check.state &= ~CHK_ST_ENABLED;
2541 altered_servers++;
2542 total_servers++;
2543 }
2544 break;
2545 case ST_ADM_ACTION_EHLTH:
2546 if (sv->check.state & CHK_ST_CONFIGURED) {
2547 sv->check.state |= CHK_ST_ENABLED;
2548 altered_servers++;
2549 total_servers++;
2550 }
2551 break;
2552 case ST_ADM_ACTION_HRUNN:
2553 if (!(sv->track)) {
2554 sv->check.health = sv->check.rise + sv->check.fall - 1;
2555 srv_set_running(sv, "changed from Web interface");
2556 altered_servers++;
2557 total_servers++;
2558 }
2559 break;
2560 case ST_ADM_ACTION_HNOLB:
2561 if (!(sv->track)) {
2562 sv->check.health = sv->check.rise + sv->check.fall - 1;
2563 srv_set_stopping(sv, "changed from Web interface");
2564 altered_servers++;
2565 total_servers++;
2566 }
2567 break;
2568 case ST_ADM_ACTION_HDOWN:
2569 if (!(sv->track)) {
2570 sv->check.health = 0;
2571 srv_set_stopped(sv, "changed from Web interface");
2572 altered_servers++;
2573 total_servers++;
2574 }
2575 break;
2576 case ST_ADM_ACTION_DAGENT:
2577 if (sv->agent.state & CHK_ST_CONFIGURED) {
2578 sv->agent.state &= ~CHK_ST_ENABLED;
2579 altered_servers++;
2580 total_servers++;
2581 }
2582 break;
2583 case ST_ADM_ACTION_EAGENT:
2584 if (sv->agent.state & CHK_ST_CONFIGURED) {
2585 sv->agent.state |= CHK_ST_ENABLED;
2586 altered_servers++;
2587 total_servers++;
2588 }
2589 break;
2590 case ST_ADM_ACTION_ARUNN:
2591 if (sv->agent.state & CHK_ST_ENABLED) {
2592 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
2593 srv_set_running(sv, "changed from Web interface");
2594 altered_servers++;
2595 total_servers++;
2596 }
2597 break;
2598 case ST_ADM_ACTION_ADOWN:
2599 if (sv->agent.state & CHK_ST_ENABLED) {
2600 sv->agent.health = 0;
2601 srv_set_stopped(sv, "changed from Web interface");
2602 altered_servers++;
2603 total_servers++;
2604 }
2605 break;
2606 case ST_ADM_ACTION_READY:
2607 srv_adm_set_ready(sv);
2608 altered_servers++;
2609 total_servers++;
2610 break;
2611 case ST_ADM_ACTION_DRAIN:
2612 srv_adm_set_drain(sv);
2613 altered_servers++;
2614 total_servers++;
2615 break;
2616 case ST_ADM_ACTION_MAINT:
2617 srv_adm_set_maint(sv);
2618 altered_servers++;
2619 total_servers++;
2620 break;
2621 case ST_ADM_ACTION_SHUTDOWN:
2622 if (px->state != PR_STSTOPPED) {
2623 struct stream *sess, *sess_bck;
2624
2625 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2626 if (sess->srv_conn == sv)
2627 stream_shutdown(sess, SF_ERR_KILLED);
2628
2629 altered_servers++;
2630 total_servers++;
2631 }
2632 break;
2633 }
2634 } else {
2635 /* the server name is unknown or ambiguous (duplicate names) */
2636 total_servers++;
2637 }
2638 }
2639 if (reprocess && px && action) {
2640 /* Now, we know the backend and the action chosen by the user.
2641 * We can safely restart from the first server parameter
2642 * to reprocess them
2643 */
2644 cur_param = st_cur_param;
2645 next_param = st_next_param;
2646 reprocess = 0;
2647 goto reprocess_servers;
2648 }
2649
2650 next_param = cur_param;
2651 }
2652 }
2653
2654 if (total_servers == 0) {
2655 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2656 }
2657 else if (altered_servers == 0) {
2658 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2659 }
2660 else if (altered_servers == total_servers) {
2661 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
2662 }
2663 else {
2664 appctx->ctx.stats.st_code = STAT_STATUS_PART;
2665 }
2666 out:
2667 return 1;
2668}
2669
2670
2671static int stats_send_http_headers(struct stream_interface *si)
2672{
2673 struct stream *s = si_strm(si);
2674 struct uri_auth *uri = s->be->uri_auth;
2675 struct appctx *appctx = objt_appctx(si->end);
2676
2677 chunk_printf(&trash,
2678 "HTTP/1.1 200 OK\r\n"
2679 "Cache-Control: no-cache\r\n"
2680 "Connection: close\r\n"
2681 "Content-Type: %s\r\n",
2682 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
2683
2684 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
2685 chunk_appendf(&trash, "Refresh: %d\r\n",
2686 uri->refresh);
2687
2688 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
2689
2690 if (appctx->ctx.stats.flags & STAT_CHUNKED)
2691 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
2692 else
2693 chunk_appendf(&trash, "\r\n");
2694
2695 s->txn->status = 200;
2696 s->logs.tv_request = now;
2697
2698 if (bi_putchk(si_ic(si), &trash) == -1) {
2699 si_applet_cant_put(si);
2700 return 0;
2701 }
2702
2703 return 1;
2704}
2705
2706static int stats_send_http_redirect(struct stream_interface *si)
2707{
2708 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2709 struct stream *s = si_strm(si);
2710 struct uri_auth *uri = s->be->uri_auth;
2711 struct appctx *appctx = objt_appctx(si->end);
2712
2713 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2714 scope_txt[0] = 0;
2715 if (appctx->ctx.stats.scope_len) {
2716 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2717 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2718 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2719 }
2720
2721 /* We don't want to land on the posted stats page because a refresh will
2722 * repost the data. We don't want this to happen on accident so we redirect
2723 * the browse to the stats page with a GET.
2724 */
2725 chunk_printf(&trash,
2726 "HTTP/1.1 303 See Other\r\n"
2727 "Cache-Control: no-cache\r\n"
2728 "Content-Type: text/plain\r\n"
2729 "Connection: close\r\n"
2730 "Location: %s;st=%s%s%s%s\r\n"
2731 "Content-length: 0\r\n"
2732 "\r\n",
2733 uri->uri_prefix,
2734 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
2735 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
2736 stat_status_codes[appctx->ctx.stats.st_code]) ?
2737 stat_status_codes[appctx->ctx.stats.st_code] :
2738 stat_status_codes[STAT_STATUS_UNKN],
2739 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2740 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2741 scope_txt);
2742
2743 s->txn->status = 303;
2744 s->logs.tv_request = now;
2745
2746 if (bi_putchk(si_ic(si), &trash) == -1) {
2747 si_applet_cant_put(si);
2748 return 0;
2749 }
2750
2751 return 1;
2752}
2753
2754/* This I/O handler runs as an applet embedded in a stream interface. It is
2755 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
2756 * appctx->st0 contains the operation in progress (dump, done). The handler
2757 * automatically unregisters itself once transfer is complete.
2758 */
2759static void http_stats_io_handler(struct appctx *appctx)
2760{
2761 struct stream_interface *si = appctx->owner;
2762 struct stream *s = si_strm(si);
2763 struct channel *req = si_oc(si);
2764 struct channel *res = si_ic(si);
2765
2766 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2767 goto out;
2768
2769 /* check that the output is not closed */
2770 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
2771 appctx->st0 = STAT_HTTP_DONE;
2772
2773 /* all states are processed in sequence */
2774 if (appctx->st0 == STAT_HTTP_HEAD) {
2775 if (stats_send_http_headers(si)) {
2776 if (s->txn->meth == HTTP_METH_HEAD)
2777 appctx->st0 = STAT_HTTP_DONE;
2778 else
2779 appctx->st0 = STAT_HTTP_DUMP;
2780 }
2781 }
2782
2783 if (appctx->st0 == STAT_HTTP_DUMP) {
2784 unsigned int prev_len = si_ib(si)->i;
2785 unsigned int data_len;
2786 unsigned int last_len;
2787 unsigned int last_fwd = 0;
2788
2789 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
2790 /* One difficulty we're facing is that we must prevent
2791 * the input data from being automatically forwarded to
2792 * the output area. For this, we temporarily disable
2793 * forwarding on the channel.
2794 */
2795 last_fwd = si_ic(si)->to_forward;
2796 si_ic(si)->to_forward = 0;
2797 chunk_printf(&trash, "\r\n000000\r\n");
2798 if (bi_putchk(si_ic(si), &trash) == -1) {
2799 si_applet_cant_put(si);
2800 si_ic(si)->to_forward = last_fwd;
2801 goto out;
2802 }
2803 }
2804
2805 data_len = si_ib(si)->i;
2806 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
2807 appctx->st0 = STAT_HTTP_DONE;
2808
2809 last_len = si_ib(si)->i;
2810
2811 /* Now we must either adjust or remove the chunk size. This is
2812 * not easy because the chunk size might wrap at the end of the
2813 * buffer, so we pretend we have nothing in the buffer, we write
2814 * the size, then restore the buffer's contents. Note that we can
2815 * only do that because no forwarding is scheduled on the stats
2816 * applet.
2817 */
2818 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
2819 si_ic(si)->total -= (last_len - prev_len);
2820 si_ib(si)->i -= (last_len - prev_len);
2821
2822 if (last_len != data_len) {
2823 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
2824 if (bi_putchk(si_ic(si), &trash) == -1)
2825 si_applet_cant_put(si);
2826
2827 si_ic(si)->total += (last_len - data_len);
2828 si_ib(si)->i += (last_len - data_len);
2829 }
2830 /* now re-enable forwarding */
2831 channel_forward(si_ic(si), last_fwd);
2832 }
2833 }
2834
2835 if (appctx->st0 == STAT_HTTP_POST) {
2836 if (stats_process_http_post(si))
2837 appctx->st0 = STAT_HTTP_LAST;
2838 else if (si_oc(si)->flags & CF_SHUTR)
2839 appctx->st0 = STAT_HTTP_DONE;
2840 }
2841
2842 if (appctx->st0 == STAT_HTTP_LAST) {
2843 if (stats_send_http_redirect(si))
2844 appctx->st0 = STAT_HTTP_DONE;
2845 }
2846
2847 if (appctx->st0 == STAT_HTTP_DONE) {
2848 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
2849 chunk_printf(&trash, "\r\n0\r\n\r\n");
2850 if (bi_putchk(si_ic(si), &trash) == -1) {
2851 si_applet_cant_put(si);
2852 goto out;
2853 }
2854 }
2855 /* eat the whole request */
2856 bo_skip(si_oc(si), si_ob(si)->o);
2857 res->flags |= CF_READ_NULL;
2858 si_shutr(si);
2859 }
2860
2861 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
2862 si_shutw(si);
2863
2864 if (appctx->st0 == STAT_HTTP_DONE) {
2865 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
2866 si_shutr(si);
2867 res->flags |= CF_READ_NULL;
2868 }
2869 }
2870 out:
2871 /* just to make gcc happy */ ;
2872}
2873
Willy Tarreau0baac8c2016-11-22 16:36:53 +01002874/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2875static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2876{
2877 int field;
2878
2879 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2880 if (!field_format(info, field))
2881 continue;
2882
2883 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2884 return 0;
2885 if (!stats_emit_raw_data_field(out, &info[field]))
2886 return 0;
2887 if (!chunk_strcat(out, "\n"))
2888 return 0;
2889 }
2890 return 1;
2891}
2892
2893/* Dump all fields from <info> into <out> using the "show info typed" format */
2894static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
2895{
2896 int field;
2897
2898 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2899 if (!field_format(info, field))
2900 continue;
2901
2902 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
2903 return 0;
2904 if (!stats_emit_field_tags(out, &info[field], ':'))
2905 return 0;
2906 if (!stats_emit_typed_data_field(out, &info[field]))
2907 return 0;
2908 if (!chunk_strcat(out, "\n"))
2909 return 0;
2910 }
2911 return 1;
2912}
2913
2914/* Fill <info> with HAProxy global info. <info> is preallocated
2915 * array of length <len>. The length of the aray must be
2916 * INF_TOTAL_FIELDS. If this length is less then this value, the
2917 * function returns 0, otherwise, it returns 1.
2918 */
2919int stats_fill_info(struct field *info, int len)
2920{
2921 unsigned int up = (now.tv_sec - start_date.tv_sec);
2922 struct chunk *out = get_trash_chunk();
2923
2924#ifdef USE_OPENSSL
2925 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2926 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2927 int ssl_reuse = 0;
2928
2929 if (ssl_key_rate < ssl_sess_rate) {
2930 /* count the ssl reuse ratio and avoid overflows in both directions */
2931 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2932 }
2933#endif
2934
2935 if (len < INF_TOTAL_FIELDS)
2936 return 0;
2937
2938 chunk_reset(out);
2939 memset(info, 0, sizeof(*info) * len);
2940
2941 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
2942 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
2943 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
2944
2945 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
2946 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
2947 info[INF_PID] = mkf_u32(FO_STATUS, pid);
2948
2949 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
2950 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
2951
2952 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
2953 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
2954 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
2955 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
2956 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
2957 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
2958 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
2959 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
2960 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
2961 info[INF_CURR_CONN] = mkf_u32(0, actconn);
2962 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
2963 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
2964#ifdef USE_OPENSSL
2965 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
2966 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
2967 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
2968#endif
2969 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
2970 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
2971 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
2972 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
2973 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
2974 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
2975 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
2976 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
2977 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
2978
2979#ifdef USE_OPENSSL
2980 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
2981 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
2982 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
2983 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
2984 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
2985 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
2986 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
2987 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
2988 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
2989 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
2990#endif
2991 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
2992 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
2993 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
2994#ifdef USE_ZLIB
2995 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
2996 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
2997#endif
2998 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
2999 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3000 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3001 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3002 if (global.desc)
3003 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3004
3005 return 1;
3006}
3007
3008/* This function dumps information onto the stream interface's read buffer.
3009 * It returns 0 as long as it does not complete, non-zero upon completion.
3010 * No state is used.
3011 */
3012static int stats_dump_info_to_buffer(struct stream_interface *si)
3013{
3014 struct appctx *appctx = __objt_appctx(si->end);
3015
3016 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3017 return 0;
3018
3019 chunk_reset(&trash);
3020
3021 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3022 stats_dump_typed_info_fields(&trash, info);
3023 else
3024 stats_dump_info_fields(&trash, info);
3025
3026 if (bi_putchk(si_ic(si), &trash) == -1) {
3027 si_applet_cant_put(si);
3028 return 0;
3029 }
3030
3031 return 1;
3032}
3033
Willy Tarreau89d467c2016-11-23 11:02:40 +01003034static int cli_parse_clear_counters(char **args, struct appctx *appctx, void *private)
3035{
3036 struct proxy *px;
3037 struct server *sv;
3038 struct listener *li;
3039 int clrall = 0;
3040
3041 if (strcmp(args[2], "all") == 0)
3042 clrall = 1;
3043
3044 /* check permissions */
3045 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3046 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3047 return 1;
3048
3049 for (px = proxy; px; px = px->next) {
3050 if (clrall) {
3051 memset(&px->be_counters, 0, sizeof(px->be_counters));
3052 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3053 }
3054 else {
3055 px->be_counters.conn_max = 0;
3056 px->be_counters.p.http.rps_max = 0;
3057 px->be_counters.sps_max = 0;
3058 px->be_counters.cps_max = 0;
3059 px->be_counters.nbpend_max = 0;
3060
3061 px->fe_counters.conn_max = 0;
3062 px->fe_counters.p.http.rps_max = 0;
3063 px->fe_counters.sps_max = 0;
3064 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003065 }
3066
3067 for (sv = px->srv; sv; sv = sv->next)
3068 if (clrall)
3069 memset(&sv->counters, 0, sizeof(sv->counters));
3070 else {
3071 sv->counters.cur_sess_max = 0;
3072 sv->counters.nbpend_max = 0;
3073 sv->counters.sps_max = 0;
3074 }
3075
3076 list_for_each_entry(li, &px->conf.listeners, by_fe)
3077 if (li->counters) {
3078 if (clrall)
3079 memset(li->counters, 0, sizeof(*li->counters));
3080 else
3081 li->counters->conn_max = 0;
3082 }
3083 }
3084
3085 global.cps_max = 0;
3086 global.sps_max = 0;
3087 return 1;
3088}
3089
3090
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003091static int cli_parse_show_info(char **args, struct appctx *appctx, void *private)
3092{
3093 if (strcmp(args[2], "typed") == 0)
3094 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003095 return 0;
3096}
3097
3098
Willy Tarreau2b812e22016-11-22 16:18:05 +01003099static int cli_parse_show_stat(char **args, struct appctx *appctx, void *private)
3100{
3101 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003102 struct proxy *px;
3103
3104 px = proxy_find_by_name(args[2], 0, 0);
3105 if (px)
3106 appctx->ctx.stats.iid = px->uuid;
3107 else
3108 appctx->ctx.stats.iid = atoi(args[2]);
3109
3110 if (!appctx->ctx.stats.iid) {
3111 appctx->ctx.cli.msg = "No such proxy.\n";
3112 appctx->st0 = CLI_ST_PRINT;
3113 return 1;
3114 }
3115
Willy Tarreau2b812e22016-11-22 16:18:05 +01003116 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003117 appctx->ctx.stats.type = atoi(args[3]);
3118 appctx->ctx.stats.sid = atoi(args[4]);
3119 if (strcmp(args[5], "typed") == 0)
3120 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3121 }
3122 else if (strcmp(args[2], "typed") == 0)
3123 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3124
Willy Tarreau2b812e22016-11-22 16:18:05 +01003125 return 0;
3126}
3127
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003128static int cli_io_handler_dump_info(struct appctx *appctx)
3129{
3130 return stats_dump_info_to_buffer(appctx->owner);
3131}
3132
Willy Tarreau2b812e22016-11-22 16:18:05 +01003133/* This I/O handler runs as an applet embedded in a stream interface. It is
3134 * used to send raw stats over a socket.
3135 */
3136static int cli_io_handler_dump_stat(struct appctx *appctx)
3137{
3138 return stats_dump_stat_to_buffer(appctx->owner, NULL);
3139}
3140
3141/* register cli keywords */
3142static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003143 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003144 { { "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 +01003145 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
3146 {{},}
3147}};
3148
William Lallemand74c24fb2016-11-21 17:18:36 +01003149struct applet http_stats_applet = {
3150 .obj_type = OBJ_TYPE_APPLET,
3151 .name = "<STATS>", /* used for logging */
3152 .fct = http_stats_io_handler,
3153 .release = NULL,
3154};
3155
Willy Tarreau2b812e22016-11-22 16:18:05 +01003156__attribute__((constructor))
3157static void __stat_init(void)
3158{
3159 cli_register_kw(&cli_kws);
3160}
3161
William Lallemand74c24fb2016-11-21 17:18:36 +01003162/*
3163 * Local variables:
3164 * c-indent-level: 8
3165 * c-basic-offset: 8
3166 * End:
3167 */