/
memstat.c
216 lines (183 loc) · 5.89 KB
/
memstat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <getopt.h>
#include <libmemcached/memcached.h>
#include "client_options.h"
#include "utilities.h"
#define PROGRAM_NAME "memstat"
#define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
/* Prototypes */
static void options_parse(int argc, char *argv[]);
static void print_server_listing(memcached_st *memc, memcached_stat_st *stat,
memcached_server_st *server_list);
static void print_analysis_report(memcached_st *memc,
memcached_analysis_st *report,
memcached_server_st *server_list);
static int opt_verbose= 0;
static int opt_displayflag= 0;
static int opt_analyze= 0;
static char *opt_servers= NULL;
static struct option long_options[]=
{
{"version", no_argument, NULL, OPT_VERSION},
{"help", no_argument, NULL, OPT_HELP},
{"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
{"debug", no_argument, &opt_verbose, OPT_DEBUG},
{"servers", required_argument, NULL, OPT_SERVERS},
{"flag", no_argument, &opt_displayflag, OPT_FLAG},
{"analyze", no_argument, NULL, OPT_ANALYZE},
{0, 0, 0, 0},
};
int main(int argc, char *argv[])
{
memcached_return rc;
memcached_st *memc;
memcached_stat_st *stat;
memcached_server_st *servers;
memcached_server_st *server_list;
memcached_analysis_st *report;
options_parse(argc, argv);
if (!opt_servers)
{
char *temp;
if ((temp= getenv("MEMCACHED_SERVERS")))
opt_servers= strdup(temp);
else
{
fprintf(stderr, "No Servers provided\n\n");
help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, 0);
exit(1);
}
}
memc= memcached_create(NULL);
servers= memcached_servers_parse(opt_servers);
memcached_server_push(memc, servers);
memcached_server_list_free(servers);
stat= memcached_stat(memc, NULL, &rc);
if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_SOME_ERRORS)
{
printf("Failure to communicate with servers (%s)\n",
memcached_strerror(memc, rc));
exit(1);
}
server_list= memcached_server_list(memc);
if (opt_analyze)
{
report= memcached_analyze(memc, stat, &rc);
if (rc != MEMCACHED_SUCCESS || report == NULL)
{
printf("Failure to analyze servers (%s)\n",
memcached_strerror(memc, rc));
exit(1);
}
print_analysis_report(memc, report, server_list);
free(report);
}
else
print_server_listing(memc, stat, server_list);
free(stat);
free(opt_servers);
memcached_free(memc);
return 0;
}
static void print_server_listing(memcached_st *memc, memcached_stat_st *stat,
memcached_server_st *server_list)
{
unsigned int x;
memcached_return rc;
printf("Listing %u Server\n\n", memcached_server_count(memc));
for (x= 0; x < memcached_server_count(memc); x++)
{
char **list;
char **ptr;
list= memcached_stat_get_keys(memc, &stat[x], &rc);
printf("Server: %s (%u)\n", memcached_server_name(memc, server_list[x]),
memcached_server_port(memc, server_list[x]));
for (ptr= list; *ptr; ptr++)
{
memcached_return rc;
char *value= memcached_stat_get_value(memc, &stat[x], *ptr, &rc);
printf("\t %s: %s\n", *ptr, value);
free(value);
}
free(list);
printf("\n");
}
}
static void print_analysis_report(memcached_st *memc,
memcached_analysis_st *report,
memcached_server_st *server_list)
{
uint32_t server_count= memcached_server_count(memc);
printf("Memcached Cluster Analysis Report\n\n");
printf("\tNumber of Servers Analyzed : %d\n", server_count);
printf("\tAverage Item Size (incl/overhead) : %u bytes\n",
report->average_item_size);
if (server_count == 1)
{
printf("\nFor a detailed report, you must supply multiple servers.\n");
return;
}
printf("\n");
printf("\tNode with most memory consumption : %s:%u (%u bytes)\n",
memcached_server_name(memc, server_list[report->most_consumed_server]),
memcached_server_port(memc, server_list[report->most_consumed_server]),
report->most_used_bytes);
printf("\tNode with least free space : %s:%u (%u bytes remaining)\n",
memcached_server_name(memc, server_list[report->least_free_server]),
memcached_server_port(memc, server_list[report->least_free_server]),
report->least_remaining_bytes);
printf("\tNode with longest uptime : %s:%u (%us)\n",
memcached_server_name(memc, server_list[report->oldest_server]),
memcached_server_port(memc, server_list[report->oldest_server]),
report->longest_uptime);
printf("\tPool-wide Hit Ratio : %1.f%%\n", report->pool_hit_ratio);
printf("\n");
}
static void options_parse(int argc, char *argv[])
{
memcached_programs_help_st help_options[]=
{
{0},
};
int option_index= 0;
int option_rv;
while (1)
{
option_rv= getopt_long(argc, argv, "Vhvds:a", long_options, &option_index);
if (option_rv == -1) break;
switch (option_rv)
{
case 0:
break;
case OPT_VERBOSE: /* --verbose or -v */
opt_verbose = OPT_VERBOSE;
break;
case OPT_DEBUG: /* --debug or -d */
opt_verbose = OPT_DEBUG;
break;
case OPT_VERSION: /* --version or -V */
version_command(PROGRAM_NAME);
break;
case OPT_HELP: /* --help or -h */
help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
break;
case OPT_SERVERS: /* --servers or -s */
opt_servers= strdup(optarg);
break;
case OPT_ANALYZE: /* --analyze or -a */
opt_analyze= OPT_ANALYZE;
break;
case '?':
/* getopt_long already printed an error message. */
exit(1);
default:
abort();
}
}
}