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