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
*stat
,
23 memcached_server_st
*server_list
);
24 static void print_server_listing(memcached_st
*memc
, memcached_stat_st
*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 {"version", no_argument
, NULL
, OPT_VERSION
},
39 {"help", no_argument
, NULL
, OPT_HELP
},
40 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
41 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
42 {"servers", required_argument
, NULL
, OPT_SERVERS
},
43 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
44 {"analyze", optional_argument
, NULL
, OPT_ANALYZE
},
48 int main(int argc
, char *argv
[])
52 memcached_stat_st
*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 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
, stat
, server_list
);
92 print_server_listing(memc
, stat
, server_list
);
102 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*stat
,
103 memcached_server_st
*server_list
)
107 if (analyze_mode
== NULL
)
109 memcached_analysis_st
*report
;
110 report
= memcached_analyze(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
/= num_of_tests
;
174 if (elapsed_time
> slowest_time
)
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
*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
, &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
++)
237 char *value
= memcached_stat_get_value(memc
, &stat
[x
], *ptr
, &rc
);
239 printf("\t %s: %s\n", *ptr
, value
);
248 static void print_analysis_report(memcached_st
*memc
,
249 memcached_analysis_st
*report
,
250 memcached_server_st
*server_list
)
252 uint32_t server_count
= memcached_server_count(memc
);
254 printf("Memcached Cluster Analysis Report\n\n");
256 printf("\tNumber of Servers Analyzed : %d\n", server_count
);
257 printf("\tAverage Item Size (incl/overhead) : %u bytes\n",
258 report
->average_item_size
);
260 if (server_count
== 1)
262 printf("\nFor a detailed report, you must supply multiple servers.\n");
267 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
268 memcached_server_name(memc
, server_list
[report
->most_consumed_server
]),
269 memcached_server_port(memc
, server_list
[report
->most_consumed_server
]),
270 (unsigned long long)report
->most_used_bytes
);
271 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
272 memcached_server_name(memc
, server_list
[report
->least_free_server
]),
273 memcached_server_port(memc
, server_list
[report
->least_free_server
]),
274 (unsigned long long)report
->least_remaining_bytes
);
275 printf("\tNode with longest uptime : %s:%u (%us)\n",
276 memcached_server_name(memc
, server_list
[report
->oldest_server
]),
277 memcached_server_port(memc
, server_list
[report
->oldest_server
]),
278 report
->longest_uptime
);
279 printf("\tPool-wide Hit Ratio : %1.f%%\n", report
->pool_hit_ratio
);
283 static void options_parse(int argc
, char *argv
[])
285 memcached_programs_help_st help_options
[]=
295 option_rv
= getopt_long(argc
, argv
, "Vhvds:a", long_options
, &option_index
);
296 if (option_rv
== -1) break;
301 case OPT_VERBOSE
: /* --verbose or -v */
302 opt_verbose
= OPT_VERBOSE
;
304 case OPT_DEBUG
: /* --debug or -d */
305 opt_verbose
= OPT_DEBUG
;
307 case OPT_VERSION
: /* --version or -V */
308 version_command(PROGRAM_NAME
);
310 case OPT_HELP
: /* --help or -h */
311 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
313 case OPT_SERVERS
: /* --servers or -s */
314 opt_servers
= strdup(optarg
);
316 case OPT_ANALYZE
: /* --analyze or -a */
317 opt_analyze
= OPT_ANALYZE
;
318 analyze_mode
= (optarg
) ? strdup(optarg
) : NULL
;
321 /* getopt_long already printed an error message. */