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