From: Date: Wed, 19 Sep 2007 13:00:48 +0000 (+0200) Subject: Adding memstat framework. X-Git-Tag: 0.2~19 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=6030db2543337976c1df861272ce90b25ef83820;hp=-c;p=awesomized%2Flibmemcached Adding memstat framework. Rewrote server parsing routines. --- 6030db2543337976c1df861272ce90b25ef83820 diff --git a/src/Makefile.am b/src/Makefile.am index fc47cc49..d4cf2e69 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,10 +1,15 @@ INCLUDES = -I$(top_builddir)/include LDADDS = ../lib/libmemcached.la -bin_PROGRAMS = memcat memcp +bin_PROGRAMS = memcat memcp memstat -memcat_SOURCES = memcat.c +noinst_includes= client_options.h utilities.h + +memcat_SOURCES = memcat.c utilities.c memcat_LDADD = $(LDADDS) -memcp_SOURCES = memcp.c +memcp_SOURCES = memcp.c utilities.c memcp_LDADD = $(LDADDS) + +memstat_SOURCES = memstat.c utilities.c +memstat_LDADD = $(LDADDS) diff --git a/src/memcat.c b/src/memcat.c index 645b54ac..dedcb945 100644 --- a/src/memcat.c +++ b/src/memcat.c @@ -2,25 +2,18 @@ #include #include #include + #include "client_options.h" +#include "utilities.h" + + +/* Prototypes */ +void options_parse(int argc, char *argv[]); static int opt_verbose; static int opt_displayflag; static char *opt_servers; -struct memcached_st *parse_opt_servers (struct memcached_st *m, - char *opt_servers) -{ - char *s, *hostname; - unsigned int portnum; - while (s = strsep(&opt_servers, ",")) { - hostname = strsep(&s, ":"); - portnum = atoi(s); - memcached_server_add(m, hostname, portnum); - } - return m; -} - int main(int argc, char *argv[]) { memcached_st *memc; @@ -30,6 +23,40 @@ int main(int argc, char *argv[]) memcached_return rc; unsigned int x; + memc= memcached_init(NULL); + + options_parse(argc, argv); + + parse_opt_servers(memc, opt_servers); + + for (x= 1; x < argc; x++) + { + string= memcached_get(memc, argv[x], strlen(argv[x]), + &string_length, &flags, &rc); + if (rc == MEMCACHED_SUCCESS) { + if (opt_displayflag) { + printf("%d\n", flags); + } else { + if (string) + { + printf("%.*s\n", string_length, string); + free(string); + } + } + } + } + + memcached_deinit(memc); + + return 0; +}; + + +void options_parse(int argc, char *argv[]) +{ + int option_index = 0; + int option_rv; + static struct option long_options[] = { {"version", no_argument, NULL, OPT_VERSION}, @@ -40,8 +67,6 @@ int main(int argc, char *argv[]) {"flag", no_argument, &opt_displayflag, OPT_FLAG}, {0, 0, 0, 0}, }; - int option_index = 0; - int option_rv; while (1) { @@ -59,7 +84,7 @@ int main(int argc, char *argv[]) exit(0); break; case OPT_SERVERS: /* --servers */ - opt_servers = strdup(optarg); + opt_servers= strdup(optarg); break; case '?': /* getopt_long already printed an error message. */ @@ -68,30 +93,4 @@ int main(int argc, char *argv[]) abort(); } } - - memc = malloc(sizeof(struct memcached_st)); - memcached_init(memc); - memc= parse_opt_servers(memc, opt_servers); - memc= memcached_init(memc); - - for (x= 1; x < argc; x++) - { - string= memcached_get(memc, argv[x], strlen(argv[x]), - &string_length, &flags, &rc); - if (rc == MEMCACHED_SUCCESS) { - if (opt_displayflag) { - printf("%d\n", flags); - } else { - if (string) - { - printf("%.*s\n", string_length, string); - free(string); - } - } - } - } - - memcached_deinit(memc); - - return 0; -}; +} diff --git a/src/memstat.c b/src/memstat.c new file mode 100644 index 00000000..939f806b --- /dev/null +++ b/src/memstat.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "client_options.h" +#include "utilities.h" + +/* Prototypes */ +void options_parse(int argc, char *argv[]); + +static int opt_verbose; +static int opt_displayflag; +static char *opt_servers; + +int main(int argc, char *argv[]) +{ + memcached_st *memc; + + memc= memcached_init(NULL); + options_parse(argc, argv); + + parse_opt_servers(memc, opt_servers); + + memcached_deinit(memc); + + return 0; +} + +void options_parse(int argc, char *argv[]) +{ + static struct option long_options[] = + { + {"version", no_argument, NULL, OPT_VERSION}, + {"help", no_argument, NULL, OPT_HELP}, + {"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {"debug", no_argument, &opt_verbose, OPT_DEBUG}, + {"servers", required_argument, NULL, OPT_SERVERS}, + {"flag", no_argument, &opt_displayflag, OPT_FLAG}, + {0, 0, 0, 0}, + }; + + int option_index = 0; + int option_rv; + + while (1) + { + option_rv = getopt_long(argc, argv, "", long_options, &option_index); + if (option_rv == -1) break; + switch (option_rv) { + case 0: + break; + case OPT_VERSION: /* --version */ + printf("memcache tools, memcat, v1.0\n"); + exit(0); + break; + case OPT_HELP: /* --help */ + printf("useful help messages go here\n"); + exit(0); + break; + case OPT_SERVERS: /* --servers */ + opt_servers = strdup(optarg); + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } +} diff --git a/src/utilities.c b/src/utilities.c new file mode 100644 index 00000000..5dd66f62 --- /dev/null +++ b/src/utilities.c @@ -0,0 +1,47 @@ +#include + +void parse_opt_servers(memcached_st *memc, + char *opt_servers) +{ + char *string; + unsigned int port; + char *begin_ptr; + + for (begin_ptr= opt_servers, string= index(opt_servers, ','); + begin_ptr[0]; + begin_ptr= ++string, string= index(begin_ptr, ',')) + { + char buffer[HUGE_STRING_LEN]; + char *ptr; + + memset(buffer, 0, HUGE_STRING_LEN); + if (string) + { + memcpy(buffer, begin_ptr, string - begin_ptr); + } + else + { + size_t length= strlen(opt_servers); + memcpy(buffer, begin_ptr, length); + string= opt_servers+length; + } + + ptr= index(buffer, ':'); + + if (ptr) + { + ptr[0]= 0; + + ptr++; + + port= strtol(ptr, (char **)NULL, 10); + + memcached_server_add(memc, buffer, port); + } + else + { + memcached_server_add(memc, buffer, 0); + } + } +} + diff --git a/src/utilities.h b/src/utilities.h new file mode 100644 index 00000000..fe797817 --- /dev/null +++ b/src/utilities.h @@ -0,0 +1,4 @@ +#include + +void parse_opt_servers (memcached_st *ptr, + char *opt_servers);