f2dcfd89489f9607e7b0a2d0266f04c82e146801
[m6w6/libmemcached] / clients / 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 <string.h>
8 #include <getopt.h>
9
10 #include <libmemcached/memcached.h>
11
12 #include "client_options.h"
13 #include "utilities.h"
14
15 #define PROGRAM_NAME "memstat"
16 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
17
18 /* Prototypes */
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);
22
23 static int opt_verbose= 0;
24 static int opt_displayflag= 0;
25 static char *opt_servers= NULL;
26
27 static struct option long_options[]=
28 {
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},
35 {0, 0, 0, 0},
36 };
37
38 int main(int argc, char *argv[])
39 {
40 memcached_return rc;
41 memcached_st *memc;
42 memcached_stat_st *stat;
43 memcached_server_st *servers;
44 memcached_server_st *server_list;
45
46 options_parse(argc, argv);
47
48 if (!opt_servers)
49 {
50 char *temp;
51
52 if ((temp= getenv("MEMCACHED_SERVERS")))
53 opt_servers= strdup(temp);
54 else
55 {
56 fprintf(stderr, "No Servers provided\n\n");
57 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, 0);
58 exit(1);
59 }
60 }
61
62 memc= memcached_create(NULL);
63
64 servers= memcached_servers_parse(opt_servers);
65 memcached_server_push(memc, servers);
66 memcached_server_list_free(servers);
67
68 stat= memcached_stat(memc, NULL, &rc);
69
70 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_SOME_ERRORS)
71 {
72 printf("Failure to communicate with servers (%s)\n",
73 memcached_strerror(memc, rc));
74 exit(1);
75 }
76
77 server_list= memcached_server_list(memc);
78 print_server_listing(memc, stat, server_list);
79
80 free(stat);
81 free(opt_servers);
82
83 memcached_free(memc);
84
85 return 0;
86 }
87
88 static void print_server_listing(memcached_st *memc, memcached_stat_st *stat,
89 memcached_server_st *server_list)
90 {
91 unsigned int x;
92 memcached_return rc;
93
94 printf("Listing %u Server\n\n", memcached_server_count(memc));
95 for (x= 0; x < memcached_server_count(memc); x++)
96 {
97 char **list;
98 char **ptr;
99
100 list= memcached_stat_get_keys(memc, &stat[x], &rc);
101
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++)
105 {
106 memcached_return rc;
107 char *value= memcached_stat_get_value(memc, &stat[x], *ptr, &rc);
108
109 printf("\t %s: %s\n", *ptr, value);
110 free(value);
111 }
112
113 free(list);
114 printf("\n");
115 }
116 }
117
118 static void options_parse(int argc, char *argv[])
119 {
120 memcached_programs_help_st help_options[]=
121 {
122 {0},
123 };
124
125 int option_index= 0;
126 int option_rv;
127
128 while (1)
129 {
130 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
131 if (option_rv == -1) break;
132 switch (option_rv)
133 {
134 case 0:
135 break;
136 case OPT_VERBOSE: /* --verbose or -v */
137 opt_verbose = OPT_VERBOSE;
138 break;
139 case OPT_DEBUG: /* --debug or -d */
140 opt_verbose = OPT_DEBUG;
141 break;
142 case OPT_VERSION: /* --version or -V */
143 version_command(PROGRAM_NAME);
144 break;
145 case OPT_HELP: /* --help or -h */
146 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
147 break;
148 case OPT_SERVERS: /* --servers or -s */
149 opt_servers= strdup(optarg);
150 break;
151 case '?':
152 /* getopt_long already printed an error message. */
153 exit(1);
154 default:
155 abort();
156 }
157 }
158 }