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(memcached_server_instance_st instance
,
72 const char *key
, size_t key_length
,
73 const char *value
, size_t value_length
,
76 static memcached_server_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 memcached_server_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
== false)
111 if ((temp
= getenv("MEMCACHED_SERVERS")))
113 opt_servers
= strdup(temp
);
117 std::cerr
<< "No Servers provided" << std::endl
;
122 memcached_st
*memc
= memcached_create(NULL
);
123 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
125 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
128 memcached_return_t rc
= memcached_server_push(memc
, servers
);
129 memcached_server_list_free(servers
);
131 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
133 memcached_free(memc
);
134 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
140 memcached_return_t ret
;
141 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
143 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
144 memcached_free(memc
);
149 if (rc
!= MEMCACHED_SUCCESS
and rc
!= MEMCACHED_SOME_ERRORS
)
151 printf("Failure to communicate with servers (%s)\n",
152 memcached_strerror(memc
, rc
));
156 if (opt_server_version
)
158 if (memcached_failed(memcached_version(memc
)))
160 std::cerr
<< "Unable to obtain server version";
164 memcached_server_fn callbacks
[1];
165 callbacks
[0]= server_print_callback
;
166 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
168 else if (opt_analyze
)
170 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
172 if (memc_stat
== NULL
)
177 run_analyzer(memc
, memc_stat
);
179 memcached_stat_free(memc
, memc_stat
);
183 rc
= memcached_stat_execute(memc
, stat_args
, stat_printer
, NULL
);
186 memcached_free(memc
);
188 return rc
== MEMCACHED_SUCCESS
? EXIT_SUCCESS
: EXIT_FAILURE
;
191 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*memc_stat
)
193 memcached_return_t rc
;
195 if (analyze_mode
== NULL
)
197 memcached_analysis_st
*report
;
198 report
= memcached_analyze(memc
, memc_stat
, &rc
);
199 if (rc
!= MEMCACHED_SUCCESS
|| report
== NULL
)
201 printf("Failure to analyze servers (%s)\n",
202 memcached_strerror(memc
, rc
));
205 print_analysis_report(memc
, report
);
208 else if (strcmp(analyze_mode
, "latency") == 0)
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";
214 memcached_st
**servers
;
215 servers
= static_cast<memcached_st
**>(malloc(sizeof(memcached_st
*) * server_count
));
218 fprintf(stderr
, "Failed to allocate memory\n");
222 for (uint32_t x
= 0; x
< server_count
; x
++)
224 memcached_server_instance_st instance
=
225 memcached_server_instance_by_position(memc
, x
);
227 if ((servers
[x
]= memcached_create(NULL
)) == NULL
)
229 fprintf(stderr
, "Failed to memcached_create()\n");
231 memcached_free(servers
[0]);
235 memcached_free(servers
[x
]);
240 memcached_server_add(servers
[x
],
241 memcached_server_name(instance
),
242 memcached_server_port(instance
));
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;
250 for (uint32_t x
= 0; x
< server_count
; x
++)
252 memcached_server_instance_st instance
=
253 memcached_server_instance_by_position(memc
, x
);
254 gettimeofday(&start_time
, NULL
);
256 for (uint32_t y
= 0; y
< num_of_tests
; y
++)
259 char *val
= memcached_get(servers
[x
], test_key
, strlen(test_key
),
261 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
265 gettimeofday(&end_time
, NULL
);
267 elapsed_time
= (long) timedif(end_time
, start_time
);
268 elapsed_time
/= (long) num_of_tests
;
270 if (elapsed_time
> slowest_time
)
273 slowest_time
= elapsed_time
;
276 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
278 printf("\t %s (%d) => failed to reach the server\n",
279 memcached_server_name(instance
),
280 memcached_server_port(instance
));
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);
291 if (server_count
> 1 && slowest_time
> 0)
293 memcached_server_instance_st slowest
=
294 memcached_server_instance_by_position(memc
, slowest_server
);
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);
304 for (uint32_t x
= 0; x
< server_count
; x
++)
305 memcached_free(servers
[x
]);
312 fprintf(stderr
, "Invalid Analyzer Option provided\n");
317 static void print_analysis_report(memcached_st
*memc
,
318 memcached_analysis_st
*report
)
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
);
326 printf("Memcached Cluster Analysis Report\n\n");
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
);
332 if (server_count
== 1)
334 printf("\nFor a detailed report, you must supply multiple servers.\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
);
355 static void options_parse(int argc
, char *argv
[])
357 memcached_programs_help_st help_options
[]=
364 bool opt_version
= false;
365 bool opt_help
= false;
368 int option_rv
= getopt_long(argc
, argv
, "Vhvds:a", long_options
, &option_index
);
378 case OPT_VERBOSE
: /* --verbose or -v */
382 case OPT_DEBUG
: /* --debug or -d */
390 case OPT_SERVER_VERSION
:
391 opt_server_version
= true;
394 case OPT_VERSION
: /* --version or -V */
398 case OPT_HELP
: /* --help or -h */
402 case OPT_SERVERS
: /* --servers or -s */
403 opt_servers
= strdup(optarg
);
407 stat_args
= strdup(optarg
);
410 case OPT_ANALYZE
: /* --analyze or -a */
411 opt_analyze
= OPT_ANALYZE
;
412 analyze_mode
= (optarg
) ? strdup(optarg
) : NULL
;
420 opt_username
= optarg
;
429 /* getopt_long already printed an error message. */
438 version_command(PROGRAM_NAME
);
444 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);