2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "mem_config.h"
27 #include <sys/types.h>
28 #include <sys/types.h>
30 #include "libmemcached-1.0/memcached.h"
32 #include "client_options.h"
33 #include "utilities.h"
35 #define PROGRAM_NAME "memstat"
36 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
39 static void options_parse(int argc
, char *argv
[]);
40 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*memc_stat
);
41 static void print_analysis_report(memcached_st
*memc
, 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
[] = {
54 {(OPTIONSTRING
) "args", required_argument
, NULL
, OPT_STAT_ARGS
},
55 {(OPTIONSTRING
) "version", no_argument
, NULL
, OPT_VERSION
},
56 {(OPTIONSTRING
) "help", no_argument
, NULL
, OPT_HELP
},
57 {(OPTIONSTRING
) "quiet", no_argument
, NULL
, OPT_QUIET
},
58 {(OPTIONSTRING
) "verbose", no_argument
, NULL
, OPT_VERBOSE
},
59 {(OPTIONSTRING
) "binary", no_argument
, NULL
, OPT_BINARY
},
60 {(OPTIONSTRING
) "debug", no_argument
, NULL
, OPT_DEBUG
},
61 {(OPTIONSTRING
) "server-version", no_argument
, NULL
, OPT_SERVER_VERSION
},
62 {(OPTIONSTRING
) "servers", required_argument
, NULL
, OPT_SERVERS
},
63 {(OPTIONSTRING
) "analyze", optional_argument
, NULL
, OPT_ANALYZE
},
64 {(OPTIONSTRING
) "username", required_argument
, NULL
, OPT_USERNAME
},
65 {(OPTIONSTRING
) "password", required_argument
, NULL
, OPT_PASSWD
},
69 static memcached_return_t
stat_printer(const memcached_instance_st
*instance
, const char *key
,
70 size_t key_length
, const char *value
, size_t value_length
,
72 static const memcached_instance_st
*last
= NULL
;
75 if (last
!= instance
) {
76 printf("Server: %s (%u)\n", memcached_server_name(instance
),
77 (uint32_t) memcached_server_port(instance
));
81 printf("\t %.*s: %.*s\n", (int) key_length
, key
, (int) value_length
, value
);
83 return MEMCACHED_SUCCESS
;
86 static memcached_return_t
server_print_callback(const memcached_st
*,
87 const memcached_instance_st
*instance
, void *) {
88 std::cerr
<< memcached_server_name(instance
) << ":" << memcached_server_port(instance
) << " "
89 << int(memcached_server_major_version(instance
)) << "."
90 << int(memcached_server_minor_version(instance
)) << "."
91 << int(memcached_server_micro_version(instance
)) << std::endl
;
93 return MEMCACHED_SUCCESS
;
96 int main(int argc
, char *argv
[]) {
97 options_parse(argc
, argv
);
100 if (opt_servers
== NULL
) {
102 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
103 opt_servers
= strdup(temp
);
106 if (opt_servers
== NULL
) {
107 std::cerr
<< "No Servers provided" << std::endl
;
112 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
113 if (servers
== NULL
or memcached_server_list_count(servers
) == 0) {
114 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
122 memcached_st
*memc
= memcached_create(NULL
);
123 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
125 memcached_return_t rc
= memcached_server_push(memc
, servers
);
126 memcached_server_list_free(servers
);
128 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
129 memcached_free(memc
);
130 std::cerr
<< "--username was supplied, but binary was not built with SASL support."
136 memcached_return_t ret
;
137 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
))) {
138 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
139 memcached_free(memc
);
144 if (rc
!= MEMCACHED_SUCCESS
and rc
!= MEMCACHED_SOME_ERRORS
) {
145 printf("Failure to communicate with servers (%s)\n", memcached_strerror(memc
, rc
));
149 if (opt_server_version
) {
150 if (memcached_failed(memcached_version(memc
))) {
151 std::cerr
<< "Unable to obtain server version" << std::endl
;
155 memcached_server_fn callbacks
[1];
156 callbacks
[0] = server_print_callback
;
157 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
158 } else if (opt_analyze
) {
159 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
161 if (memc_stat
== NULL
|| rc
!= MEMCACHED_SUCCESS
) {
162 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
166 run_analyzer(memc
, memc_stat
);
168 memcached_stat_free(memc
, memc_stat
);
170 rc
= memcached_stat_execute(memc
, stat_args
, stat_printer
, NULL
);
173 memcached_free(memc
);
175 return rc
== MEMCACHED_SUCCESS
? EXIT_SUCCESS
: EXIT_FAILURE
;
178 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*memc_stat
) {
179 memcached_return_t rc
;
181 if (analyze_mode
== NULL
) {
182 memcached_analysis_st
*report
;
183 report
= memcached_analyze(memc
, memc_stat
, &rc
);
184 if (rc
!= MEMCACHED_SUCCESS
|| report
== NULL
) {
185 printf("Failure to analyze servers (%s)\n", memcached_strerror(memc
, rc
));
188 print_analysis_report(memc
, report
);
190 } else if (strcmp(analyze_mode
, "latency") == 0) {
191 uint32_t flags
, server_count
= memcached_server_count(memc
);
192 uint32_t num_of_tests
= 32;
193 const char *test_key
= "libmemcached_test_key";
195 memcached_st
**servers
=
196 static_cast<memcached_st
**>(malloc(sizeof(memcached_st
*) * server_count
));
197 if (servers
== NULL
) {
198 fprintf(stderr
, "Failed to allocate memory\n");
202 for (uint32_t x
= 0; x
< server_count
; x
++) {
203 const memcached_instance_st
*instance
= memcached_server_instance_by_position(memc
, x
);
205 if ((servers
[x
] = memcached_create(NULL
)) == NULL
) {
206 fprintf(stderr
, "Failed to memcached_create()\n");
208 memcached_free(servers
[0]);
213 memcached_free(servers
[x
]);
220 memcached_server_add(servers
[x
], memcached_server_name(instance
),
221 memcached_server_port(instance
));
224 printf("Network Latency Test:\n\n");
225 struct timeval start_time
, end_time
;
226 uint32_t slowest_server
= 0;
227 long elapsed_time
, slowest_time
= 0;
229 for (uint32_t x
= 0; x
< server_count
; x
++) {
230 const memcached_instance_st
*instance
= memcached_server_instance_by_position(memc
, x
);
231 gettimeofday(&start_time
, NULL
);
233 for (uint32_t y
= 0; y
< num_of_tests
; y
++) {
235 char *val
= memcached_get(servers
[x
], test_key
, strlen(test_key
), &vlen
, &flags
, &rc
);
236 if (rc
!= MEMCACHED_NOTFOUND
and rc
!= MEMCACHED_SUCCESS
) {
241 gettimeofday(&end_time
, NULL
);
243 elapsed_time
= (long) timedif(end_time
, start_time
);
244 elapsed_time
/= (long) num_of_tests
;
246 if (elapsed_time
> slowest_time
) {
248 slowest_time
= elapsed_time
;
251 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
) {
252 printf("\t %s (%d) => failed to reach the server\n", memcached_server_name(instance
),
253 memcached_server_port(instance
));
255 printf("\t %s (%d) => %ld.%ld seconds\n", memcached_server_name(instance
),
256 memcached_server_port(instance
), elapsed_time
/ 1000, elapsed_time
% 1000);
260 if (server_count
> 1 && slowest_time
> 0) {
261 const memcached_instance_st
*slowest
=
262 memcached_server_instance_by_position(memc
, slowest_server
);
265 printf("Slowest Server: %s (%d) => %ld.%ld seconds\n", memcached_server_name(slowest
),
266 memcached_server_port(slowest
), slowest_time
/ 1000, slowest_time
% 1000);
270 for (uint32_t x
= 0; x
< server_count
; x
++) {
271 memcached_free(servers
[x
]);
277 fprintf(stderr
, "Invalid Analyzer Option provided\n");
282 static void print_analysis_report(memcached_st
*memc
, memcached_analysis_st
*report
)
285 uint32_t server_count
= memcached_server_count(memc
);
286 const memcached_instance_st
*most_consumed_server
=
287 memcached_server_instance_by_position(memc
, report
->most_consumed_server
);
288 const memcached_instance_st
*least_free_server
=
289 memcached_server_instance_by_position(memc
, report
->least_free_server
);
290 const memcached_instance_st
*oldest_server
=
291 memcached_server_instance_by_position(memc
, report
->oldest_server
);
293 printf("Memcached Cluster Analysis Report\n\n");
295 printf("\tNumber of Servers Analyzed : %u\n", server_count
);
296 printf("\tAverage Item Size (incl/overhead) : %u bytes\n", report
->average_item_size
);
298 if (server_count
== 1) {
299 printf("\nFor a detailed report, you must supply multiple servers.\n");
304 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
305 memcached_server_name(most_consumed_server
),
306 (uint32_t) memcached_server_port(most_consumed_server
),
307 (unsigned long long) report
->most_used_bytes
);
308 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
309 memcached_server_name(least_free_server
),
310 (uint32_t) memcached_server_port(least_free_server
),
311 (unsigned long long) report
->least_remaining_bytes
);
312 printf("\tNode with longest uptime : %s:%u (%us)\n",
313 memcached_server_name(oldest_server
), (uint32_t) memcached_server_port(oldest_server
),
314 report
->longest_uptime
);
315 printf("\tPool-wide Hit Ratio : %1.f%%\n", report
->pool_hit_ratio
);
319 static void options_parse(int argc
, char *argv
[]) {
320 memcached_programs_help_st help_options
[] = {
324 int option_index
= 0;
326 bool opt_version
= false;
327 bool opt_help
= false;
329 int option_rv
= getopt_long(argc
, argv
, "Vhvds:a", long_options
, &option_index
);
338 case OPT_VERBOSE
: /* --verbose or -v */
342 case OPT_DEBUG
: /* --debug or -d */
350 case OPT_SERVER_VERSION
:
351 opt_server_version
= true;
354 case OPT_VERSION
: /* --version or -V */
358 case OPT_HELP
: /* --help or -h */
362 case OPT_SERVERS
: /* --servers or -s */
363 opt_servers
= strdup(optarg
);
367 stat_args
= strdup(optarg
);
370 case OPT_ANALYZE
: /* --analyze or -a */
372 analyze_mode
= (optarg
) ? strdup(optarg
) : NULL
;
380 opt_username
= optarg
;
389 /* getopt_long already printed an error message. */
397 version_command(PROGRAM_NAME
);
402 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);