10 #include <libmemcached/memcached.h>
12 #include "client_options.h"
13 #include "utilities.h"
15 #define PROGRAM_NAME "memstat"
16 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
19 static void options_parse(int argc
, char *argv
[]);
20 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*stat
,
21 memcached_server_st
*server_list
, char *mode
);
22 static void print_server_listing(memcached_st
*memc
, memcached_stat_st
*stat
,
23 memcached_server_st
*server_list
);
24 static void print_analysis_report(memcached_st
*memc
,
25 memcached_analysis_st
*report
,
26 memcached_server_st
*server_list
);
28 static int opt_verbose
= 0;
29 static int opt_displayflag
= 0;
30 static int opt_analyze
= 0;
31 static char *opt_servers
= NULL
;
32 static char *analyze_mode
= NULL
;
34 static struct option long_options
[]=
36 {"version", no_argument
, NULL
, OPT_VERSION
},
37 {"help", no_argument
, NULL
, OPT_HELP
},
38 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
39 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
40 {"servers", required_argument
, NULL
, OPT_SERVERS
},
41 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
42 {"analyze", optional_argument
, NULL
, OPT_ANALYZE
},
46 int main(int argc
, char *argv
[])
50 memcached_stat_st
*stat
;
51 memcached_server_st
*servers
;
52 memcached_server_st
*server_list
;
54 options_parse(argc
, argv
);
60 if ((temp
= getenv("MEMCACHED_SERVERS")))
61 opt_servers
= strdup(temp
);
64 fprintf(stderr
, "No Servers provided\n\n");
65 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, 0);
70 memc
= memcached_create(NULL
);
72 servers
= memcached_servers_parse(opt_servers
);
73 memcached_server_push(memc
, servers
);
74 memcached_server_list_free(servers
);
76 stat
= memcached_stat(memc
, NULL
, &rc
);
78 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_SOME_ERRORS
)
80 printf("Failure to communicate with servers (%s)\n",
81 memcached_strerror(memc
, rc
));
85 server_list
= memcached_server_list(memc
);
88 run_analyzer(memc
, stat
, server_list
, analyze_mode
);
90 print_server_listing(memc
, stat
, server_list
);
100 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*stat
,
101 memcached_server_st
*server_list
, char *mode
)
105 if (analyze_mode
== NULL
)
107 memcached_analysis_st
*report
;
108 report
= memcached_analyze(memc
, stat
, &rc
);
109 if (rc
!= MEMCACHED_SUCCESS
|| report
== NULL
)
111 printf("Failure to analyze servers (%s)\n",
112 memcached_strerror(memc
, rc
));
115 print_analysis_report(memc
, report
, server_list
);
120 /* More code here in the next patch */
125 static void print_server_listing(memcached_st
*memc
, memcached_stat_st
*stat
,
126 memcached_server_st
*server_list
)
131 printf("Listing %u Server\n\n", memcached_server_count(memc
));
132 for (x
= 0; x
< memcached_server_count(memc
); x
++)
137 list
= memcached_stat_get_keys(memc
, &stat
[x
], &rc
);
139 printf("Server: %s (%u)\n", memcached_server_name(memc
, server_list
[x
]),
140 memcached_server_port(memc
, server_list
[x
]));
141 for (ptr
= list
; *ptr
; ptr
++)
144 char *value
= memcached_stat_get_value(memc
, &stat
[x
], *ptr
, &rc
);
146 printf("\t %s: %s\n", *ptr
, value
);
155 static void print_analysis_report(memcached_st
*memc
,
156 memcached_analysis_st
*report
,
157 memcached_server_st
*server_list
)
159 uint32_t server_count
= memcached_server_count(memc
);
161 printf("Memcached Cluster Analysis Report\n\n");
163 printf("\tNumber of Servers Analyzed : %d\n", server_count
);
164 printf("\tAverage Item Size (incl/overhead) : %u bytes\n",
165 report
->average_item_size
);
167 if (server_count
== 1)
169 printf("\nFor a detailed report, you must supply multiple servers.\n");
174 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
175 memcached_server_name(memc
, server_list
[report
->most_consumed_server
]),
176 memcached_server_port(memc
, server_list
[report
->most_consumed_server
]),
177 (unsigned long long)report
->most_used_bytes
);
178 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
179 memcached_server_name(memc
, server_list
[report
->least_free_server
]),
180 memcached_server_port(memc
, server_list
[report
->least_free_server
]),
181 (unsigned long long)report
->least_remaining_bytes
);
182 printf("\tNode with longest uptime : %s:%u (%us)\n",
183 memcached_server_name(memc
, server_list
[report
->oldest_server
]),
184 memcached_server_port(memc
, server_list
[report
->oldest_server
]),
185 report
->longest_uptime
);
186 printf("\tPool-wide Hit Ratio : %1.f%%\n", report
->pool_hit_ratio
);
190 static void options_parse(int argc
, char *argv
[])
192 memcached_programs_help_st help_options
[]=
202 option_rv
= getopt_long(argc
, argv
, "Vhvds:a", long_options
, &option_index
);
203 if (option_rv
== -1) break;
208 case OPT_VERBOSE
: /* --verbose or -v */
209 opt_verbose
= OPT_VERBOSE
;
211 case OPT_DEBUG
: /* --debug or -d */
212 opt_verbose
= OPT_DEBUG
;
214 case OPT_VERSION
: /* --version or -V */
215 version_command(PROGRAM_NAME
);
217 case OPT_HELP
: /* --help or -h */
218 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
220 case OPT_SERVERS
: /* --servers or -s */
221 opt_servers
= strdup(optarg
);
223 case OPT_ANALYZE
: /* --analyze or -a */
224 opt_analyze
= OPT_ANALYZE
;
225 analyze_mode
= (optarg
) ? strdup(optarg
) : NULL
;
228 /* getopt_long already printed an error message. */