All *_init() have been changed to _create()
[awesomized/libmemcached] / src / memstat.c
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <sys/types.h>
5 #include <sys/mman.h>
6 #include <fcntl.h>
7 #include <getopt.h>
8
9 #include <memcached.h>
10
11 #include "client_options.h"
12 #include "utilities.h"
13
14 #define PROGRAM_NAME "memstat"
15 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
16
17 /* Prototypes */
18 void options_parse(int argc, char *argv[]);
19
20 static int opt_verbose= 0;
21 static int opt_displayflag= 0;
22 static char *opt_servers= NULL;
23
24 int main(int argc, char *argv[])
25 {
26 unsigned int x;
27 memcached_return rc;
28 memcached_st *memc;
29 memcached_stat_st *stat;
30 memcached_server_st *servers;
31 memcached_server_st *server_list;
32
33 options_parse(argc, argv);
34
35 if (!opt_servers)
36 return 0;
37
38 memc= memcached_create(NULL);
39
40 servers= parse_opt_servers(opt_servers);
41 memcached_server_push(memc, servers);
42 memcached_server_list_free(servers);
43
44 stat= memcached_stat(memc, NULL, &rc);
45
46 if (rc != MEMCACHED_SUCCESS || rc != MEMCACHED_SOME_ERRORS);
47 {
48 printf("Failure to communicate with servers (%s)\n",
49 memcached_strerror(memc, rc));
50 exit(1);
51 }
52
53 server_list= memcached_server_list(memc);
54
55 printf("Listing %u Server\n\n", memcached_server_count(memc));
56 for (x= 0; x < memcached_server_count(memc); x++)
57 {
58 char **list;
59 char **ptr;
60
61 list= memcached_stat_get_keys(memc, &stat[x], &rc);
62 assert(list);
63 assert(rc == MEMCACHED_SUCCESS);
64
65 printf("Server: %s (%u)\n", memcached_server_name(memc, server_list[x]),
66 memcached_server_port(memc, server_list[x]));
67 for (ptr= list; *ptr; ptr++)
68 {
69 memcached_return rc;
70 char *value= memcached_stat_get_value(memc, &stat[x], *ptr, &rc);
71
72 printf("\t %s: %s\n", *ptr, value);
73 free(value);
74 }
75
76 free(list);
77 printf("\n");
78 }
79
80 free(stat);
81 free(opt_servers);
82
83 memcached_free(memc);
84
85 return 0;
86 }
87
88 void options_parse(int argc, char *argv[])
89 {
90 memcached_programs_help_st help_options[]=
91 {
92 {0},
93 };
94
95 static struct option long_options[]=
96 {
97 {"version", no_argument, NULL, OPT_VERSION},
98 {"help", no_argument, NULL, OPT_HELP},
99 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
100 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
101 {"servers", required_argument, NULL, OPT_SERVERS},
102 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
103 {0, 0, 0, 0},
104 };
105
106 int option_index= 0;
107 int option_rv;
108
109 while (1)
110 {
111 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
112 if (option_rv == -1) break;
113 switch (option_rv)
114 {
115 case 0:
116 break;
117 case OPT_VERBOSE: /* --verbose or -v */
118 opt_verbose = OPT_VERBOSE;
119 break;
120 case OPT_DEBUG: /* --debug or -d */
121 opt_verbose = OPT_DEBUG;
122 break;
123 case OPT_VERSION: /* --version or -V */
124 version_command(PROGRAM_NAME);
125 break;
126 case OPT_HELP: /* --help or -h */
127 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
128 break;
129 case OPT_SERVERS: /* --servers or -s */
130 opt_servers= strdup(optarg);
131 break;
132 case '?':
133 /* getopt_long already printed an error message. */
134 exit(1);
135 default:
136 abort();
137 }
138 }
139 }