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";
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
)
187 run_analyzer(memc
, memc_stat
);
189 memcached_stat_free(memc
, memc_stat
);
193 rc
= memcached_stat_execute(memc
, stat_args
, stat_printer
, NULL
);
196 memcached_free(memc
);
198 return rc
== MEMCACHED_SUCCESS
? EXIT_SUCCESS
: EXIT_FAILURE
;
201 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*memc_stat
)
203 memcached_return_t rc
;
205 if (analyze_mode
== NULL
)
207 memcached_analysis_st
*report
;
208 report
= memcached_analyze(memc
, memc_stat
, &rc
);
209 if (rc
!= MEMCACHED_SUCCESS
|| report
== NULL
)
211 printf("Failure to analyze servers (%s)\n",
212 memcached_strerror(memc
, rc
));
215 print_analysis_report(memc
, report
);
218 else if (strcmp(analyze_mode
, "latency") == 0)
220 uint32_t flags
, server_count
= memcached_server_count(memc
);
221 uint32_t num_of_tests
= 32;
222 const char *test_key
= "libmemcached_test_key";
224 memcached_st
**servers
= static_cast<memcached_st
**>(malloc(sizeof(memcached_st
*) * server_count
));
227 fprintf(stderr
, "Failed to allocate memory\n");
231 for (uint32_t x
= 0; x
< server_count
; x
++)
233 const memcached_instance_st
* instance
=
234 memcached_server_instance_by_position(memc
, x
);
236 if ((servers
[x
]= memcached_create(NULL
)) == NULL
)
238 fprintf(stderr
, "Failed to memcached_create()\n");
241 memcached_free(servers
[0]);
247 memcached_free(servers
[x
]);
254 memcached_server_add(servers
[x
],
255 memcached_server_name(instance
),
256 memcached_server_port(instance
));
259 printf("Network Latency Test:\n\n");
260 struct timeval start_time
, end_time
;
261 uint32_t slowest_server
= 0;
262 long elapsed_time
, slowest_time
= 0;
264 for (uint32_t x
= 0; x
< server_count
; x
++)
266 const memcached_instance_st
* instance
=
267 memcached_server_instance_by_position(memc
, x
);
268 gettimeofday(&start_time
, NULL
);
270 for (uint32_t y
= 0; y
< num_of_tests
; y
++)
273 char *val
= memcached_get(servers
[x
], test_key
, strlen(test_key
),
275 if (rc
!= MEMCACHED_NOTFOUND
and rc
!= MEMCACHED_SUCCESS
)
281 gettimeofday(&end_time
, NULL
);
283 elapsed_time
= (long) timedif(end_time
, start_time
);
284 elapsed_time
/= (long) num_of_tests
;
286 if (elapsed_time
> slowest_time
)
289 slowest_time
= elapsed_time
;
292 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
294 printf("\t %s (%d) => failed to reach the server\n",
295 memcached_server_name(instance
),
296 memcached_server_port(instance
));
300 printf("\t %s (%d) => %ld.%ld seconds\n",
301 memcached_server_name(instance
),
302 memcached_server_port(instance
),
303 elapsed_time
/ 1000, elapsed_time
% 1000);
307 if (server_count
> 1 && slowest_time
> 0)
309 const memcached_instance_st
* slowest
=
310 memcached_server_instance_by_position(memc
, slowest_server
);
313 printf("Slowest Server: %s (%d) => %ld.%ld seconds\n",
314 memcached_server_name(slowest
),
315 memcached_server_port(slowest
),
316 slowest_time
/ 1000, slowest_time
% 1000);
320 for (uint32_t x
= 0; x
< server_count
; x
++)
322 memcached_free(servers
[x
]);
330 fprintf(stderr
, "Invalid Analyzer Option provided\n");
335 static void print_analysis_report(memcached_st
*memc
,
336 memcached_analysis_st
*report
)
339 uint32_t server_count
= memcached_server_count(memc
);
340 const memcached_instance_st
* most_consumed_server
= memcached_server_instance_by_position(memc
, report
->most_consumed_server
);
341 const memcached_instance_st
* least_free_server
= memcached_server_instance_by_position(memc
, report
->least_free_server
);
342 const memcached_instance_st
* oldest_server
= memcached_server_instance_by_position(memc
, report
->oldest_server
);
344 printf("Memcached Cluster Analysis Report\n\n");
346 printf("\tNumber of Servers Analyzed : %u\n", server_count
);
347 printf("\tAverage Item Size (incl/overhead) : %u bytes\n",
348 report
->average_item_size
);
350 if (server_count
== 1)
352 printf("\nFor a detailed report, you must supply multiple servers.\n");
357 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
358 memcached_server_name(most_consumed_server
),
359 (uint32_t)memcached_server_port(most_consumed_server
),
360 (unsigned long long)report
->most_used_bytes
);
361 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
362 memcached_server_name(least_free_server
),
363 (uint32_t)memcached_server_port(least_free_server
),
364 (unsigned long long)report
->least_remaining_bytes
);
365 printf("\tNode with longest uptime : %s:%u (%us)\n",
366 memcached_server_name(oldest_server
),
367 (uint32_t)memcached_server_port(oldest_server
),
368 report
->longest_uptime
);
369 printf("\tPool-wide Hit Ratio : %1.f%%\n", report
->pool_hit_ratio
);
373 static void options_parse(int argc
, char *argv
[])
375 memcached_programs_help_st help_options
[]=
382 bool opt_version
= false;
383 bool opt_help
= false;
386 int option_rv
= getopt_long(argc
, argv
, "Vhvds:a", long_options
, &option_index
);
396 case OPT_VERBOSE
: /* --verbose or -v */
400 case OPT_DEBUG
: /* --debug or -d */
408 case OPT_SERVER_VERSION
:
409 opt_server_version
= true;
412 case OPT_VERSION
: /* --version or -V */
416 case OPT_HELP
: /* --help or -h */
420 case OPT_SERVERS
: /* --servers or -s */
421 opt_servers
= strdup(optarg
);
425 stat_args
= strdup(optarg
);
428 case OPT_ANALYZE
: /* --analyze or -a */
429 opt_analyze
= OPT_ANALYZE
;
430 analyze_mode
= (optarg
) ? strdup(optarg
) : NULL
;
438 opt_username
= optarg
;
447 /* getopt_long already printed an error message. */
456 version_command(PROGRAM_NAME
);
462 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);