2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
15 #include "mem_config.h"
26 #include <sys/types.h>
27 #include <sys/types.h>
29 #include "libmemcached-1.0/memcached.h"
31 #include "client_options.h"
32 #include "utilities.h"
34 #define PROGRAM_NAME "memstat"
35 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
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
);
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
;
53 static struct option long_options
[]=
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
},
71 static memcached_return_t
stat_printer(const memcached_instance_st
* instance
,
72 const char *key
, size_t key_length
,
73 const char *value
, size_t value_length
,
76 static const memcached_instance_st
* last
= NULL
;
81 printf("Server: %s (%u)\n", memcached_server_name(instance
),
82 (uint32_t)memcached_server_port(instance
));
86 printf("\t %.*s: %.*s\n", (int)key_length
, key
, (int)value_length
, value
);
88 return MEMCACHED_SUCCESS
;
91 static memcached_return_t
server_print_callback(const memcached_st
*,
92 const memcached_instance_st
* instance
,
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
;
100 return MEMCACHED_SUCCESS
;
103 int main(int argc
, char *argv
[])
105 options_parse(argc
, argv
);
106 initialize_sockets();
108 if (opt_servers
== NULL
)
111 if ((temp
= getenv("MEMCACHED_SERVERS")))
113 opt_servers
= strdup(temp
);
116 if (opt_servers
== NULL
)
118 std::cerr
<< "No Servers provided" << std::endl
;
123 memcached_server_st
* servers
= memcached_servers_parse(opt_servers
);
124 if (servers
== NULL
or memcached_server_list_count(servers
) == 0)
126 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
135 memcached_st
*memc
= memcached_create(NULL
);
136 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
138 memcached_return_t rc
= memcached_server_push(memc
, servers
);
139 memcached_server_list_free(servers
);
141 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
143 memcached_free(memc
);
144 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
150 memcached_return_t ret
;
151 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
153 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
154 memcached_free(memc
);
159 if (rc
!= MEMCACHED_SUCCESS
and rc
!= MEMCACHED_SOME_ERRORS
)
161 printf("Failure to communicate with servers (%s)\n",
162 memcached_strerror(memc
, rc
));
166 if (opt_server_version
)
168 if (memcached_failed(memcached_version(memc
)))
170 std::cerr
<< "Unable to obtain server version" << std::endl
;
174 memcached_server_fn callbacks
[1];
175 callbacks
[0]= server_print_callback
;
176 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
178 else if (opt_analyze
)
180 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
182 if (memc_stat
== NULL
|| rc
!= MEMCACHED_SUCCESS
)
184 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
188 run_analyzer(memc
, memc_stat
);
190 memcached_stat_free(memc
, memc_stat
);
194 rc
= memcached_stat_execute(memc
, stat_args
, stat_printer
, NULL
);
197 memcached_free(memc
);
199 return rc
== MEMCACHED_SUCCESS
? EXIT_SUCCESS
: EXIT_FAILURE
;
202 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*memc_stat
)
204 memcached_return_t rc
;
206 if (analyze_mode
== NULL
)
208 memcached_analysis_st
*report
;
209 report
= memcached_analyze(memc
, memc_stat
, &rc
);
210 if (rc
!= MEMCACHED_SUCCESS
|| report
== NULL
)
212 printf("Failure to analyze servers (%s)\n",
213 memcached_strerror(memc
, rc
));
216 print_analysis_report(memc
, report
);
219 else if (strcmp(analyze_mode
, "latency") == 0)
221 uint32_t flags
, server_count
= memcached_server_count(memc
);
222 uint32_t num_of_tests
= 32;
223 const char *test_key
= "libmemcached_test_key";
225 memcached_st
**servers
= static_cast<memcached_st
**>(malloc(sizeof(memcached_st
*) * server_count
));
228 fprintf(stderr
, "Failed to allocate memory\n");
232 for (uint32_t x
= 0; x
< server_count
; x
++)
234 const memcached_instance_st
* instance
=
235 memcached_server_instance_by_position(memc
, x
);
237 if ((servers
[x
]= memcached_create(NULL
)) == NULL
)
239 fprintf(stderr
, "Failed to memcached_create()\n");
242 memcached_free(servers
[0]);
248 memcached_free(servers
[x
]);
255 memcached_server_add(servers
[x
],
256 memcached_server_name(instance
),
257 memcached_server_port(instance
));
260 printf("Network Latency Test:\n\n");
261 struct timeval start_time
, end_time
;
262 uint32_t slowest_server
= 0;
263 long elapsed_time
, slowest_time
= 0;
265 for (uint32_t x
= 0; x
< server_count
; x
++)
267 const memcached_instance_st
* instance
=
268 memcached_server_instance_by_position(memc
, x
);
269 gettimeofday(&start_time
, NULL
);
271 for (uint32_t y
= 0; y
< num_of_tests
; y
++)
274 char *val
= memcached_get(servers
[x
], test_key
, strlen(test_key
),
276 if (rc
!= MEMCACHED_NOTFOUND
and rc
!= MEMCACHED_SUCCESS
)
282 gettimeofday(&end_time
, NULL
);
284 elapsed_time
= (long) timedif(end_time
, start_time
);
285 elapsed_time
/= (long) num_of_tests
;
287 if (elapsed_time
> slowest_time
)
290 slowest_time
= elapsed_time
;
293 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
295 printf("\t %s (%d) => failed to reach the server\n",
296 memcached_server_name(instance
),
297 memcached_server_port(instance
));
301 printf("\t %s (%d) => %ld.%ld seconds\n",
302 memcached_server_name(instance
),
303 memcached_server_port(instance
),
304 elapsed_time
/ 1000, elapsed_time
% 1000);
308 if (server_count
> 1 && slowest_time
> 0)
310 const memcached_instance_st
* slowest
=
311 memcached_server_instance_by_position(memc
, slowest_server
);
314 printf("Slowest Server: %s (%d) => %ld.%ld seconds\n",
315 memcached_server_name(slowest
),
316 memcached_server_port(slowest
),
317 slowest_time
/ 1000, slowest_time
% 1000);
321 for (uint32_t x
= 0; x
< server_count
; x
++)
323 memcached_free(servers
[x
]);
331 fprintf(stderr
, "Invalid Analyzer Option provided\n");
336 static void print_analysis_report(memcached_st
*memc
,
337 memcached_analysis_st
*report
)
340 uint32_t server_count
= memcached_server_count(memc
);
341 const memcached_instance_st
* most_consumed_server
= memcached_server_instance_by_position(memc
, report
->most_consumed_server
);
342 const memcached_instance_st
* least_free_server
= memcached_server_instance_by_position(memc
, report
->least_free_server
);
343 const memcached_instance_st
* oldest_server
= memcached_server_instance_by_position(memc
, report
->oldest_server
);
345 printf("Memcached Cluster Analysis Report\n\n");
347 printf("\tNumber of Servers Analyzed : %u\n", server_count
);
348 printf("\tAverage Item Size (incl/overhead) : %u bytes\n",
349 report
->average_item_size
);
351 if (server_count
== 1)
353 printf("\nFor a detailed report, you must supply multiple servers.\n");
358 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
359 memcached_server_name(most_consumed_server
),
360 (uint32_t)memcached_server_port(most_consumed_server
),
361 (unsigned long long)report
->most_used_bytes
);
362 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
363 memcached_server_name(least_free_server
),
364 (uint32_t)memcached_server_port(least_free_server
),
365 (unsigned long long)report
->least_remaining_bytes
);
366 printf("\tNode with longest uptime : %s:%u (%us)\n",
367 memcached_server_name(oldest_server
),
368 (uint32_t)memcached_server_port(oldest_server
),
369 report
->longest_uptime
);
370 printf("\tPool-wide Hit Ratio : %1.f%%\n", report
->pool_hit_ratio
);
374 static void options_parse(int argc
, char *argv
[])
376 memcached_programs_help_st help_options
[]=
383 bool opt_version
= false;
384 bool opt_help
= false;
387 int option_rv
= getopt_long(argc
, argv
, "Vhvds:a", long_options
, &option_index
);
397 case OPT_VERBOSE
: /* --verbose or -v */
401 case OPT_DEBUG
: /* --debug or -d */
409 case OPT_SERVER_VERSION
:
410 opt_server_version
= true;
413 case OPT_VERSION
: /* --version or -V */
417 case OPT_HELP
: /* --help or -h */
421 case OPT_SERVERS
: /* --servers or -s */
422 opt_servers
= strdup(optarg
);
426 stat_args
= strdup(optarg
);
429 case OPT_ANALYZE
: /* --analyze or -a */
431 analyze_mode
= (optarg
) ? strdup(optarg
) : NULL
;
439 opt_username
= optarg
;
448 /* getopt_long already printed an error message. */
457 version_command(PROGRAM_NAME
);
463 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);