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 void options_parse(int argc
, char *argv
[]);
21 static int opt_verbose
= 0;
22 static int opt_displayflag
= 0;
23 static char *opt_servers
= NULL
;
25 static struct option long_options
[]=
27 {"version", no_argument
, NULL
, OPT_VERSION
},
28 {"help", no_argument
, NULL
, OPT_HELP
},
29 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
30 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
31 {"servers", required_argument
, NULL
, OPT_SERVERS
},
32 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
36 int main(int argc
, char *argv
[])
41 memcached_stat_st
*stat
;
42 memcached_server_st
*servers
;
43 memcached_server_st
*server_list
;
45 options_parse(argc
, argv
);
51 if ((temp
= getenv("MEMCACHED_SERVERS")))
52 opt_servers
= strdup(temp
);
55 fprintf(stderr
, "No Servers provided\n\n");
56 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, 0);
61 memc
= memcached_create(NULL
);
63 servers
= memcached_servers_parse(opt_servers
);
64 memcached_server_push(memc
, servers
);
65 memcached_server_list_free(servers
);
67 stat
= memcached_stat(memc
, NULL
, &rc
);
69 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_SOME_ERRORS
)
71 printf("Failure to communicate with servers (%s)\n",
72 memcached_strerror(memc
, rc
));
76 server_list
= memcached_server_list(memc
);
78 printf("Listing %u Server\n\n", memcached_server_count(memc
));
79 for (x
= 0; x
< memcached_server_count(memc
); x
++)
84 list
= memcached_stat_get_keys(memc
, &stat
[x
], &rc
);
86 printf("Server: %s (%u)\n", memcached_server_name(memc
, server_list
[x
]),
87 memcached_server_port(memc
, server_list
[x
]));
88 for (ptr
= list
; *ptr
; ptr
++)
91 char *value
= memcached_stat_get_value(memc
, &stat
[x
], *ptr
, &rc
);
93 printf("\t %s: %s\n", *ptr
, value
);
104 memcached_free(memc
);
109 void options_parse(int argc
, char *argv
[])
111 memcached_programs_help_st help_options
[]=
121 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
122 if (option_rv
== -1) break;
127 case OPT_VERBOSE
: /* --verbose or -v */
128 opt_verbose
= OPT_VERBOSE
;
130 case OPT_DEBUG
: /* --debug or -d */
131 opt_verbose
= OPT_DEBUG
;
133 case OPT_VERSION
: /* --version or -V */
134 version_command(PROGRAM_NAME
);
136 case OPT_HELP
: /* --help or -h */
137 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
139 case OPT_SERVERS
: /* --servers or -s */
140 opt_servers
= strdup(optarg
);
143 /* getopt_long already printed an error message. */