Slight style cleanup.
[awesomized/libmemcached] / clients / memstat.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 * Authors:
12 * Brian Aker
13 * Toru Maesaka
14 */
15 #include <mem_config.h>
16
17 #include <cstdio>
18 #include <cstring>
19 #include <ctime>
20 #include <iostream>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <sys/types.h>
28
29 #include <libmemcached-1.0/memcached.h>
30
31 #include "client_options.h"
32 #include "utilities.h"
33
34 #define PROGRAM_NAME "memstat"
35 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
36
37 /* Prototypes */
38 static void options_parse(int argc, char *argv[]);
39 static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat);
40 static void print_analysis_report(memcached_st *memc,
41 memcached_analysis_st *report);
42
43 static bool opt_binary= false;
44 static bool opt_verbose= false;
45 static bool opt_server_version= false;
46 static bool opt_analyze= false;
47 static char *opt_servers= NULL;
48 static char *stat_args= NULL;
49 static char *analyze_mode= NULL;
50 static char *opt_username;
51 static char *opt_passwd;
52
53 static struct option long_options[]=
54 {
55 {(OPTIONSTRING)"args", required_argument, NULL, OPT_STAT_ARGS},
56 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
57 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
58 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
59 {(OPTIONSTRING)"verbose", no_argument, NULL, OPT_VERBOSE},
60 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
61 {(OPTIONSTRING)"debug", no_argument, NULL, OPT_DEBUG},
62 {(OPTIONSTRING)"server-version", no_argument, NULL, OPT_SERVER_VERSION},
63 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
64 {(OPTIONSTRING)"analyze", optional_argument, NULL, OPT_ANALYZE},
65 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
66 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
67 {0, 0, 0, 0},
68 };
69
70
71 static memcached_return_t stat_printer(memcached_server_instance_st instance,
72 const char *key, size_t key_length,
73 const char *value, size_t value_length,
74 void *context)
75 {
76 static memcached_server_instance_st last= NULL;
77 (void)context;
78
79 if (last != instance)
80 {
81 printf("Server: %s (%u)\n", memcached_server_name(instance),
82 (uint32_t)memcached_server_port(instance));
83 last= instance;
84 }
85
86 printf("\t %.*s: %.*s\n", (int)key_length, key, (int)value_length, value);
87
88 return MEMCACHED_SUCCESS;
89 }
90
91 static memcached_return_t server_print_callback(const memcached_st *,
92 memcached_server_instance_st instance,
93 void *)
94 {
95 std::cerr << memcached_server_name(instance) << ":" << memcached_server_port(instance) <<
96 " " << int(memcached_server_major_version(instance)) <<
97 "." << int(memcached_server_minor_version(instance)) <<
98 "." << int(memcached_server_micro_version(instance)) << std::endl;
99
100 return MEMCACHED_SUCCESS;
101 }
102
103 int main(int argc, char *argv[])
104 {
105 options_parse(argc, argv);
106 initialize_sockets();
107
108 if (opt_servers == false)
109 {
110 char *temp;
111 if ((temp= getenv("MEMCACHED_SERVERS")))
112 {
113 opt_servers= strdup(temp);
114 }
115 else
116 {
117 std::cerr << "No Servers provided" << std::endl;
118 return EXIT_FAILURE;
119 }
120 }
121
122 memcached_st *memc= memcached_create(NULL);
123 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
124
125 memcached_server_st *servers= memcached_servers_parse(opt_servers);
126 free(opt_servers);
127
128 memcached_return_t rc= memcached_server_push(memc, servers);
129 memcached_server_list_free(servers);
130
131 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
132 {
133 memcached_free(memc);
134 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
135 return EXIT_FAILURE;
136 }
137
138 if (opt_username)
139 {
140 memcached_return_t ret;
141 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
142 {
143 std::cerr << memcached_last_error_message(memc) << std::endl;
144 memcached_free(memc);
145 return EXIT_FAILURE;
146 }
147 }
148
149 if (rc != MEMCACHED_SUCCESS and rc != MEMCACHED_SOME_ERRORS)
150 {
151 printf("Failure to communicate with servers (%s)\n",
152 memcached_strerror(memc, rc));
153 exit(EXIT_FAILURE);
154 }
155
156 if (opt_server_version)
157 {
158 if (memcached_failed(memcached_version(memc)))
159 {
160 std::cerr << "Unable to obtain server version";
161 exit(EXIT_FAILURE);
162 }
163
164 memcached_server_fn callbacks[1];
165 callbacks[0]= server_print_callback;
166 memcached_server_cursor(memc, callbacks, NULL, 1);
167 }
168 else if (opt_analyze)
169 {
170 memcached_stat_st *memc_stat= memcached_stat(memc, NULL, &rc);
171
172 if (memc_stat == NULL)
173 {
174 exit(EXIT_FAILURE);
175 }
176
177 run_analyzer(memc, memc_stat);
178
179 memcached_stat_free(memc, memc_stat);
180 }
181 else
182 {
183 rc= memcached_stat_execute(memc, stat_args, stat_printer, NULL);
184 }
185
186 memcached_free(memc);
187
188 return rc == MEMCACHED_SUCCESS ? EXIT_SUCCESS: EXIT_FAILURE;
189 }
190
191 static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat)
192 {
193 memcached_return_t rc;
194
195 if (analyze_mode == NULL)
196 {
197 memcached_analysis_st *report;
198 report= memcached_analyze(memc, memc_stat, &rc);
199 if (rc != MEMCACHED_SUCCESS || report == NULL)
200 {
201 printf("Failure to analyze servers (%s)\n",
202 memcached_strerror(memc, rc));
203 exit(1);
204 }
205 print_analysis_report(memc, report);
206 free(report);
207 }
208 else if (strcmp(analyze_mode, "latency") == 0)
209 {
210 uint32_t flags, server_count= memcached_server_count(memc);
211 uint32_t num_of_tests= 32;
212 const char *test_key= "libmemcached_test_key";
213
214 memcached_st **servers;
215 servers= static_cast<memcached_st**>(malloc(sizeof(memcached_st*) * server_count));
216 if (not servers)
217 {
218 fprintf(stderr, "Failed to allocate memory\n");
219 return;
220 }
221
222 for (uint32_t x= 0; x < server_count; x++)
223 {
224 memcached_server_instance_st instance=
225 memcached_server_instance_by_position(memc, x);
226
227 if ((servers[x]= memcached_create(NULL)) == NULL)
228 {
229 fprintf(stderr, "Failed to memcached_create()\n");
230 if (x > 0)
231 memcached_free(servers[0]);
232 x--;
233
234 for (; x > 0; x--)
235 memcached_free(servers[x]);
236
237 free(servers);
238 return;
239 }
240 memcached_server_add(servers[x],
241 memcached_server_name(instance),
242 memcached_server_port(instance));
243 }
244
245 printf("Network Latency Test:\n\n");
246 struct timeval start_time, end_time;
247 uint32_t slowest_server= 0;
248 long elapsed_time, slowest_time= 0;
249
250 for (uint32_t x= 0; x < server_count; x++)
251 {
252 memcached_server_instance_st instance=
253 memcached_server_instance_by_position(memc, x);
254 gettimeofday(&start_time, NULL);
255
256 for (uint32_t y= 0; y < num_of_tests; y++)
257 {
258 size_t vlen;
259 char *val= memcached_get(servers[x], test_key, strlen(test_key),
260 &vlen, &flags, &rc);
261 if (rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_SUCCESS)
262 break;
263 free(val);
264 }
265 gettimeofday(&end_time, NULL);
266
267 elapsed_time= (long) timedif(end_time, start_time);
268 elapsed_time /= (long) num_of_tests;
269
270 if (elapsed_time > slowest_time)
271 {
272 slowest_server= x;
273 slowest_time= elapsed_time;
274 }
275
276 if (rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_SUCCESS)
277 {
278 printf("\t %s (%d) => failed to reach the server\n",
279 memcached_server_name(instance),
280 memcached_server_port(instance));
281 }
282 else
283 {
284 printf("\t %s (%d) => %ld.%ld seconds\n",
285 memcached_server_name(instance),
286 memcached_server_port(instance),
287 elapsed_time / 1000, elapsed_time % 1000);
288 }
289 }
290
291 if (server_count > 1 && slowest_time > 0)
292 {
293 memcached_server_instance_st slowest=
294 memcached_server_instance_by_position(memc, slowest_server);
295
296 printf("---\n");
297 printf("Slowest Server: %s (%d) => %ld.%ld seconds\n",
298 memcached_server_name(slowest),
299 memcached_server_port(slowest),
300 slowest_time / 1000, slowest_time % 1000);
301 }
302 printf("\n");
303
304 for (uint32_t x= 0; x < server_count; x++)
305 memcached_free(servers[x]);
306
307 free(servers);
308 free(analyze_mode);
309 }
310 else
311 {
312 fprintf(stderr, "Invalid Analyzer Option provided\n");
313 free(analyze_mode);
314 }
315 }
316
317 static void print_analysis_report(memcached_st *memc,
318 memcached_analysis_st *report)
319
320 {
321 uint32_t server_count= memcached_server_count(memc);
322 memcached_server_instance_st most_consumed_server= memcached_server_instance_by_position(memc, report->most_consumed_server);
323 memcached_server_instance_st least_free_server= memcached_server_instance_by_position(memc, report->least_free_server);
324 memcached_server_instance_st oldest_server= memcached_server_instance_by_position(memc, report->oldest_server);
325
326 printf("Memcached Cluster Analysis Report\n\n");
327
328 printf("\tNumber of Servers Analyzed : %u\n", server_count);
329 printf("\tAverage Item Size (incl/overhead) : %u bytes\n",
330 report->average_item_size);
331
332 if (server_count == 1)
333 {
334 printf("\nFor a detailed report, you must supply multiple servers.\n");
335 return;
336 }
337
338 printf("\n");
339 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
340 memcached_server_name(most_consumed_server),
341 (uint32_t)memcached_server_port(most_consumed_server),
342 (unsigned long long)report->most_used_bytes);
343 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
344 memcached_server_name(least_free_server),
345 (uint32_t)memcached_server_port(least_free_server),
346 (unsigned long long)report->least_remaining_bytes);
347 printf("\tNode with longest uptime : %s:%u (%us)\n",
348 memcached_server_name(oldest_server),
349 (uint32_t)memcached_server_port(oldest_server),
350 report->longest_uptime);
351 printf("\tPool-wide Hit Ratio : %1.f%%\n", report->pool_hit_ratio);
352 printf("\n");
353 }
354
355 static void options_parse(int argc, char *argv[])
356 {
357 memcached_programs_help_st help_options[]=
358 {
359 {0},
360 };
361
362 int option_index= 0;
363
364 bool opt_version= false;
365 bool opt_help= false;
366 while (1)
367 {
368 int option_rv= getopt_long(argc, argv, "Vhvds:a", long_options, &option_index);
369
370 if (option_rv == -1)
371 break;
372
373 switch (option_rv)
374 {
375 case 0:
376 break;
377
378 case OPT_VERBOSE: /* --verbose or -v */
379 opt_verbose= true;
380 break;
381
382 case OPT_DEBUG: /* --debug or -d */
383 opt_verbose= true;
384 break;
385
386 case OPT_BINARY:
387 opt_binary= true;
388 break;
389
390 case OPT_SERVER_VERSION:
391 opt_server_version= true;
392 break;
393
394 case OPT_VERSION: /* --version or -V */
395 opt_version= true;
396 break;
397
398 case OPT_HELP: /* --help or -h */
399 opt_help= true;
400 break;
401
402 case OPT_SERVERS: /* --servers or -s */
403 opt_servers= strdup(optarg);
404 break;
405
406 case OPT_STAT_ARGS:
407 stat_args= strdup(optarg);
408 break;
409
410 case OPT_ANALYZE: /* --analyze or -a */
411 opt_analyze= OPT_ANALYZE;
412 analyze_mode= (optarg) ? strdup(optarg) : NULL;
413 break;
414
415 case OPT_QUIET:
416 close_stdio();
417 break;
418
419 case OPT_USERNAME:
420 opt_username= optarg;
421 opt_binary= true;
422 break;
423
424 case OPT_PASSWD:
425 opt_passwd= optarg;
426 break;
427
428 case '?':
429 /* getopt_long already printed an error message. */
430 exit(1);
431 default:
432 abort();
433 }
434 }
435
436 if (opt_version)
437 {
438 version_command(PROGRAM_NAME);
439 exit(EXIT_SUCCESS);
440 }
441
442 if (opt_help)
443 {
444 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
445 exit(EXIT_SUCCESS);
446 }
447 }