11 #include <memcached.h>
13 #include "client_options.h"
14 #include "utilities.h"
16 #define PROGRAM_NAME "memstat"
17 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
20 void options_parse(int argc
, char *argv
[]);
22 static int opt_verbose
= 0;
23 static int opt_displayflag
= 0;
24 static char *opt_servers
= NULL
;
26 int main(int argc
, char *argv
[])
31 memcached_stat_st
*stat
;
32 memcached_server_st
*servers
;
33 memcached_server_st
*server_list
;
35 options_parse(argc
, argv
);
41 if ((temp
= getenv("MEMCACHED_SERVERS")))
42 opt_servers
= strdup(temp
);
47 memc
= memcached_create(NULL
);
49 servers
= memcached_servers_parse(opt_servers
);
50 memcached_server_push(memc
, servers
);
51 memcached_server_list_free(servers
);
53 stat
= memcached_stat(memc
, NULL
, &rc
);
55 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_SOME_ERRORS
)
57 printf("Failure to communicate with servers (%s)\n",
58 memcached_strerror(memc
, rc
));
62 server_list
= memcached_server_list(memc
);
64 printf("Listing %u Server\n\n", memcached_server_count(memc
));
65 for (x
= 0; x
< memcached_server_count(memc
); x
++)
70 list
= memcached_stat_get_keys(memc
, &stat
[x
], &rc
);
72 assert(rc
== MEMCACHED_SUCCESS
);
74 printf("Server: %s (%u)\n", memcached_server_name(memc
, server_list
[x
]),
75 memcached_server_port(memc
, server_list
[x
]));
76 for (ptr
= list
; *ptr
; ptr
++)
79 char *value
= memcached_stat_get_value(memc
, &stat
[x
], *ptr
, &rc
);
81 printf("\t %s: %s\n", *ptr
, value
);
97 void options_parse(int argc
, char *argv
[])
99 memcached_programs_help_st help_options
[]=
104 static struct option long_options
[]=
106 {"version", no_argument
, NULL
, OPT_VERSION
},
107 {"help", no_argument
, NULL
, OPT_HELP
},
108 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
109 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
110 {"servers", required_argument
, NULL
, OPT_SERVERS
},
111 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
120 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
121 if (option_rv
== -1) break;
126 case OPT_VERBOSE
: /* --verbose or -v */
127 opt_verbose
= OPT_VERBOSE
;
129 case OPT_DEBUG
: /* --debug or -d */
130 opt_verbose
= OPT_DEBUG
;
132 case OPT_VERSION
: /* --version or -V */
133 version_command(PROGRAM_NAME
);
135 case OPT_HELP
: /* --help or -h */
136 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
138 case OPT_SERVERS
: /* --servers or -s */
139 opt_servers
= strdup(optarg
);
142 /* getopt_long already printed an error message. */