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 int main(int argc
, char *argv
[])
30 memcached_stat_st
*stat
;
31 memcached_server_st
*servers
;
32 memcached_server_st
*server_list
;
34 options_parse(argc
, argv
);
40 if ((temp
= getenv("MEMCACHED_SERVERS")))
41 opt_servers
= strdup(temp
);
44 fprintf(stderr
, "No Servers provided\n");
49 memc
= memcached_create(NULL
);
51 servers
= memcached_servers_parse(opt_servers
);
52 memcached_server_push(memc
, servers
);
53 memcached_server_list_free(servers
);
55 stat
= memcached_stat(memc
, NULL
, &rc
);
57 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_SOME_ERRORS
)
59 printf("Failure to communicate with servers (%s)\n",
60 memcached_strerror(memc
, rc
));
64 server_list
= memcached_server_list(memc
);
66 printf("Listing %u Server\n\n", memcached_server_count(memc
));
67 for (x
= 0; x
< memcached_server_count(memc
); x
++)
72 list
= memcached_stat_get_keys(memc
, &stat
[x
], &rc
);
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. */