10 #include <libmemcached/memcached.h>
12 #include "client_options.h"
13 #include "utilities.h"
15 #define PROGRAM_NAME "memstat"
16 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
19 static void options_parse(int argc
, char *argv
[]);
20 static void print_server_listing(memcached_st
*memc
, memcached_stat_st
*stat
,
21 memcached_server_st
*server_list
);
23 static int opt_verbose
= 0;
24 static int opt_displayflag
= 0;
25 static char *opt_servers
= NULL
;
27 static struct option long_options
[]=
29 {"version", no_argument
, NULL
, OPT_VERSION
},
30 {"help", no_argument
, NULL
, OPT_HELP
},
31 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
32 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
33 {"servers", required_argument
, NULL
, OPT_SERVERS
},
34 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
38 int main(int argc
, char *argv
[])
42 memcached_stat_st
*stat
;
43 memcached_server_st
*servers
;
44 memcached_server_st
*server_list
;
46 options_parse(argc
, argv
);
52 if ((temp
= getenv("MEMCACHED_SERVERS")))
53 opt_servers
= strdup(temp
);
56 fprintf(stderr
, "No Servers provided\n\n");
57 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, 0);
62 memc
= memcached_create(NULL
);
64 servers
= memcached_servers_parse(opt_servers
);
65 memcached_server_push(memc
, servers
);
66 memcached_server_list_free(servers
);
68 stat
= memcached_stat(memc
, NULL
, &rc
);
70 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_SOME_ERRORS
)
72 printf("Failure to communicate with servers (%s)\n",
73 memcached_strerror(memc
, rc
));
77 server_list
= memcached_server_list(memc
);
78 print_server_listing(memc
, stat
, server_list
);
88 static void print_server_listing(memcached_st
*memc
, memcached_stat_st
*stat
,
89 memcached_server_st
*server_list
)
94 printf("Listing %u Server\n\n", memcached_server_count(memc
));
95 for (x
= 0; x
< memcached_server_count(memc
); x
++)
100 list
= memcached_stat_get_keys(memc
, &stat
[x
], &rc
);
102 printf("Server: %s (%u)\n", memcached_server_name(memc
, server_list
[x
]),
103 memcached_server_port(memc
, server_list
[x
]));
104 for (ptr
= list
; *ptr
; ptr
++)
107 char *value
= memcached_stat_get_value(memc
, &stat
[x
], *ptr
, &rc
);
109 printf("\t %s: %s\n", *ptr
, value
);
118 static void options_parse(int argc
, char *argv
[])
120 memcached_programs_help_st help_options
[]=
130 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
131 if (option_rv
== -1) break;
136 case OPT_VERBOSE
: /* --verbose or -v */
137 opt_verbose
= OPT_VERBOSE
;
139 case OPT_DEBUG
: /* --debug or -d */
140 opt_verbose
= OPT_DEBUG
;
142 case OPT_VERSION
: /* --version or -V */
143 version_command(PROGRAM_NAME
);
145 case OPT_HELP
: /* --help or -h */
146 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
148 case OPT_SERVERS
: /* --servers or -s */
149 opt_servers
= strdup(optarg
);
152 /* getopt_long already printed an error message. */