809d78e03d439e3ccd5b78e471a2ea974735b57f
[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 run_analyzer(memcached_st *memc, memcached_stat_st *stat,
21 memcached_server_st *server_list, char *mode);
22 static void print_server_listing(memcached_st *memc, memcached_stat_st *stat,
23 memcached_server_st *server_list);
24 static void print_analysis_report(memcached_st *memc,
25 memcached_analysis_st *report,
26 memcached_server_st *server_list);
27
28 static int opt_verbose= 0;
29 static int opt_displayflag= 0;
30 static int opt_analyze= 0;
31 static char *opt_servers= NULL;
32 static char *analyze_mode= NULL;
33
34 static struct option long_options[]=
35 {
36 {"version", no_argument, NULL, OPT_VERSION},
37 {"help", no_argument, NULL, OPT_HELP},
38 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
39 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
40 {"servers", required_argument, NULL, OPT_SERVERS},
41 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
42 {"analyze", optional_argument, NULL, OPT_ANALYZE},
43 {0, 0, 0, 0},
44 };
45
46 int main(int argc, char *argv[])
47 {
48 memcached_return rc;
49 memcached_st *memc;
50 memcached_stat_st *stat;
51 memcached_server_st *servers;
52 memcached_server_st *server_list;
53
54 options_parse(argc, argv);
55
56 if (!opt_servers)
57 {
58 char *temp;
59
60 if ((temp= getenv("MEMCACHED_SERVERS")))
61 opt_servers= strdup(temp);
62 else
63 {
64 fprintf(stderr, "No Servers provided\n\n");
65 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, 0);
66 exit(1);
67 }
68 }
69
70 memc= memcached_create(NULL);
71
72 servers= memcached_servers_parse(opt_servers);
73 memcached_server_push(memc, servers);
74 memcached_server_list_free(servers);
75
76 stat= memcached_stat(memc, NULL, &rc);
77
78 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_SOME_ERRORS)
79 {
80 printf("Failure to communicate with servers (%s)\n",
81 memcached_strerror(memc, rc));
82 exit(1);
83 }
84
85 server_list= memcached_server_list(memc);
86
87 if (opt_analyze)
88 run_analyzer(memc, stat, server_list, analyze_mode);
89 else
90 print_server_listing(memc, stat, server_list);
91
92 free(stat);
93 free(opt_servers);
94
95 memcached_free(memc);
96
97 return 0;
98 }
99
100 static void run_analyzer(memcached_st *memc, memcached_stat_st *stat,
101 memcached_server_st *server_list, char *mode)
102 {
103 memcached_return rc;
104
105 if (analyze_mode == NULL)
106 {
107 memcached_analysis_st *report;
108 report= memcached_analyze(memc, stat, &rc);
109 if (rc != MEMCACHED_SUCCESS || report == NULL)
110 {
111 printf("Failure to analyze servers (%s)\n",
112 memcached_strerror(memc, rc));
113 exit(1);
114 }
115 print_analysis_report(memc, report, server_list);
116 free(report);
117 }
118 else
119 {
120 /* More code here in the next patch */
121 free(mode);
122 }
123 }
124
125 static void print_server_listing(memcached_st *memc, memcached_stat_st *stat,
126 memcached_server_st *server_list)
127 {
128 unsigned int x;
129 memcached_return rc;
130
131 printf("Listing %u Server\n\n", memcached_server_count(memc));
132 for (x= 0; x < memcached_server_count(memc); x++)
133 {
134 char **list;
135 char **ptr;
136
137 list= memcached_stat_get_keys(memc, &stat[x], &rc);
138
139 printf("Server: %s (%u)\n", memcached_server_name(memc, server_list[x]),
140 memcached_server_port(memc, server_list[x]));
141 for (ptr= list; *ptr; ptr++)
142 {
143 memcached_return rc;
144 char *value= memcached_stat_get_value(memc, &stat[x], *ptr, &rc);
145
146 printf("\t %s: %s\n", *ptr, value);
147 free(value);
148 }
149
150 free(list);
151 printf("\n");
152 }
153 }
154
155 static void print_analysis_report(memcached_st *memc,
156 memcached_analysis_st *report,
157 memcached_server_st *server_list)
158 {
159 uint32_t server_count= memcached_server_count(memc);
160
161 printf("Memcached Cluster Analysis Report\n\n");
162
163 printf("\tNumber of Servers Analyzed : %d\n", server_count);
164 printf("\tAverage Item Size (incl/overhead) : %u bytes\n",
165 report->average_item_size);
166
167 if (server_count == 1)
168 {
169 printf("\nFor a detailed report, you must supply multiple servers.\n");
170 return;
171 }
172
173 printf("\n");
174 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
175 memcached_server_name(memc, server_list[report->most_consumed_server]),
176 memcached_server_port(memc, server_list[report->most_consumed_server]),
177 (unsigned long long)report->most_used_bytes);
178 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
179 memcached_server_name(memc, server_list[report->least_free_server]),
180 memcached_server_port(memc, server_list[report->least_free_server]),
181 (unsigned long long)report->least_remaining_bytes);
182 printf("\tNode with longest uptime : %s:%u (%us)\n",
183 memcached_server_name(memc, server_list[report->oldest_server]),
184 memcached_server_port(memc, server_list[report->oldest_server]),
185 report->longest_uptime);
186 printf("\tPool-wide Hit Ratio : %1.f%%\n", report->pool_hit_ratio);
187 printf("\n");
188 }
189
190 static void options_parse(int argc, char *argv[])
191 {
192 memcached_programs_help_st help_options[]=
193 {
194 {0},
195 };
196
197 int option_index= 0;
198 int option_rv;
199
200 while (1)
201 {
202 option_rv= getopt_long(argc, argv, "Vhvds:a", long_options, &option_index);
203 if (option_rv == -1) break;
204 switch (option_rv)
205 {
206 case 0:
207 break;
208 case OPT_VERBOSE: /* --verbose or -v */
209 opt_verbose = OPT_VERBOSE;
210 break;
211 case OPT_DEBUG: /* --debug or -d */
212 opt_verbose = OPT_DEBUG;
213 break;
214 case OPT_VERSION: /* --version or -V */
215 version_command(PROGRAM_NAME);
216 break;
217 case OPT_HELP: /* --help or -h */
218 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
219 break;
220 case OPT_SERVERS: /* --servers or -s */
221 opt_servers= strdup(optarg);
222 break;
223 case OPT_ANALYZE: /* --analyze or -a */
224 opt_analyze= OPT_ANALYZE;
225 analyze_mode= (optarg) ? strdup(optarg) : NULL;
226 break;
227 case '?':
228 /* getopt_long already printed an error message. */
229 exit(1);
230 default:
231 abort();
232 }
233 }
234 }