From 1e029aa9656cc6a171bead62b68b0c7bdab8a009 Mon Sep 17 00:00:00 2001 From: Emeric Brun Date: Thu, 23 Sep 2010 18:12:53 +0200 Subject: [PATCH] [MINOR] Manage all types (ip, integer, string, binary) on cli "show table" command --- src/dumpstats.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/src/dumpstats.c b/src/dumpstats.c index 7745f8f35..269518456 100644 --- a/src/dumpstats.c +++ b/src/dumpstats.c @@ -3063,6 +3063,75 @@ int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep) } } + +/* print a string of text buffer to . The format is : + * Non-printable chars \t, \n, \r and \e are * encoded in C format. + * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped. + * Print stopped if null char or is reached, or if no more place in the chunk. + */ + +static int dump_text(struct chunk *out, const char *buf, int bsize) +{ + unsigned char c; + int ptr = 0; + + while (buf[ptr] && ptr < bsize) { + c = buf[ptr]; + if (isprint(c) && isascii(c) && c != '\\' && c != ' ') { + if (out->len > out->size - 1) + break; + out->str[out->len++] = c; + } + else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') { + if (out->len > out->size - 2) + break; + out->str[out->len++] = '\\'; + switch (c) { + case ' ': c = ' '; break; + case '\t': c = 't'; break; + case '\n': c = 'n'; break; + case '\r': c = 'r'; break; + case '\e': c = 'e'; break; + case '\\': c = '\\'; break; + } + out->str[out->len++] = c; + } + else { + if (out->len > out->size - 4) + break; + out->str[out->len++] = '\\'; + out->str[out->len++] = 'x'; + out->str[out->len++] = hextab[(c >> 4) & 0xF]; + out->str[out->len++] = hextab[c & 0xF]; + } + ptr++; + } + + return ptr; +} + +/* print a buffer in hexa. + * Print stopped if is reached, or if no more place in the chunk. + */ + +static int dump_binary(struct chunk *out, const char *buf, int bsize) +{ + unsigned char c; + int ptr = 0; + + while (ptr < bsize) { + c = buf[ptr]; + + if (out->len > out->size - 2) + break; + out->str[out->len++] = hextab[(c >> 4) & 0xF]; + out->str[out->len++] = hextab[c & 0xF]; + + ptr++; + } + return ptr; +} + /* This function is called to send output to the response buffer. * It dumps the tables states onto the output buffer . * Expects to be called with client socket shut down on input. @@ -3118,9 +3187,9 @@ int stats_dump_table_to_buffer(struct session *s, struct buffer *rep) } if (s->data_ctx.table.proxy->table.size) { - chunk_printf(&msg, "# table: %s, type: %ld, size:%d, used:%d\n", + chunk_printf(&msg, "# table: %s, type: %s, size:%d, used:%d\n", s->data_ctx.table.proxy->id, - s->data_ctx.table.proxy->table.type, + stktable_types[s->data_ctx.table.proxy->table.type].kw, s->data_ctx.table.proxy->table.size, s->data_ctx.table.proxy->table.current); @@ -3201,8 +3270,17 @@ int stats_dump_table_to_buffer(struct session *s, struct buffer *rep) addr, sizeof(addr)); chunk_printf(&msg, " key=%s", addr); } - else - chunk_printf(&msg, " key=?"); + else if (s->data_ctx.table.proxy->table.type == STKTABLE_TYPE_INTEGER) { + chunk_printf(&msg, " key=%u", *(unsigned int *)s->data_ctx.table.entry->key.key); + } + else if (s->data_ctx.table.proxy->table.type == STKTABLE_TYPE_STRING) { + chunk_printf(&msg, " key="); + dump_text(&msg, (const char *)s->data_ctx.table.entry->key.key, s->data_ctx.table.proxy->table.key_size); + } + else { + chunk_printf(&msg, " key="); + dump_binary(&msg, (const char *)s->data_ctx.table.entry->key.key, s->data_ctx.table.proxy->table.key_size); + } chunk_printf(&msg, " use=%d exp=%d", s->data_ctx.table.entry->ref_cnt - 1,