Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions dedicated to statistics output |
| 3 | * |
Willy Tarreau | a206fa9 | 2009-01-25 14:02:00 +0100 | [diff] [blame] | 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 19 | #include <pwd.h> |
| 20 | #include <grp.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 10522fd | 2008-07-09 20:12:41 +0200 | [diff] [blame] | 26 | #include <common/cfgparse.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 27 | #include <common/compat.h> |
| 28 | #include <common/config.h> |
| 29 | #include <common/debug.h> |
| 30 | #include <common/memory.h> |
| 31 | #include <common/mini-clist.h> |
| 32 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 33 | #include <common/ticks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 34 | #include <common/time.h> |
| 35 | #include <common/uri_auth.h> |
| 36 | #include <common/version.h> |
| 37 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 38 | #include <types/global.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 39 | |
| 40 | #include <proto/backend.h> |
| 41 | #include <proto/buffers.h> |
| 42 | #include <proto/dumpstats.h> |
| 43 | #include <proto/fd.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 44 | #include <proto/freq_ctr.h> |
Willy Tarreau | a206fa9 | 2009-01-25 14:02:00 +0100 | [diff] [blame] | 45 | #include <proto/pipe.h> |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 46 | #include <proto/proto_uxst.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 47 | #include <proto/session.h> |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 48 | #include <proto/server.h> |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 49 | #include <proto/stream_interface.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 50 | |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 51 | /* This function parses a "stats" statement in the "global" section. It returns |
| 52 | * -1 if there is any error, otherwise zero. If it returns -1, it may write an |
| 53 | * error message into ther <err> buffer, for at most <errlen> bytes, trailing |
| 54 | * zero included. The trailing '\n' must not be written. The function must be |
| 55 | * called with <args> pointing to the first word after "stats". |
| 56 | */ |
Willy Tarreau | 10522fd | 2008-07-09 20:12:41 +0200 | [diff] [blame] | 57 | static int stats_parse_global(char **args, int section_type, struct proxy *curpx, |
| 58 | struct proxy *defpx, char *err, int errlen) |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 59 | { |
Willy Tarreau | 10522fd | 2008-07-09 20:12:41 +0200 | [diff] [blame] | 60 | args++; |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 61 | if (!strcmp(args[0], "socket")) { |
| 62 | struct sockaddr_un su; |
| 63 | int cur_arg; |
| 64 | |
| 65 | if (*args[1] == 0) { |
| 66 | snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket"); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | if (global.stats_sock.state != LI_NEW) { |
| 71 | snprintf(err, errlen, "'stats socket' already specified in global section"); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | su.sun_family = AF_UNIX; |
| 76 | strncpy(su.sun_path, args[1], sizeof(su.sun_path)); |
| 77 | su.sun_path[sizeof(su.sun_path) - 1] = 0; |
| 78 | memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit |
| 79 | |
| 80 | global.stats_sock.state = LI_INIT; |
Willy Tarreau | 6fb42e0 | 2007-10-28 17:02:33 +0100 | [diff] [blame] | 81 | global.stats_sock.options = LI_O_NONE; |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 82 | global.stats_sock.accept = uxst_event_accept; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 83 | global.stats_sock.handler = uxst_process_session; |
| 84 | global.stats_sock.analysers = AN_REQ_UNIX_STATS; |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 85 | global.stats_sock.private = NULL; |
| 86 | |
| 87 | cur_arg = 2; |
| 88 | while (*args[cur_arg]) { |
| 89 | if (!strcmp(args[cur_arg], "uid")) { |
| 90 | global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]); |
| 91 | cur_arg += 2; |
| 92 | } |
| 93 | else if (!strcmp(args[cur_arg], "gid")) { |
| 94 | global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]); |
| 95 | cur_arg += 2; |
| 96 | } |
| 97 | else if (!strcmp(args[cur_arg], "mode")) { |
| 98 | global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8); |
| 99 | cur_arg += 2; |
| 100 | } |
| 101 | else if (!strcmp(args[cur_arg], "user")) { |
| 102 | struct passwd *user; |
| 103 | user = getpwnam(args[cur_arg + 1]); |
| 104 | if (!user) { |
| 105 | snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')", |
| 106 | args[cur_arg + 1]); |
| 107 | return -1; |
| 108 | } |
| 109 | global.stats_sock.perm.ux.uid = user->pw_uid; |
| 110 | cur_arg += 2; |
| 111 | } |
| 112 | else if (!strcmp(args[cur_arg], "group")) { |
| 113 | struct group *group; |
| 114 | group = getgrnam(args[cur_arg + 1]); |
| 115 | if (!group) { |
| 116 | snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')", |
| 117 | args[cur_arg + 1]); |
| 118 | return -1; |
| 119 | } |
| 120 | global.stats_sock.perm.ux.gid = group->gr_gid; |
| 121 | cur_arg += 2; |
| 122 | } |
| 123 | else { |
| 124 | snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'"); |
| 125 | return -1; |
| 126 | } |
| 127 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 128 | |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 129 | uxst_add_listener(&global.stats_sock); |
| 130 | global.maxsock++; |
| 131 | } |
| 132 | else if (!strcmp(args[0], "timeout")) { |
Willy Tarreau | b3f32f5 | 2007-12-02 22:15:14 +0100 | [diff] [blame] | 133 | unsigned timeout; |
| 134 | const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS); |
| 135 | |
| 136 | if (res) { |
| 137 | snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res); |
| 138 | return -1; |
| 139 | } |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 140 | |
Willy Tarreau | b3f32f5 | 2007-12-02 22:15:14 +0100 | [diff] [blame] | 141 | if (!timeout) { |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 142 | snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'"); |
| 143 | return -1; |
| 144 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 145 | global.stats_timeout = MS_TO_TICKS(timeout); |
Willy Tarreau | fbee713 | 2007-10-18 13:53:22 +0200 | [diff] [blame] | 146 | } |
| 147 | else if (!strcmp(args[0], "maxconn")) { |
| 148 | int maxconn = atol(args[1]); |
| 149 | |
| 150 | if (maxconn <= 0) { |
| 151 | snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'"); |
| 152 | return -1; |
| 153 | } |
| 154 | global.maxsock -= global.stats_sock.maxconn; |
| 155 | global.stats_sock.maxconn = maxconn; |
| 156 | global.maxsock += global.stats_sock.maxconn; |
| 157 | } |
| 158 | else { |
| 159 | snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section"); |
| 160 | return -1; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 165 | int print_csv_header(struct chunk *msg, int size) |
| 166 | { |
| 167 | return chunk_printf(msg, size, |
| 168 | "# pxname,svname," |
| 169 | "qcur,qmax," |
| 170 | "scur,smax,slim,stot," |
| 171 | "bin,bout," |
| 172 | "dreq,dresp," |
| 173 | "ereq,econ,eresp," |
| 174 | "wretr,wredis," |
| 175 | "status,weight,act,bck," |
| 176 | "chkfail,chkdown,lastchg,downtime,qlimit," |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 177 | "pid,iid,sid,throttle,lbtot,tracked,type,rate," |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 178 | "\n"); |
| 179 | } |
| 180 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 181 | /* |
| 182 | * Produces statistics data for the session <s>. Expects to be called with |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 183 | * client socket shut down on input. It *may* make use of informations from |
| 184 | * <uri>. s->data_ctx must have been zeroed first, and the flags properly set. |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 185 | * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the |
| 186 | * dump is finished and the session must be closed, or -1 in case of any error. |
Willy Tarreau | 0a46489 | 2008-12-07 18:30:00 +0100 | [diff] [blame] | 187 | * It automatically clears the HIJACK bit from the response buffer. |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 188 | */ |
Willy Tarreau | 0a46489 | 2008-12-07 18:30:00 +0100 | [diff] [blame] | 189 | int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri) |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 190 | { |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 191 | struct proxy *px; |
| 192 | struct chunk msg; |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 193 | unsigned int up; |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 194 | |
| 195 | msg.len = 0; |
| 196 | msg.str = trash; |
| 197 | |
| 198 | switch (s->data_state) { |
| 199 | case DATA_ST_INIT: |
| 200 | /* the function had not been called yet, let's prepare the |
| 201 | * buffer for a response. |
| 202 | */ |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 203 | stream_int_retnclose(rep->cons, &msg); |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 204 | s->data_state = DATA_ST_HEAD; |
| 205 | /* fall through */ |
| 206 | |
| 207 | case DATA_ST_HEAD: |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 208 | if (s->data_ctx.stats.flags & STAT_SHOW_STAT) { |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 209 | print_csv_header(&msg, sizeof(trash)); |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 210 | if (buffer_write_chunk(rep, &msg) >= 0) |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 211 | return 0; |
| 212 | } |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 213 | |
| 214 | s->data_state = DATA_ST_INFO; |
| 215 | /* fall through */ |
| 216 | |
| 217 | case DATA_ST_INFO: |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 218 | up = (now.tv_sec - start_date.tv_sec); |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 219 | if (s->data_ctx.stats.flags & STAT_SHOW_INFO) { |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 220 | chunk_printf(&msg, sizeof(trash), |
| 221 | "Name: " PRODUCT_NAME "\n" |
| 222 | "Version: " HAPROXY_VERSION "\n" |
| 223 | "Release_date: " HAPROXY_DATE "\n" |
| 224 | "Nbproc: %d\n" |
| 225 | "Process_num: %d\n" |
| 226 | "Pid: %d\n" |
| 227 | "Uptime: %dd %dh%02dm%02ds\n" |
| 228 | "Uptime_sec: %d\n" |
| 229 | "Memmax_MB: %d\n" |
| 230 | "Ulimit-n: %d\n" |
| 231 | "Maxsock: %d\n" |
| 232 | "Maxconn: %d\n" |
Willy Tarreau | a206fa9 | 2009-01-25 14:02:00 +0100 | [diff] [blame] | 233 | "Maxpipes: %d\n" |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 234 | "CurrConns: %d\n" |
Willy Tarreau | a206fa9 | 2009-01-25 14:02:00 +0100 | [diff] [blame] | 235 | "PipesUsed: %d\n" |
| 236 | "PipesFree: %d\n" |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 237 | "", |
| 238 | global.nbproc, |
| 239 | relative_pid, |
| 240 | pid, |
| 241 | up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60), |
| 242 | up, |
| 243 | global.rlimit_memmax, |
| 244 | global.rlimit_nofile, |
Willy Tarreau | a206fa9 | 2009-01-25 14:02:00 +0100 | [diff] [blame] | 245 | global.maxsock, global.maxconn, global.maxpipes, |
| 246 | actconn, pipes_used, pipes_free |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 247 | ); |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 248 | if (buffer_write_chunk(rep, &msg) >= 0) |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 252 | s->data_ctx.stats.px = proxy; |
| 253 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 254 | |
| 255 | s->data_ctx.stats.sv = NULL; |
| 256 | s->data_ctx.stats.sv_st = 0; |
| 257 | |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 258 | s->data_state = DATA_ST_LIST; |
| 259 | /* fall through */ |
| 260 | |
| 261 | case DATA_ST_LIST: |
| 262 | /* dump proxies */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 263 | if (s->data_ctx.stats.flags & STAT_SHOW_STAT) { |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 264 | while (s->data_ctx.stats.px) { |
| 265 | px = s->data_ctx.stats.px; |
| 266 | /* skip the disabled proxies and non-networked ones */ |
| 267 | if (px->state != PR_STSTOPPED && |
| 268 | (px->cap & (PR_CAP_FE | PR_CAP_BE))) |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 269 | if (stats_dump_proxy(s, px, NULL) == 0) |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 270 | return 0; |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 271 | |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 272 | s->data_ctx.stats.px = px->next; |
| 273 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 274 | } |
| 275 | /* here, we just have reached the last proxy */ |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 276 | } |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 277 | |
| 278 | s->data_state = DATA_ST_END; |
| 279 | /* fall through */ |
| 280 | |
| 281 | case DATA_ST_END: |
| 282 | s->data_state = DATA_ST_FIN; |
Willy Tarreau | 0a46489 | 2008-12-07 18:30:00 +0100 | [diff] [blame] | 283 | /* fall through */ |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 284 | |
| 285 | case DATA_ST_FIN: |
Willy Tarreau | 0a46489 | 2008-12-07 18:30:00 +0100 | [diff] [blame] | 286 | buffer_stop_hijack(rep); |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 287 | return 1; |
| 288 | |
| 289 | default: |
| 290 | /* unknown state ! */ |
Willy Tarreau | 0a46489 | 2008-12-07 18:30:00 +0100 | [diff] [blame] | 291 | buffer_stop_hijack(rep); |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 292 | return -1; |
| 293 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
| 297 | /* This function is called to send output to the response buffer. It simply |
| 298 | * calls stats_dump_raw(), and releases the buffer's hijack bit when the dump |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 299 | * is finished. |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 300 | */ |
Willy Tarreau | 0a46489 | 2008-12-07 18:30:00 +0100 | [diff] [blame] | 301 | void stats_dump_raw_to_buffer(struct session *s, struct buffer *rep) |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 302 | { |
| 303 | if (s->ana_state != STATS_ST_REP) |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 304 | return; |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 305 | |
Willy Tarreau | 0a46489 | 2008-12-07 18:30:00 +0100 | [diff] [blame] | 306 | if (stats_dump_raw(s, rep, NULL) != 0) |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 307 | s->ana_state = STATS_ST_CLOSE; |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 308 | return; |
Willy Tarreau | 3e76e72 | 2007-10-17 18:57:38 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
| 312 | /* |
| 313 | * Produces statistics data for the session <s>. Expects to be called with |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 314 | * client socket shut down on input. It stops by itself by unsetting the |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 315 | * BF_HIJACK flag from the buffer, which it uses to keep on being called |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 316 | * when there is free space in the buffer, of simply by letting an empty buffer |
| 317 | * upon return.s->data_ctx must have been zeroed before the first call, and the |
| 318 | * flags set. It returns 0 if it had to stop writing data and an I/O is needed, |
| 319 | * 1 if the dump is finished and the session must be closed, or -1 in case of |
| 320 | * any error. |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 321 | */ |
Willy Tarreau | 0a46489 | 2008-12-07 18:30:00 +0100 | [diff] [blame] | 322 | int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 323 | { |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 324 | struct proxy *px; |
| 325 | struct chunk msg; |
| 326 | unsigned int up; |
| 327 | |
| 328 | msg.len = 0; |
| 329 | msg.str = trash; |
| 330 | |
| 331 | switch (s->data_state) { |
| 332 | case DATA_ST_INIT: |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 333 | chunk_printf(&msg, sizeof(trash), |
| 334 | "HTTP/1.0 200 OK\r\n" |
| 335 | "Cache-Control: no-cache\r\n" |
| 336 | "Connection: close\r\n" |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 337 | "Content-Type: %s\r\n", |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 338 | (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html"); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 339 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 340 | if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH)) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 341 | chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n", |
| 342 | uri->refresh); |
| 343 | |
| 344 | chunk_printf(&msg, sizeof(trash), "\r\n"); |
| 345 | |
| 346 | s->txn.status = 200; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 347 | stream_int_retnclose(rep->cons, &msg); // send the start of the response. |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 348 | msg.len = 0; |
| 349 | |
| 350 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 351 | s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy |
| 352 | if (!(s->flags & SN_FINST_MASK)) |
| 353 | s->flags |= SN_FINST_R; |
| 354 | |
| 355 | if (s->txn.meth == HTTP_METH_HEAD) { |
| 356 | /* that's all we return in case of HEAD request */ |
| 357 | s->data_state = DATA_ST_FIN; |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 358 | buffer_stop_hijack(rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 359 | return 1; |
| 360 | } |
| 361 | |
| 362 | s->data_state = DATA_ST_HEAD; /* let's start producing data */ |
| 363 | /* fall through */ |
| 364 | |
| 365 | case DATA_ST_HEAD: |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 366 | if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 367 | /* WARNING! This must fit in the first buffer !!! */ |
| 368 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 369 | "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n" |
| 370 | "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n" |
| 371 | "<style type=\"text/css\"><!--\n" |
| 372 | "body {" |
| 373 | " font-family: helvetica, arial;" |
| 374 | " font-size: 12px;" |
| 375 | " font-weight: normal;" |
| 376 | " color: black;" |
| 377 | " background: white;" |
| 378 | "}\n" |
| 379 | "th,td {" |
| 380 | " font-size: 0.8em;" |
| 381 | " align: center;" |
| 382 | "}\n" |
| 383 | "h1 {" |
| 384 | " font-size: xx-large;" |
| 385 | " margin-bottom: 0.5em;" |
| 386 | "}\n" |
| 387 | "h2 {" |
| 388 | " font-family: helvetica, arial;" |
| 389 | " font-size: x-large;" |
| 390 | " font-weight: bold;" |
| 391 | " font-style: italic;" |
| 392 | " color: #6020a0;" |
| 393 | " margin-top: 0em;" |
| 394 | " margin-bottom: 0em;" |
| 395 | "}\n" |
| 396 | "h3 {" |
| 397 | " font-family: helvetica, arial;" |
| 398 | " font-size: 16px;" |
| 399 | " font-weight: bold;" |
| 400 | " color: #b00040;" |
| 401 | " background: #e8e8d0;" |
| 402 | " margin-top: 0em;" |
| 403 | " margin-bottom: 0em;" |
| 404 | "}\n" |
| 405 | "li {" |
| 406 | " margin-top: 0.25em;" |
| 407 | " margin-right: 2em;" |
| 408 | "}\n" |
| 409 | ".hr {margin-top: 0.25em;" |
| 410 | " border-color: black;" |
| 411 | " border-bottom-style: solid;" |
| 412 | "}\n" |
| 413 | ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n" |
| 414 | ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n" |
| 415 | ".total {background: #20D0D0;color: #ffff80;}\n" |
| 416 | ".frontend {background: #e8e8d0;}\n" |
| 417 | ".backend {background: #e8e8d0;}\n" |
| 418 | ".active0 {background: #ff9090;}\n" |
| 419 | ".active1 {background: #ffd020;}\n" |
| 420 | ".active2 {background: #ffffa0;}\n" |
| 421 | ".active3 {background: #c0ffc0;}\n" |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 422 | ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */ |
| 423 | ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */ |
| 424 | ".active6 {background: #e0e0e0;}\n" |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 425 | ".backup0 {background: #ff9090;}\n" |
| 426 | ".backup1 {background: #ff80ff;}\n" |
| 427 | ".backup2 {background: #c060ff;}\n" |
| 428 | ".backup3 {background: #b0d0ff;}\n" |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 429 | ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */ |
| 430 | ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */ |
| 431 | ".backup6 {background: #e0e0e0;}\n" |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 432 | "table.tbl { border-collapse: collapse; border-style: none;}\n" |
| 433 | "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n" |
| 434 | "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n" |
| 435 | "table.tbl th.empty { border-style: none; empty-cells: hide;}\n" |
| 436 | "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n" |
| 437 | "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n" |
| 438 | "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n" |
| 439 | "-->\n" |
| 440 | "</style></head>\n"); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 441 | } else { |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 442 | print_csv_header(&msg, sizeof(trash)); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 443 | } |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 444 | if (buffer_write_chunk(rep, &msg) >= 0) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 445 | return 0; |
| 446 | |
| 447 | s->data_state = DATA_ST_INFO; |
| 448 | /* fall through */ |
| 449 | |
| 450 | case DATA_ST_INFO: |
| 451 | up = (now.tv_sec - start_date.tv_sec); |
| 452 | |
| 453 | /* WARNING! this has to fit the first packet too. |
| 454 | * We are around 3.5 kB, add adding entries will |
| 455 | * become tricky if we want to support 4kB buffers ! |
| 456 | */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 457 | if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 458 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 459 | "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">" |
| 460 | PRODUCT_NAME "%s</a></h1>\n" |
| 461 | "<h2>Statistics Report for pid %d</h2>\n" |
| 462 | "<hr width=\"100%%\" class=\"hr\">\n" |
| 463 | "<h3>> General process information</h3>\n" |
| 464 | "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n" |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 465 | "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n" |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 466 | "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n" |
| 467 | "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n" |
Willy Tarreau | a206fa9 | 2009-01-25 14:02:00 +0100 | [diff] [blame] | 468 | "<b>maxsock = </b> %d ; <b>maxconn = </b> %d ; <b>maxpipes = </b> %d<br>\n" |
| 469 | "current conns = %d ; current pipes = %d/%d<br>\n" |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 470 | "</td><td align=\"center\" nowrap>\n" |
| 471 | "<table class=\"lgd\"><tr>\n" |
| 472 | "<td class=\"active3\"> </td><td class=\"noborder\">active UP </td>" |
| 473 | "<td class=\"backup3\"> </td><td class=\"noborder\">backup UP </td>" |
| 474 | "</tr><tr>\n" |
| 475 | "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>" |
| 476 | "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>" |
| 477 | "</tr><tr>\n" |
| 478 | "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>" |
| 479 | "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>" |
| 480 | "</tr><tr>\n" |
| 481 | "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN </td>" |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 482 | "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>" |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 483 | "</tr></table>\n" |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 484 | "Note: UP with load-balancing disabled is reported as \"NOLB\"." |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 485 | "</td>" |
| 486 | "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">" |
| 487 | "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">" |
| 488 | "", |
| 489 | (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING), |
Willy Tarreau | a8efd36 | 2008-01-03 10:19:15 +0100 | [diff] [blame] | 490 | pid, pid, |
| 491 | relative_pid, global.nbproc, |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 492 | up / 86400, (up % 86400) / 3600, |
| 493 | (up % 3600) / 60, (up % 60), |
| 494 | global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited", |
| 495 | global.rlimit_memmax ? " MB" : "", |
| 496 | global.rlimit_nofile, |
Willy Tarreau | a206fa9 | 2009-01-25 14:02:00 +0100 | [diff] [blame] | 497 | global.maxsock, global.maxconn, global.maxpipes, |
| 498 | actconn, pipes_used, pipes_used+pipes_free |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 499 | ); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 500 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 501 | if (s->data_ctx.stats.flags & STAT_HIDE_DOWN) |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 502 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 503 | "<li><a href=\"%s%s%s\">Show all servers</a><br>\n", |
| 504 | uri->uri_prefix, |
| 505 | "", |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 506 | (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : ""); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 507 | else |
| 508 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 509 | "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n", |
| 510 | uri->uri_prefix, |
| 511 | ";up", |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 512 | (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : ""); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 513 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 514 | if (uri->refresh > 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 515 | if (s->data_ctx.stats.flags & STAT_NO_REFRESH) |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 516 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 517 | "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n", |
| 518 | uri->uri_prefix, |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 519 | (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 520 | ""); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 521 | else |
| 522 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 523 | "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n", |
| 524 | uri->uri_prefix, |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 525 | (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 526 | ";norefresh"); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 527 | } |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 528 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 529 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 530 | "<li><a href=\"%s%s%s\">Refresh now</a><br>\n", |
| 531 | uri->uri_prefix, |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 532 | (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 533 | (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : ""); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 534 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 535 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 5031e6a | 2007-10-18 11:05:48 +0200 | [diff] [blame] | 536 | "<li><a href=\"%s;csv%s\">CSV export</a><br>\n", |
| 537 | uri->uri_prefix, |
| 538 | (uri->refresh > 0) ? ";norefresh" : ""); |
| 539 | |
| 540 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 541 | "</td>" |
| 542 | "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">" |
| 543 | "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n" |
| 544 | "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n" |
| 545 | "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n" |
| 546 | "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n" |
| 547 | "</ul>" |
| 548 | "</td>" |
| 549 | "</tr></table>\n" |
| 550 | "" |
| 551 | ); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 552 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 553 | if (buffer_write_chunk(rep, &msg) >= 0) |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 554 | return 0; |
| 555 | } |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 556 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 557 | s->data_ctx.stats.px = proxy; |
| 558 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 559 | s->data_state = DATA_ST_LIST; |
| 560 | /* fall through */ |
| 561 | |
| 562 | case DATA_ST_LIST: |
| 563 | /* dump proxies */ |
| 564 | while (s->data_ctx.stats.px) { |
| 565 | px = s->data_ctx.stats.px; |
| 566 | /* skip the disabled proxies and non-networked ones */ |
| 567 | if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE))) |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 568 | if (stats_dump_proxy(s, px, uri) == 0) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 569 | return 0; |
| 570 | |
| 571 | s->data_ctx.stats.px = px->next; |
| 572 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 573 | } |
| 574 | /* here, we just have reached the last proxy */ |
| 575 | |
| 576 | s->data_state = DATA_ST_END; |
| 577 | /* fall through */ |
| 578 | |
| 579 | case DATA_ST_END: |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 580 | if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 581 | chunk_printf(&msg, sizeof(trash), "</body></html>\n"); |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 582 | if (buffer_write_chunk(rep, &msg) >= 0) |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 583 | return 0; |
| 584 | } |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 585 | |
| 586 | s->data_state = DATA_ST_FIN; |
| 587 | /* fall through */ |
| 588 | |
| 589 | case DATA_ST_FIN: |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 590 | buffer_stop_hijack(rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 591 | return 1; |
| 592 | |
| 593 | default: |
| 594 | /* unknown state ! */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 595 | buffer_stop_hijack(rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 596 | return -1; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | /* |
| 602 | * Dumps statistics for a proxy. |
| 603 | * Returns 0 if it had to stop dumping data because of lack of buffer space, |
| 604 | * ot non-zero if everything completed. |
| 605 | */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 606 | int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 607 | { |
| 608 | struct buffer *rep = s->rep; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 609 | struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */ |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 610 | struct chunk msg; |
| 611 | |
| 612 | msg.len = 0; |
| 613 | msg.str = trash; |
| 614 | |
| 615 | switch (s->data_ctx.stats.px_st) { |
| 616 | case DATA_ST_PX_INIT: |
| 617 | /* we are on a new proxy */ |
| 618 | |
| 619 | if (uri && uri->scope) { |
| 620 | /* we have a limited scope, we have to check the proxy name */ |
| 621 | struct stat_scope *scope; |
| 622 | int len; |
| 623 | |
| 624 | len = strlen(px->id); |
| 625 | scope = uri->scope; |
| 626 | |
| 627 | while (scope) { |
| 628 | /* match exact proxy name */ |
| 629 | if (scope->px_len == len && !memcmp(px->id, scope->px_id, len)) |
| 630 | break; |
| 631 | |
| 632 | /* match '.' which means 'self' proxy */ |
Willy Tarreau | 1388a3a | 2007-10-18 16:38:37 +0200 | [diff] [blame] | 633 | if (!strcmp(scope->px_id, ".") && px == s->be) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 634 | break; |
| 635 | scope = scope->next; |
| 636 | } |
| 637 | |
| 638 | /* proxy name not found : don't dump anything */ |
| 639 | if (scope == NULL) |
| 640 | return 1; |
| 641 | } |
| 642 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 643 | if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) && |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 644 | (px->uuid != s->data_ctx.stats.iid)) |
| 645 | return 1; |
| 646 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 647 | s->data_ctx.stats.px_st = DATA_ST_PX_TH; |
| 648 | /* fall through */ |
| 649 | |
| 650 | case DATA_ST_PX_TH: |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 651 | if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 652 | /* print a new table */ |
| 653 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 654 | "<table cols=\"27\" class=\"tbl\" width=\"100%%\">\n" |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 655 | "<tr align=\"center\" class=\"titre\">" |
| 656 | "<th colspan=2 class=\"pxname\">%s</th>" |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 657 | "<th colspan=25 class=\"empty\"></th>" |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 658 | "</tr>\n" |
| 659 | "<tr align=\"center\" class=\"titre\">" |
| 660 | "<th rowspan=2></th>" |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 661 | "<th colspan=3>Queue</th><th colspan=6>Sessions</th>" |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 662 | "<th colspan=2>Bytes</th><th colspan=2>Denied</th>" |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 663 | "<th colspan=3>Errors</th><th colspan=2>Warnings</th>" |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 664 | "<th colspan=8>Server</th>" |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 665 | "</tr>\n" |
| 666 | "<tr align=\"center\" class=\"titre\">" |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 667 | "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>" |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 668 | "<th>Limit</th><th>Rate</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>" |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 669 | "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>" |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 670 | "<th>Resp</th><th>Retr</th><th>Redis</th>" |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 671 | "<th>Status</th><th>Wght</th><th>Act</th>" |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 672 | "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>" |
| 673 | "<th>Thrtle</th>\n" |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 674 | "</tr>", |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 675 | px->id); |
| 676 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 677 | if (buffer_write_chunk(rep, &msg) >= 0) |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 678 | return 0; |
| 679 | } |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 680 | |
| 681 | s->data_ctx.stats.px_st = DATA_ST_PX_FE; |
| 682 | /* fall through */ |
| 683 | |
| 684 | case DATA_ST_PX_FE: |
| 685 | /* print the frontend */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 686 | if ((px->cap & PR_CAP_FE) && |
| 687 | (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) { |
| 688 | if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 689 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 690 | /* name, queue */ |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 691 | "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>" |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 692 | /* sessions : current, max, limit, rate, total, lbtot */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 693 | "<td align=right>%d</td><td align=right>%d</td>" |
| 694 | "<td align=right>%d</td><td align=right>%d</td>" |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 695 | "<td align=right>%d</td><td align=right></td>" |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 696 | /* bytes : in, out */ |
| 697 | "<td align=right>%lld</td><td align=right>%lld</td>" |
| 698 | /* denied: req, resp */ |
| 699 | "<td align=right>%d</td><td align=right>%d</td>" |
| 700 | /* errors : request, connect, response */ |
| 701 | "<td align=right>%d</td><td align=right></td><td align=right></td>" |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 702 | /* warnings: retries, redispatches */ |
| 703 | "<td align=right></td><td align=right></td>" |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 704 | /* server status : reflect frontend status */ |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 705 | "<td align=center>%s</td>" |
| 706 | /* rest of server: nothing */ |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 707 | "<td align=center colspan=7></td></tr>" |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 708 | "", |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 709 | px->feconn, px->feconn_max, px->maxconn, |
| 710 | read_freq_ctr(&px->fe_sess_per_sec), px->cum_feconn, |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 711 | px->bytes_in, px->bytes_out, |
| 712 | px->denied_req, px->denied_resp, |
| 713 | px->failed_req, |
| 714 | px->state == PR_STRUN ? "OPEN" : |
| 715 | px->state == PR_STIDLE ? "FULL" : "STOP"); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 716 | } else { |
| 717 | chunk_printf(&msg, sizeof(trash), |
| 718 | /* pxid, name, queue cur, queue max, */ |
| 719 | "%s,FRONTEND,,," |
Willy Tarreau | ddbb82f | 2007-12-05 10:34:49 +0100 | [diff] [blame] | 720 | /* sessions : current, max, limit, total */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 721 | "%d,%d,%d,%d," |
| 722 | /* bytes : in, out */ |
| 723 | "%lld,%lld," |
| 724 | /* denied: req, resp */ |
| 725 | "%d,%d," |
| 726 | /* errors : request, connect, response */ |
| 727 | "%d,,," |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 728 | /* warnings: retries, redispatches */ |
| 729 | ",," |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 730 | /* server status : reflect frontend status */ |
| 731 | "%s," |
| 732 | /* rest of server: nothing */ |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 733 | ",,,,,,,," |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 734 | /* pid, iid, sid, throttle, lbtot, tracked, type */ |
| 735 | "%d,%d,0,,,,%d," |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 736 | /* rate */ |
| 737 | "%u," |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 738 | "\n", |
| 739 | px->id, |
| 740 | px->feconn, px->feconn_max, px->maxconn, px->cum_feconn, |
| 741 | px->bytes_in, px->bytes_out, |
| 742 | px->denied_req, px->denied_resp, |
| 743 | px->failed_req, |
| 744 | px->state == PR_STRUN ? "OPEN" : |
Willy Tarreau | dcd4771 | 2007-11-04 23:35:08 +0100 | [diff] [blame] | 745 | px->state == PR_STIDLE ? "FULL" : "STOP", |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 746 | relative_pid, px->uuid, STATS_TYPE_FE, |
| 747 | read_freq_ctr(&px->fe_sess_per_sec)); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 748 | } |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 749 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 750 | if (buffer_write_chunk(rep, &msg) >= 0) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | s->data_ctx.stats.sv = px->srv; /* may be NULL */ |
| 755 | s->data_ctx.stats.px_st = DATA_ST_PX_SV; |
| 756 | /* fall through */ |
| 757 | |
| 758 | case DATA_ST_PX_SV: |
| 759 | /* stats.sv has been initialized above */ |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 760 | for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) { |
| 761 | |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 762 | int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB, 6=unchecked */ |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 763 | |
| 764 | sv = s->data_ctx.stats.sv; |
| 765 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 766 | if (s->data_ctx.stats.flags & STAT_BOUND) { |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 767 | if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV))) |
| 768 | break; |
| 769 | |
| 770 | if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid) |
| 771 | continue; |
| 772 | } |
| 773 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 774 | if (sv->tracked) |
| 775 | svs = sv->tracked; |
| 776 | else |
| 777 | svs = sv; |
| 778 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 779 | /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */ |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 780 | if (!(svs->state & SRV_CHECKED)) |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 781 | sv_state = 6; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 782 | else if (svs->state & SRV_RUNNING) { |
| 783 | if (svs->health == svs->rise + svs->fall - 1) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 784 | sv_state = 3; /* UP */ |
| 785 | else |
| 786 | sv_state = 2; /* going down */ |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 787 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 788 | if (svs->state & SRV_GOINGDOWN) |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 789 | sv_state += 2; |
| 790 | } |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 791 | else |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 792 | if (svs->health) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 793 | sv_state = 1; /* going up */ |
| 794 | else |
| 795 | sv_state = 0; /* DOWN */ |
| 796 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 797 | if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) { |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 798 | /* do not report servers which are DOWN */ |
| 799 | s->data_ctx.stats.sv = sv->next; |
| 800 | continue; |
| 801 | } |
| 802 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 803 | if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 804 | static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d ↑", |
| 805 | "UP %d/%d ↓", "UP", |
| 806 | "NOLB %d/%d ↓", "NOLB", |
| 807 | "<i>no check</i>" }; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 808 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 809 | /* name */ |
| 810 | "<tr align=\"center\" class=\"%s%d\"><td>%s</td>" |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 811 | /* queue : current, max, limit */ |
| 812 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td>" |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 813 | /* sessions : current, max, limit, rate, total, lbtot */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 814 | "<td align=right>%d</td><td align=right>%d</td>" |
| 815 | "<td align=right>%s</td><td align=right>%d</td>" |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 816 | "<td align=right>%d</td><td align=right>%d</td>" |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 817 | /* bytes : in, out */ |
| 818 | "<td align=right>%lld</td><td align=right>%lld</td>" |
| 819 | /* denied: req, resp */ |
| 820 | "<td align=right></td><td align=right>%d</td>" |
| 821 | /* errors : request, connect, response */ |
| 822 | "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n" |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 823 | /* warnings: retries, redispatches */ |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 824 | "<td align=right>%u</td><td align=right>%u</td>" |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 825 | "", |
| 826 | (sv->state & SRV_BACKUP) ? "backup" : "active", |
| 827 | sv_state, sv->id, |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 828 | sv->nbpend, sv->nbpend_max, LIM2A0(sv->maxqueue, "-"), |
Willy Tarreau | ddbb82f | 2007-12-05 10:34:49 +0100 | [diff] [blame] | 829 | sv->cur_sess, sv->cur_sess_max, LIM2A1(sv->maxconn, "-"), |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 830 | read_freq_ctr(&sv->sess_per_sec), |
Willy Tarreau | ddbb82f | 2007-12-05 10:34:49 +0100 | [diff] [blame] | 831 | sv->cum_sess, sv->cum_lbconn, |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 832 | sv->bytes_in, sv->bytes_out, |
| 833 | sv->failed_secu, |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 834 | sv->failed_conns, sv->failed_resp, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 835 | sv->retries, sv->redispatches); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 836 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 837 | /* status */ |
| 838 | chunk_printf(&msg, sizeof(trash), "<td nowrap>"); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 839 | |
| 840 | if (sv->state & SRV_CHECKED) |
| 841 | chunk_printf(&msg, sizeof(trash), "%s ", |
| 842 | human_time(now.tv_sec - sv->last_change, 1)); |
| 843 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 844 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 845 | srv_hlt_st[sv_state], |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 846 | (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health), |
| 847 | (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise)); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 848 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 849 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 850 | /* weight */ |
| 851 | "</td><td>%d</td>" |
| 852 | /* act, bck */ |
| 853 | "<td>%s</td><td>%s</td>" |
| 854 | "", |
Willy Tarreau | 5542af6 | 2007-12-03 02:04:00 +0100 | [diff] [blame] | 855 | (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv, |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 856 | (sv->state & SRV_BACKUP) ? "-" : "Y", |
| 857 | (sv->state & SRV_BACKUP) ? "Y" : "-"); |
| 858 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 859 | /* check failures: unique, fatal, down time */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 860 | if (sv->state & SRV_CHECKED) |
| 861 | chunk_printf(&msg, sizeof(trash), |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 862 | "<td align=right>%d</td><td align=right>%d</td>" |
| 863 | "<td nowrap align=right>%s</td>" |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 864 | "", |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 865 | svs->failed_checks, svs->down_trans, |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 866 | human_time(srv_downtime(sv), 1)); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 867 | else if (sv != svs) |
| 868 | chunk_printf(&msg, sizeof(trash), |
| 869 | "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id ); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 870 | else |
| 871 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 872 | "<td colspan=3></td>"); |
| 873 | |
| 874 | /* throttle */ |
| 875 | if ((sv->state & SRV_WARMINGUP) && |
| 876 | now.tv_sec < sv->last_change + sv->slowstart && |
| 877 | now.tv_sec >= sv->last_change) { |
| 878 | unsigned int ratio; |
| 879 | ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart); |
| 880 | chunk_printf(&msg, sizeof(trash), |
| 881 | "<td>%d %%</td></tr>\n", ratio); |
| 882 | } else { |
| 883 | chunk_printf(&msg, sizeof(trash), |
| 884 | "<td>-</td></tr>\n"); |
| 885 | } |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 886 | } else { |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 887 | static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,", |
| 888 | "UP %d/%d,", "UP,", |
| 889 | "NOLB %d/%d,", "NOLB,", |
| 890 | "no check," }; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 891 | chunk_printf(&msg, sizeof(trash), |
| 892 | /* pxid, name */ |
| 893 | "%s,%s," |
| 894 | /* queue : current, max */ |
| 895 | "%d,%d," |
Willy Tarreau | ddbb82f | 2007-12-05 10:34:49 +0100 | [diff] [blame] | 896 | /* sessions : current, max, limit, total */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 897 | "%d,%d,%s,%d," |
| 898 | /* bytes : in, out */ |
| 899 | "%lld,%lld," |
| 900 | /* denied: req, resp */ |
| 901 | ",%d," |
| 902 | /* errors : request, connect, response */ |
| 903 | ",%d,%d," |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 904 | /* warnings: retries, redispatches */ |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 905 | "%u,%u," |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 906 | "", |
| 907 | px->id, sv->id, |
| 908 | sv->nbpend, sv->nbpend_max, |
Willy Tarreau | dcd4771 | 2007-11-04 23:35:08 +0100 | [diff] [blame] | 909 | sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess, |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 910 | sv->bytes_in, sv->bytes_out, |
| 911 | sv->failed_secu, |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 912 | sv->failed_conns, sv->failed_resp, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 913 | sv->retries, sv->redispatches); |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 914 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 915 | /* status */ |
| 916 | chunk_printf(&msg, sizeof(trash), |
| 917 | srv_hlt_st[sv_state], |
| 918 | (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health), |
| 919 | (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise)); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 920 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 921 | chunk_printf(&msg, sizeof(trash), |
| 922 | /* weight, active, backup */ |
| 923 | "%d,%d,%d," |
| 924 | "", |
Willy Tarreau | 5542af6 | 2007-12-03 02:04:00 +0100 | [diff] [blame] | 925 | (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv, |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 926 | (sv->state & SRV_BACKUP) ? 0 : 1, |
| 927 | (sv->state & SRV_BACKUP) ? 1 : 0); |
| 928 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 929 | /* check failures: unique, fatal; last change, total downtime */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 930 | if (sv->state & SRV_CHECKED) |
| 931 | chunk_printf(&msg, sizeof(trash), |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 932 | "%d,%d,%d,%d,", |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 933 | sv->failed_checks, sv->down_trans, |
| 934 | now.tv_sec - sv->last_change, srv_downtime(sv)); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 935 | else |
| 936 | chunk_printf(&msg, sizeof(trash), |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 937 | ",,,,"); |
| 938 | |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 939 | /* queue limit, pid, iid, sid, */ |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 940 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | dcd4771 | 2007-11-04 23:35:08 +0100 | [diff] [blame] | 941 | "%s," |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 942 | "%d,%d,%d,", |
Willy Tarreau | dcd4771 | 2007-11-04 23:35:08 +0100 | [diff] [blame] | 943 | LIM2A0(sv->maxqueue, ""), |
| 944 | relative_pid, px->uuid, sv->puid); |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 945 | |
| 946 | /* throttle */ |
| 947 | if ((sv->state & SRV_WARMINGUP) && |
| 948 | now.tv_sec < sv->last_change + sv->slowstart && |
| 949 | now.tv_sec >= sv->last_change) { |
| 950 | unsigned int ratio; |
| 951 | ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart); |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 952 | chunk_printf(&msg, sizeof(trash), "%d", ratio); |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 953 | } |
| 954 | |
Willy Tarreau | ddbb82f | 2007-12-05 10:34:49 +0100 | [diff] [blame] | 955 | /* sessions: lbtot */ |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 956 | chunk_printf(&msg, sizeof(trash), ",%d,", sv->cum_lbconn); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 957 | |
| 958 | /* tracked */ |
| 959 | if (sv->tracked) |
Krzysztof Piotr Oledzki | f58a962 | 2008-02-23 01:19:10 +0100 | [diff] [blame] | 960 | chunk_printf(&msg, sizeof(trash), "%s/%s,", |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 961 | sv->tracked->proxy->id, sv->tracked->id); |
| 962 | else |
| 963 | chunk_printf(&msg, sizeof(trash), ","); |
| 964 | |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 965 | /* type */ |
| 966 | chunk_printf(&msg, sizeof(trash), "%d,", STATS_TYPE_SV); |
| 967 | |
| 968 | /* rate */ |
| 969 | chunk_printf(&msg, sizeof(trash), "%u,", read_freq_ctr(&sv->sess_per_sec)); |
| 970 | |
| 971 | /* finish with EOL */ |
| 972 | chunk_printf(&msg, sizeof(trash), "\n"); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 973 | } |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 974 | if (buffer_write_chunk(rep, &msg) >= 0) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 975 | return 0; |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 976 | } /* for sv */ |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 977 | |
| 978 | s->data_ctx.stats.px_st = DATA_ST_PX_BE; |
| 979 | /* fall through */ |
| 980 | |
| 981 | case DATA_ST_PX_BE: |
| 982 | /* print the backend */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 983 | if ((px->cap & PR_CAP_BE) && |
| 984 | (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) { |
| 985 | if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 986 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 987 | /* name */ |
| 988 | "<tr align=center class=\"backend\"><td>Backend</td>" |
| 989 | /* queue : current, max */ |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 990 | "<td align=right>%d</td><td align=right>%d</td><td></td>" |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 991 | /* sessions : current, max, limit, rate, total, lbtot */ |
Willy Tarreau | ddbb82f | 2007-12-05 10:34:49 +0100 | [diff] [blame] | 992 | "<td align=right>%d</td><td align=right>%d</td>" |
| 993 | "<td align=right>%d</td><td align=right>%d</td>" |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 994 | "<td align=right>%d</td><td align=right>%d</td>" |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 995 | /* bytes : in, out */ |
| 996 | "<td align=right>%lld</td><td align=right>%lld</td>" |
| 997 | /* denied: req, resp */ |
| 998 | "<td align=right>%d</td><td align=right>%d</td>" |
| 999 | /* errors : request, connect, response */ |
| 1000 | "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n" |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 1001 | /* warnings: retries, redispatches */ |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 1002 | "<td align=right>%u</td><td align=right>%u</td>" |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1003 | /* backend status: reflect backend status (up/down): we display UP |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1004 | * if the backend has known working servers or if it has no server at |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1005 | * all (eg: for stats). Then we display the total weight, number of |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1006 | * active and backups. */ |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1007 | "<td align=center nowrap>%s %s</td><td align=center>%d</td>" |
| 1008 | "<td align=center>%d</td><td align=center>%d</td>", |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1009 | px->nbpend /* or px->totpend ? */, px->nbpend_max, |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 1010 | px->beconn, px->beconn_max, px->fullconn, |
| 1011 | read_freq_ctr(&px->be_sess_per_sec), |
| 1012 | px->cum_beconn, px->cum_lbconn, |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1013 | px->bytes_in, px->bytes_out, |
| 1014 | px->denied_req, px->denied_resp, |
| 1015 | px->failed_conns, px->failed_resp, |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 1016 | px->retries, px->redispatches, |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1017 | human_time(now.tv_sec - px->last_change, 1), |
Willy Tarreau | 2ea8193 | 2007-11-30 12:04:38 +0100 | [diff] [blame] | 1018 | (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : |
| 1019 | "<font color=\"red\"><b>DOWN</b></font>", |
Willy Tarreau | 5542af6 | 2007-12-03 02:04:00 +0100 | [diff] [blame] | 1020 | (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv, |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 1021 | px->srv_act, px->srv_bck); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1022 | |
| 1023 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 1024 | /* rest of backend: nothing, down transitions, total downtime, throttle */ |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1025 | "<td align=center> </td><td align=\"right\">%d</td>" |
| 1026 | "<td align=\"right\" nowrap>%s</td>" |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 1027 | "<td></td>" |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1028 | "</tr>", |
| 1029 | px->down_trans, |
| 1030 | px->srv?human_time(be_downtime(px), 1):" "); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 1031 | } else { |
| 1032 | chunk_printf(&msg, sizeof(trash), |
| 1033 | /* pxid, name */ |
| 1034 | "%s,BACKEND," |
| 1035 | /* queue : current, max */ |
| 1036 | "%d,%d," |
Willy Tarreau | ddbb82f | 2007-12-05 10:34:49 +0100 | [diff] [blame] | 1037 | /* sessions : current, max, limit, total */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 1038 | "%d,%d,%d,%d," |
| 1039 | /* bytes : in, out */ |
| 1040 | "%lld,%lld," |
| 1041 | /* denied: req, resp */ |
| 1042 | "%d,%d," |
| 1043 | /* errors : request, connect, response */ |
| 1044 | ",%d,%d," |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 1045 | /* warnings: retries, redispatches */ |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 1046 | "%u,%u," |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1047 | /* backend status: reflect backend status (up/down): we display UP |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 1048 | * if the backend has known working servers or if it has no server at |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1049 | * all (eg: for stats). Then we display the total weight, number of |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 1050 | * active and backups. */ |
| 1051 | "%s," |
| 1052 | "%d,%d,%d," |
Willy Tarreau | 4bab24d | 2007-11-30 18:16:29 +0100 | [diff] [blame] | 1053 | /* rest of backend: nothing, down transitions, last change, total downtime */ |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 1054 | ",%d,%d,%d,," |
Krzysztof Piotr Oledzki | 2c6962c | 2008-03-02 02:42:14 +0100 | [diff] [blame] | 1055 | /* pid, iid, sid, throttle, lbtot, tracked, type */ |
| 1056 | "%d,%d,0,,%d,,%d," |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 1057 | /* rate */ |
| 1058 | "%u," |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 1059 | "\n", |
| 1060 | px->id, |
| 1061 | px->nbpend /* or px->totpend ? */, px->nbpend_max, |
| 1062 | px->beconn, px->beconn_max, px->fullconn, px->cum_beconn, |
| 1063 | px->bytes_in, px->bytes_out, |
| 1064 | px->denied_req, px->denied_resp, |
| 1065 | px->failed_conns, px->failed_resp, |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 1066 | px->retries, px->redispatches, |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 1067 | (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN", |
Willy Tarreau | 5542af6 | 2007-12-03 02:04:00 +0100 | [diff] [blame] | 1068 | (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv, |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 1069 | px->srv_act, px->srv_bck, |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1070 | px->down_trans, now.tv_sec - px->last_change, |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 1071 | px->srv?be_downtime(px):0, |
Willy Tarreau | ddbb82f | 2007-12-05 10:34:49 +0100 | [diff] [blame] | 1072 | relative_pid, px->uuid, |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 1073 | px->cum_lbconn, STATS_TYPE_BE, |
| 1074 | read_freq_ctr(&px->be_sess_per_sec)); |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 1075 | } |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 1076 | if (buffer_write_chunk(rep, &msg) >= 0) |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1077 | return 0; |
| 1078 | } |
Willy Tarreau | b1356cf | 2008-12-07 16:06:43 +0100 | [diff] [blame] | 1079 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1080 | s->data_ctx.stats.px_st = DATA_ST_PX_END; |
| 1081 | /* fall through */ |
| 1082 | |
| 1083 | case DATA_ST_PX_END: |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 1084 | if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 1085 | chunk_printf(&msg, sizeof(trash), "</table><p>\n"); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1086 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 1087 | if (buffer_write_chunk(rep, &msg) >= 0) |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 1088 | return 0; |
| 1089 | } |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1090 | |
| 1091 | s->data_ctx.stats.px_st = DATA_ST_PX_FIN; |
| 1092 | /* fall through */ |
| 1093 | |
| 1094 | case DATA_ST_PX_FIN: |
| 1095 | return 1; |
| 1096 | |
| 1097 | default: |
| 1098 | /* unknown state, we should put an abort() here ! */ |
| 1099 | return 1; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1103 | |
| 1104 | /* This function is called to send output to the response buffer. |
| 1105 | * It dumps the sessions states onto the output buffer <rep>. |
| 1106 | * Expects to be called with client socket shut down on input. |
| 1107 | * s->data_ctx must have been zeroed first, and the flags properly set. |
| 1108 | * It automatically clears the HIJACK bit from the response buffer. |
| 1109 | */ |
| 1110 | void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep) |
| 1111 | { |
| 1112 | struct chunk msg; |
| 1113 | |
| 1114 | if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) { |
| 1115 | /* If we're forced to shut down, we might have to remove our |
| 1116 | * reference to the last session being dumped. |
| 1117 | */ |
| 1118 | if (s->data_state == DATA_ST_LIST) { |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 1119 | if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) { |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1120 | LIST_DEL(&s->data_ctx.sess.bref.users); |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 1121 | LIST_INIT(&s->data_ctx.sess.bref.users); |
| 1122 | } |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1123 | } |
| 1124 | s->data_state = DATA_ST_FIN; |
| 1125 | buffer_stop_hijack(rep); |
| 1126 | s->ana_state = STATS_ST_CLOSE; |
| 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | if (s->ana_state != STATS_ST_REP) |
| 1131 | return; |
| 1132 | |
| 1133 | msg.len = 0; |
| 1134 | msg.str = trash; |
| 1135 | |
| 1136 | switch (s->data_state) { |
| 1137 | case DATA_ST_INIT: |
| 1138 | /* the function had not been called yet, let's prepare the |
| 1139 | * buffer for a response. We initialize the current session |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 1140 | * pointer to the first in the global list. When a target |
| 1141 | * session is being destroyed, it is responsible for updating |
| 1142 | * this pointer. We know we have reached the end when this |
| 1143 | * pointer points back to the head of the sessions list. |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1144 | */ |
| 1145 | stream_int_retnclose(rep->cons, &msg); |
| 1146 | LIST_INIT(&s->data_ctx.sess.bref.users); |
| 1147 | s->data_ctx.sess.bref.ref = sessions.n; |
| 1148 | s->data_state = DATA_ST_LIST; |
| 1149 | /* fall through */ |
| 1150 | |
| 1151 | case DATA_ST_LIST: |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 1152 | /* first, let's detach the back-ref from a possible previous session */ |
| 1153 | if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) { |
| 1154 | LIST_DEL(&s->data_ctx.sess.bref.users); |
| 1155 | LIST_INIT(&s->data_ctx.sess.bref.users); |
| 1156 | } |
| 1157 | |
| 1158 | /* and start from where we stopped */ |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1159 | while (s->data_ctx.sess.bref.ref != &sessions) { |
| 1160 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1161 | struct session *curr_sess; |
| 1162 | |
| 1163 | curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list); |
| 1164 | |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1165 | chunk_printf(&msg, sizeof(trash), |
| 1166 | "%p: proto=%s", |
| 1167 | curr_sess, |
| 1168 | curr_sess->listener->proto->name); |
| 1169 | |
| 1170 | switch (curr_sess->listener->proto->sock_family) { |
| 1171 | case AF_INET: |
| 1172 | inet_ntop(AF_INET, |
| 1173 | (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr, |
| 1174 | pn, sizeof(pn)); |
| 1175 | |
| 1176 | chunk_printf(&msg, sizeof(trash), |
| 1177 | " src=%s:%d fe=%s be=%s srv=%s", |
| 1178 | pn, |
| 1179 | ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port), |
| 1180 | curr_sess->fe->id, |
| 1181 | curr_sess->be->id, |
| 1182 | curr_sess->srv ? curr_sess->srv->id : "<none>" |
| 1183 | ); |
| 1184 | break; |
| 1185 | case AF_INET6: |
| 1186 | inet_ntop(AF_INET6, |
| 1187 | (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr, |
| 1188 | pn, sizeof(pn)); |
| 1189 | |
| 1190 | chunk_printf(&msg, sizeof(trash), |
| 1191 | " src=%s:%d fe=%s be=%s srv=%s", |
| 1192 | pn, |
| 1193 | ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port), |
| 1194 | curr_sess->fe->id, |
| 1195 | curr_sess->be->id, |
| 1196 | curr_sess->srv ? curr_sess->srv->id : "<none>" |
| 1197 | ); |
| 1198 | |
| 1199 | break; |
| 1200 | case AF_UNIX: |
| 1201 | /* no more information to print right now */ |
| 1202 | break; |
| 1203 | } |
| 1204 | |
| 1205 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 8a5c626 | 2008-12-08 00:16:21 +0100 | [diff] [blame] | 1206 | " si=(%d,%d) as=%d ts=%02x age=%s", |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1207 | curr_sess->si[0].state, curr_sess->si[1].state, |
Willy Tarreau | 8a5c626 | 2008-12-08 00:16:21 +0100 | [diff] [blame] | 1208 | curr_sess->ana_state, curr_sess->task->state, |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1209 | human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1)); |
| 1210 | |
Willy Tarreau | 8a5c626 | 2008-12-08 00:16:21 +0100 | [diff] [blame] | 1211 | if (curr_sess->task->state & TASK_IN_RUNQUEUE) |
| 1212 | chunk_printf(&msg, sizeof(trash), " run(nice=%d)\n", curr_sess->task->nice); |
| 1213 | else |
| 1214 | chunk_printf(&msg, sizeof(trash), |
| 1215 | " exp=%s\n", |
| 1216 | curr_sess->task->expire ? |
| 1217 | human_time(TICKS_TO_MS(tick_remain(now_ms, curr_sess->task->expire)), |
| 1218 | TICKS_TO_MS(1000)) |
| 1219 | : "never"); |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1220 | |
| 1221 | if (buffer_write_chunk(rep, &msg) >= 0) { |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 1222 | /* let's try again later from this session. We add ourselves into |
| 1223 | * this session's users so that it can remove us upon termination. |
| 1224 | */ |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1225 | LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users); |
| 1226 | return; |
| 1227 | } |
| 1228 | |
| 1229 | s->data_ctx.sess.bref.ref = curr_sess->list.n; |
| 1230 | } |
| 1231 | s->data_state = DATA_ST_FIN; |
| 1232 | /* fall through */ |
| 1233 | |
| 1234 | default: |
| 1235 | s->data_state = DATA_ST_FIN; |
| 1236 | buffer_stop_hijack(rep); |
| 1237 | s->ana_state = STATS_ST_CLOSE; |
| 1238 | return; |
| 1239 | } |
| 1240 | } |
| 1241 | |
Willy Tarreau | 74808cb | 2009-03-04 15:53:18 +0100 | [diff] [blame] | 1242 | /* print a line of error buffer (limited to 70 bytes) to <out>. The format is : |
| 1243 | * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n> |
| 1244 | * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are |
| 1245 | * encoded in C format. Other non-printable chars are encoded "\xHH". Original |
| 1246 | * lines are respected within the limit of 70 output chars. Lines that are |
| 1247 | * continuation of a previous truncated line begin with "+" instead of " " |
| 1248 | * after the offset. The new pointer is returned. |
| 1249 | */ |
| 1250 | static int dump_error_line(struct chunk *out, int size, |
| 1251 | struct error_snapshot *err, int *line, int ptr) |
| 1252 | { |
| 1253 | int end; |
| 1254 | unsigned char c; |
| 1255 | |
| 1256 | end = out->len + 80; |
| 1257 | if (end > size) |
| 1258 | return ptr; |
| 1259 | |
| 1260 | chunk_printf(out, size, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+'); |
| 1261 | |
| 1262 | while (ptr < err->len) { |
| 1263 | c = err->buf[ptr]; |
| 1264 | if (isprint(c)) { |
| 1265 | if (out->len > end - 2) |
| 1266 | break; |
| 1267 | out->str[out->len++] = c; |
| 1268 | } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e') { |
| 1269 | if (out->len > end - 3) |
| 1270 | break; |
| 1271 | out->str[out->len++] = '\\'; |
| 1272 | switch (c) { |
| 1273 | case '\t': c = 't'; break; |
| 1274 | case '\n': c = 'n'; break; |
| 1275 | case '\r': c = 'r'; break; |
| 1276 | case '\e': c = 'e'; break; |
| 1277 | } |
| 1278 | out->str[out->len++] = c; |
| 1279 | } else { |
| 1280 | if (out->len > end - 5) |
| 1281 | break; |
| 1282 | out->str[out->len++] = '\\'; |
| 1283 | out->str[out->len++] = 'x'; |
| 1284 | out->str[out->len++] = hextab[(c >> 4) & 0xF]; |
| 1285 | out->str[out->len++] = hextab[c & 0xF]; |
| 1286 | } |
| 1287 | if (err->buf[ptr++] == '\n') { |
| 1288 | /* we had a line break, let's return now */ |
| 1289 | out->str[out->len++] = '\n'; |
| 1290 | *line = ptr; |
| 1291 | return ptr; |
| 1292 | } |
| 1293 | } |
| 1294 | /* we have an incomplete line, we return it as-is */ |
| 1295 | out->str[out->len++] = '\n'; |
| 1296 | return ptr; |
| 1297 | } |
| 1298 | |
| 1299 | /* This function is called to send output to the response buffer. |
| 1300 | * It dumps the errors logged in proxies onto the output buffer <rep>. |
| 1301 | * Expects to be called with client socket shut down on input. |
| 1302 | * s->data_ctx must have been zeroed first, and the flags properly set. |
| 1303 | * It automatically clears the HIJACK bit from the response buffer. |
| 1304 | */ |
| 1305 | void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep) |
| 1306 | { |
| 1307 | extern const char *monthname[12]; |
| 1308 | struct chunk msg; |
| 1309 | |
| 1310 | if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) { |
| 1311 | s->data_state = DATA_ST_FIN; |
| 1312 | buffer_stop_hijack(rep); |
| 1313 | s->ana_state = STATS_ST_CLOSE; |
| 1314 | return; |
| 1315 | } |
| 1316 | |
| 1317 | if (s->ana_state != STATS_ST_REP) |
| 1318 | return; |
| 1319 | |
| 1320 | msg.len = 0; |
| 1321 | msg.str = trash; |
| 1322 | |
| 1323 | if (!s->data_ctx.errors.px) { |
| 1324 | /* the function had not been called yet, let's prepare the |
| 1325 | * buffer for a response. |
| 1326 | */ |
| 1327 | stream_int_retnclose(rep->cons, &msg); |
| 1328 | s->data_ctx.errors.px = proxy; |
| 1329 | s->data_ctx.errors.buf = 0; |
| 1330 | s->data_ctx.errors.bol = 0; |
| 1331 | s->data_ctx.errors.ptr = -1; |
| 1332 | } |
| 1333 | |
| 1334 | /* we have two inner loops here, one for the proxy, the other one for |
| 1335 | * the buffer. |
| 1336 | */ |
| 1337 | while (s->data_ctx.errors.px) { |
| 1338 | struct error_snapshot *es; |
| 1339 | |
| 1340 | if (s->data_ctx.errors.buf == 0) |
| 1341 | es = &s->data_ctx.errors.px->invalid_req; |
| 1342 | else |
| 1343 | es = &s->data_ctx.errors.px->invalid_rep; |
| 1344 | |
| 1345 | if (!es->when.tv_sec) |
| 1346 | goto next; |
| 1347 | |
| 1348 | if (s->data_ctx.errors.iid >= 0 && |
| 1349 | s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid && |
| 1350 | es->oe->uuid != s->data_ctx.errors.iid) |
| 1351 | goto next; |
| 1352 | |
| 1353 | if (s->data_ctx.errors.ptr < 0) { |
| 1354 | /* just print headers now */ |
| 1355 | |
| 1356 | char pn[INET6_ADDRSTRLEN]; |
| 1357 | struct tm tm; |
| 1358 | |
| 1359 | get_localtime(es->when.tv_sec, &tm); |
| 1360 | chunk_printf(&msg, sizeof(trash), "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 1361 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 1362 | tm.tm_hour, tm.tm_min, tm.tm_sec, es->when.tv_usec/1000); |
| 1363 | |
| 1364 | |
| 1365 | if (es->src.ss_family == AF_INET) |
| 1366 | inet_ntop(AF_INET, |
| 1367 | (const void *)&((struct sockaddr_in *)&es->src)->sin_addr, |
| 1368 | pn, sizeof(pn)); |
| 1369 | else |
| 1370 | inet_ntop(AF_INET6, |
| 1371 | (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr, |
| 1372 | pn, sizeof(pn)); |
| 1373 | |
| 1374 | switch (s->data_ctx.errors.buf) { |
| 1375 | case 0: |
| 1376 | chunk_printf(&msg, sizeof(trash), |
| 1377 | " frontend %s (#%d): invalid request\n" |
| 1378 | " src %s, session #%d, backend %s (#%d), server %s (#%d)\n" |
| 1379 | " request length %d bytes, error at position %d:\n\n", |
| 1380 | s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid, |
| 1381 | pn, es->sid, es->oe->id, es->oe->uuid, |
| 1382 | es->srv ? es->srv->id : "<NONE>", |
| 1383 | es->srv ? es->srv->puid : -1, |
| 1384 | es->len, es->pos); |
| 1385 | break; |
| 1386 | case 1: |
| 1387 | chunk_printf(&msg, sizeof(trash), |
| 1388 | " backend %s (#%d) : invalid response\n" |
| 1389 | " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n" |
| 1390 | " response length %d bytes, error at position %d:\n\n", |
| 1391 | s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid, |
| 1392 | pn, es->sid, es->oe->id, es->oe->uuid, |
| 1393 | es->srv ? es->srv->id : "<NONE>", |
| 1394 | es->srv ? es->srv->puid : -1, |
| 1395 | es->len, es->pos); |
| 1396 | break; |
| 1397 | } |
| 1398 | |
| 1399 | if (buffer_write_chunk(rep, &msg) >= 0) { |
| 1400 | /* Socket buffer full. Let's try again later from the same point */ |
| 1401 | return; |
| 1402 | } |
| 1403 | s->data_ctx.errors.ptr = 0; |
| 1404 | s->data_ctx.errors.sid = es->sid; |
| 1405 | } |
| 1406 | |
| 1407 | if (s->data_ctx.errors.sid != es->sid) { |
| 1408 | /* the snapshot changed while we were dumping it */ |
| 1409 | chunk_printf(&msg, sizeof(trash), |
| 1410 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
| 1411 | if (buffer_write_chunk(rep, &msg) >= 0) |
| 1412 | return; |
| 1413 | goto next; |
| 1414 | } |
| 1415 | |
| 1416 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 1417 | while (s->data_ctx.errors.ptr < es->len) { |
| 1418 | int newptr; |
| 1419 | int newline; |
| 1420 | |
| 1421 | newline = s->data_ctx.errors.bol; |
| 1422 | newptr = dump_error_line(&msg, sizeof(trash), es, &newline, s->data_ctx.errors.ptr); |
| 1423 | if (newptr == s->data_ctx.errors.ptr) |
| 1424 | return; |
| 1425 | |
| 1426 | if (buffer_write_chunk(rep, &msg) >= 0) { |
| 1427 | /* Socket buffer full. Let's try again later from the same point */ |
| 1428 | return; |
| 1429 | } |
| 1430 | s->data_ctx.errors.ptr = newptr; |
| 1431 | s->data_ctx.errors.bol = newline; |
| 1432 | }; |
| 1433 | next: |
| 1434 | s->data_ctx.errors.bol = 0; |
| 1435 | s->data_ctx.errors.ptr = -1; |
| 1436 | s->data_ctx.errors.buf++; |
| 1437 | if (s->data_ctx.errors.buf > 1) { |
| 1438 | s->data_ctx.errors.buf = 0; |
| 1439 | s->data_ctx.errors.px = s->data_ctx.errors.px->next; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | /* dump complete */ |
| 1444 | buffer_stop_hijack(rep); |
| 1445 | s->ana_state = STATS_ST_CLOSE; |
| 1446 | } |
| 1447 | |
Willy Tarreau | 3dfe6cd | 2008-12-07 22:29:48 +0100 | [diff] [blame] | 1448 | |
Willy Tarreau | 10522fd | 2008-07-09 20:12:41 +0200 | [diff] [blame] | 1449 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 1450 | { CFG_GLOBAL, "stats", stats_parse_global }, |
| 1451 | { 0, NULL, NULL }, |
| 1452 | }}; |
| 1453 | |
| 1454 | __attribute__((constructor)) |
| 1455 | static void __dumpstats_module_init(void) |
| 1456 | { |
| 1457 | cfg_register_keywords(&cfg_kws); |
| 1458 | } |
| 1459 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1460 | /* |
| 1461 | * Local variables: |
| 1462 | * c-indent-level: 8 |
| 1463 | * c-basic-offset: 8 |
| 1464 | * End: |
| 1465 | */ |