1 #include "libmemcached/common.h"
12 #include <libmemcached/memcached.h>
14 #include "client_options.h"
15 #include "utilities.h"
17 #define PROGRAM_NAME "memstat"
18 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
21 static void options_parse(int argc
, char *argv
[]);
22 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*memc_stat
,
23 memcached_server_st
*server_list
);
24 static void print_server_listing(memcached_st
*memc
, memcached_stat_st
*memc_stat
,
25 memcached_server_st
*server_list
);
26 static void print_analysis_report(memcached_st
*memc
,
27 memcached_analysis_st
*report
,
28 memcached_server_st
*server_list
);
30 static int opt_verbose
= 0;
31 static int opt_displayflag
= 0;
32 static int opt_analyze
= 0;
33 static char *opt_servers
= NULL
;
34 static char *analyze_mode
= NULL
;
36 static struct option long_options
[]=
38 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
39 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
40 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
41 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
42 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
43 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
44 {(OPTIONSTRING
)"analyze", optional_argument
, NULL
, OPT_ANALYZE
},
48 int main(int argc
, char *argv
[])
52 memcached_stat_st
*memc_stat
;
53 memcached_server_st
*servers
;
54 memcached_server_st
*server_list
;
56 options_parse(argc
, argv
);
62 if ((temp
= getenv("MEMCACHED_SERVERS")))
63 opt_servers
= strdup(temp
);
66 fprintf(stderr
, "No Servers provided\n\n");
67 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, 0);
72 memc
= memcached_create(NULL
);
74 servers
= memcached_servers_parse(opt_servers
);
75 memcached_server_push(memc
, servers
);
76 memcached_server_list_free(servers
);
78 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
80 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_SOME_ERRORS
)
82 printf("Failure to communicate with servers (%s)\n",
83 memcached_strerror(memc
, rc
));
87 server_list
= memcached_server_list(memc
);
90 run_analyzer(memc
, memc_stat
, server_list
);
92 print_server_listing(memc
, memc_stat
, server_list
);
102 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*memc_stat
,
103 memcached_server_st
*server_list
)
107 if (analyze_mode
== NULL
)
109 memcached_analysis_st
*report
;
110 report
= memcached_analyze(memc
, memc_stat
, &rc
);
111 if (rc
!= MEMCACHED_SUCCESS
|| report
== NULL
)
113 printf("Failure to analyze servers (%s)\n",
114 memcached_strerror(memc
, rc
));
117 print_analysis_report(memc
, report
, server_list
);
120 else if (strcmp(analyze_mode
, "latency") == 0)
122 memcached_st
**servers
;
123 uint32_t x
, y
, flags
, server_count
= memcached_server_count(memc
);
124 uint32_t num_of_tests
= 32;
125 const char *test_key
= "libmemcached_test_key";
127 servers
= malloc(sizeof(memcached_st
*) * server_count
);
130 fprintf(stderr
, "Failed to allocate memory\n");
134 for (x
= 0; x
< server_count
; x
++)
136 if((servers
[x
]= memcached_create(NULL
)) == NULL
)
138 fprintf(stderr
, "Failed to memcached_create()\n");
140 memcached_free(servers
[0]);
143 memcached_free(servers
[x
]);
148 memcached_server_add(servers
[x
],
149 memcached_server_name(memc
, server_list
[x
]),
150 memcached_server_port(memc
, server_list
[x
]));
153 printf("Network Latency Test:\n\n");
154 struct timeval start_time
, end_time
;
155 long elapsed_time
, slowest_time
= 0, slowest_server
= 0;
157 for (x
= 0; x
< server_count
; x
++)
159 gettimeofday(&start_time
, NULL
);
160 for (y
= 0; y
< num_of_tests
; y
++)
163 char *val
= memcached_get(servers
[x
], test_key
, strlen(test_key
),
165 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
169 gettimeofday(&end_time
, NULL
);
171 elapsed_time
= timedif(end_time
, start_time
);
172 elapsed_time
/= (long)num_of_tests
;
174 if (elapsed_time
> slowest_time
)
176 slowest_server
= (long)x
;
177 slowest_time
= elapsed_time
;
180 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
182 printf("\t %s (%d) => failed to reach the server\n",
183 memcached_server_name(memc
, server_list
[x
]),
184 memcached_server_port(memc
, server_list
[x
]));
188 printf("\t %s (%d) => %ld.%ld seconds\n",
189 memcached_server_name(memc
, server_list
[x
]),
190 memcached_server_port(memc
, server_list
[x
]),
191 elapsed_time
/ 1000, elapsed_time
% 1000);
195 if (server_count
> 1 && slowest_time
> 0)
198 printf("Slowest Server: %s (%d) => %ld.%ld seconds\n",
199 memcached_server_name(memc
, server_list
[slowest_server
]),
200 memcached_server_port(memc
, server_list
[slowest_server
]),
201 slowest_time
/ 1000, slowest_time
% 1000);
205 for (x
= 0; x
< server_count
; x
++)
206 memcached_free(servers
[x
]);
213 fprintf(stderr
, "Invalid Analyzer Option provided\n");
218 static void print_server_listing(memcached_st
*memc
, memcached_stat_st
*memc_stat
,
219 memcached_server_st
*server_list
)
224 printf("Listing %u Server\n\n", memcached_server_count(memc
));
225 for (x
= 0; x
< memcached_server_count(memc
); x
++)
230 list
= memcached_stat_get_keys(memc
, &memc_stat
[x
], &rc
);
232 printf("Server: %s (%u)\n", memcached_server_name(memc
, server_list
[x
]),
233 memcached_server_port(memc
, server_list
[x
]));
234 for (ptr
= list
; *ptr
; ptr
++)
236 char *value
= memcached_stat_get_value(memc
, &memc_stat
[x
], *ptr
, &rc
);
238 printf("\t %s: %s\n", *ptr
, value
);
247 static void print_analysis_report(memcached_st
*memc
,
248 memcached_analysis_st
*report
,
249 memcached_server_st
*server_list
)
251 uint32_t server_count
= memcached_server_count(memc
);
253 printf("Memcached Cluster Analysis Report\n\n");
255 printf("\tNumber of Servers Analyzed : %d\n", server_count
);
256 printf("\tAverage Item Size (incl/overhead) : %u bytes\n",
257 report
->average_item_size
);
259 if (server_count
== 1)
261 printf("\nFor a detailed report, you must supply multiple servers.\n");
266 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
267 memcached_server_name(memc
, server_list
[report
->most_consumed_server
]),
268 memcached_server_port(memc
, server_list
[report
->most_consumed_server
]),
269 (unsigned long long)report
->most_used_bytes
);
270 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
271 memcached_server_name(memc
, server_list
[report
->least_free_server
]),
272 memcached_server_port(memc
, server_list
[report
->least_free_server
]),
273 (unsigned long long)report
->least_remaining_bytes
);
274 printf("\tNode with longest uptime : %s:%u (%us)\n",
275 memcached_server_name(memc
, server_list
[report
->oldest_server
]),
276 memcached_server_port(memc
, server_list
[report
->oldest_server
]),
277 report
->longest_uptime
);
278 printf("\tPool-wide Hit Ratio : %1.f%%\n", report
->pool_hit_ratio
);
282 static void options_parse(int argc
, char *argv
[])
284 memcached_programs_help_st help_options
[]=
294 option_rv
= getopt_long(argc
, argv
, "Vhvds:a", long_options
, &option_index
);
295 if (option_rv
== -1) break;
300 case OPT_VERBOSE
: /* --verbose or -v */
301 opt_verbose
= OPT_VERBOSE
;
303 case OPT_DEBUG
: /* --debug or -d */
304 opt_verbose
= OPT_DEBUG
;
306 case OPT_VERSION
: /* --version or -V */
307 version_command(PROGRAM_NAME
);
309 case OPT_HELP
: /* --help or -h */
310 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
312 case OPT_SERVERS
: /* --servers or -s */
313 opt_servers
= strdup(optarg
);
315 case OPT_ANALYZE
: /* --analyze or -a */
316 opt_analyze
= OPT_ANALYZE
;
317 analyze_mode
= (optarg
) ? strdup(optarg
) : NULL
;
320 /* getopt_long already printed an error message. */