cbfdef3503b4009cd608dd4e987a129e52f854d3
[awesomized/libmemcached] / lib / memcached_stats.c
1 /*
2 */
3
4 #include "common.h"
5
6 static char *memcached_stat_keys[] = {
7 "pid",
8 "uptime",
9 "time",
10 "version",
11 "pointer_size",
12 "rusage_user",
13 "rusage_system",
14 "rusage_user_seconds",
15 "rusage_user_microseconds",
16 "rusage_system_seconds",
17 "rusage_system_microseconds",
18 "curr_items",
19 "total_items",
20 "bytes",
21 "curr_connections",
22 "total_connections",
23 "connection_structures",
24 "cmd_get",
25 "cmd_set",
26 "get_hits",
27 "get_misses",
28 "evictions",
29 "bytes_read",
30 "bytes_written",
31 "limit_maxbytes",
32 "threads",
33 NULL
34 };
35
36
37 static void set_data(memcached_stat_st *stat, char *key, char *value)
38 {
39 if (!memcmp("pid", key, strlen("pid")))
40 {
41 stat->pid= strtol(value, (char **)NULL, 10);
42 }
43 else if (!memcmp("uptime", key, strlen("uptime")))
44 {
45 stat->uptime= strtol(value, (char **)NULL, 10);
46 }
47 else if (!memcmp("time", key, strlen("time")))
48 {
49 stat->time= strtoll(value, (char **)NULL, 10);
50 }
51 else if (!memcmp("version", key, strlen("version")))
52 {
53 memcpy(stat->version, value, 8);
54 }
55 else if (!memcmp("pointer_size", key, strlen("pointer_size")))
56 {
57 stat->pointer_size= strtoll(value, (char **)NULL, 10);
58 }
59 else if (!memcmp("rusage_user", key, strlen("rusage_user")))
60 {
61 stat->rusage_user= strtoll(value, (char **)NULL, 10);
62 }
63 else if (!memcmp("rusage_system", key, strlen("rusage_system")))
64 {
65 stat->rusage_system= strtoll(value, (char **)NULL, 10);
66 }
67 else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds")))
68 {
69 stat->rusage_user_seconds= strtoll(value, (char **)NULL, 10);
70 }
71 else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds")))
72 {
73 stat->rusage_user_microseconds= strtoll(value, (char **)NULL, 10);
74 }
75 else if (!memcmp("rusage_system_seconds", key, strlen("rusage_system_seconds")))
76 {
77 stat->rusage_system_seconds= strtoll(value, (char **)NULL, 10);
78 }
79 else if (!memcmp("rusage_system_microseconds", key, strlen("rusage_system_microseconds")))
80 {
81 stat->rusage_system_microseconds= strtoll(value, (char **)NULL, 10);
82 }
83 else if (!memcmp("curr_items", key, strlen("curr_items")))
84 {
85 stat->curr_items= strtoll(value, (char **)NULL, 10);
86 }
87 else if (!memcmp("total_items", key, strlen("total_items")))
88 {
89 stat->total_items= strtoll(value, (char **)NULL, 10);
90 }
91 else if (!memcmp("bytes", key, strlen("bytes")))
92 {
93 stat->bytes= strtoll(value, (char **)NULL, 10);
94 }
95 else if (!memcmp("curr_connections", key, strlen("curr_connections")))
96 {
97 stat->curr_connections= strtoll(value, (char **)NULL, 10);
98 }
99 else if (!memcmp("total_connections", key, strlen("total_connections")))
100 {
101 stat->total_connections= strtoll(value, (char **)NULL, 10);
102 }
103 else if (!memcmp("connection_structures", key, strlen("connection_structures")))
104 {
105 stat->connection_structures= strtoll(value, (char **)NULL, 10);
106 }
107 else if (!memcmp("cmd_get", key, strlen("cmd_get")))
108 {
109 stat->cmd_get= strtoll(value, (char **)NULL, 10);
110 }
111 else if (!memcmp("cmd_set", key, strlen("cmd_set")))
112 {
113 stat->cmd_set= strtoll(value, (char **)NULL, 10);
114 }
115 else if (!memcmp("get_hits", key, strlen("get_hits")))
116 {
117 stat->get_hits= strtoll(value, (char **)NULL, 10);
118 }
119 else if (!memcmp("get_misses", key, strlen("get_misses")))
120 {
121 stat->get_misses= strtoll(value, (char **)NULL, 10);
122 }
123 else if (!memcmp("evictions", key, strlen("evictions")))
124 {
125 stat->evictions= strtoll(value, (char **)NULL, 10);
126 }
127 else if (!memcmp("bytes_read", key, strlen("bytes_read")))
128 {
129 stat->bytes_read= strtoll(value, (char **)NULL, 10);
130 }
131 else if (!memcmp("bytes_written", key, strlen("bytes_written")))
132 {
133 stat->bytes_written= strtoll(value, (char **)NULL, 10);
134 }
135 else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
136 {
137 stat->limit_maxbytes= strtoll(value, (char **)NULL, 10);
138 }
139 else if (!memcmp("threads", key, strlen("threads")))
140 {
141 stat->threads= strtol(key, (char **)NULL, 10);
142 }
143 else
144 {
145 fprintf(stderr, "Unknown key %s\n", key);
146 }
147 }
148
149 char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat,
150 char *key, memcached_return *error)
151 {
152 char buffer[SMALL_STRING_LEN];
153 *error= MEMCACHED_SUCCESS;
154
155
156 memset(buffer, 0, SMALL_STRING_LEN);
157
158 if (!memcmp("pid", key, strlen("pid")))
159 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid);
160 else if (!memcmp("uptime", key, strlen("uptime")))
161 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->uptime);
162 else if (!memcmp("time", key, strlen("time")))
163 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->time);
164 else if (!memcmp("version", key, strlen("version")))
165 snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version);
166 else if (!memcmp("pointer_size", key, strlen("pointer_size")))
167 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size);
168 else if (!memcmp("rusage_user", key, strlen("rusage_user")))
169 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user);
170 else if (!memcmp("rusage_system", key, strlen("rusage_system")))
171 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system);
172 else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds")))
173 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user_seconds);
174 else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds")))
175 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user_microseconds);
176 else if (!memcmp("rusage_system_seconds", key, strlen("rusage_system_seconds")))
177 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system_seconds);
178 else if (!memcmp("rusage_system_microseconds", key, strlen("rusage_system_microseconds")))
179 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system_microseconds);
180 else if (!memcmp("curr_items", key, strlen("curr_items")))
181 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_items);
182 else if (!memcmp("total_items", key, strlen("total_items")))
183 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items);
184 else if (!memcmp("bytes", key, strlen("bytes")))
185 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes);
186 else if (!memcmp("curr_connections", key, strlen("curr_connections")))
187 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections);
188 else if (!memcmp("total_connections", key, strlen("total_connections")))
189 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_connections);
190 else if (!memcmp("connection_structures", key, strlen("connection_structures")))
191 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures);
192 else if (!memcmp("cmd_get", key, strlen("cmd_get")))
193 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->cmd_get);
194 else if (!memcmp("cmd_set", key, strlen("cmd_set")))
195 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->cmd_set);
196 else if (!memcmp("get_hits", key, strlen("get_hits")))
197 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->get_hits);
198 else if (!memcmp("get_misses", key, strlen("get_misses")))
199 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->get_misses);
200 else if (!memcmp("evictions", key, strlen("evictions")))
201 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->evictions);
202 else if (!memcmp("bytes_read", key, strlen("bytes_read")))
203 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes_read);
204 else if (!memcmp("bytes_written", key, strlen("bytes_written")))
205 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes_written);
206 else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
207 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->limit_maxbytes);
208 else if (!memcmp("threads", key, strlen("threads")))
209 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads);
210 else
211 {
212 *error= MEMCACHED_NOTFOUND;
213 return NULL;
214 }
215
216 return strdup(buffer);
217 }
218
219 static memcached_return memcached_stats_fetch(memcached_st *ptr,
220 memcached_stat_st *stat,
221 char *args,
222 unsigned int server_key)
223 {
224 memcached_return rc;
225 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
226 size_t send_length;
227
228 rc= memcached_connect(ptr, server_key);
229
230 if (rc != MEMCACHED_SUCCESS)
231 return rc;
232
233 if (args)
234 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
235 "stats %s\r\n", args);
236 else
237 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
238 "stats\r\n");
239
240 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
241 return MEMCACHED_WRITE_FAILURE;
242
243 rc= memcached_do(ptr, server_key, buffer, send_length, 1);
244 if (rc != MEMCACHED_SUCCESS)
245 goto error;
246
247 while (1)
248 {
249 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
250
251 if (rc == MEMCACHED_STAT)
252 {
253 char *string_ptr, *end_ptr;
254 char *key, *value;
255
256 string_ptr= buffer;
257 string_ptr+= 5; /* Move past STAT */
258 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
259 key= string_ptr;
260 key[(size_t)(end_ptr-string_ptr)]= 0;
261
262 string_ptr= end_ptr + 1;
263 for (end_ptr= string_ptr; *end_ptr != '\r'; end_ptr++);
264 value= string_ptr;
265 value[(size_t)(end_ptr-string_ptr)]= 0;
266 string_ptr= end_ptr + 2;
267 set_data(stat, key, value);
268 }
269 else
270 break;
271 }
272
273 error:
274 if (rc == MEMCACHED_END)
275 return MEMCACHED_SUCCESS;
276 else
277 return rc;
278 }
279
280 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error)
281 {
282 unsigned int x;
283 memcached_return rc;
284 memcached_stat_st *stats;
285
286 stats= (memcached_stat_st *)malloc(sizeof(memcached_st)*(ptr->number_of_hosts+1));
287 if (!stats)
288 {
289 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
290 free(stats);
291 return NULL;
292 }
293 memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts+1));
294
295 rc= MEMCACHED_SUCCESS;
296 for (x= 0; x < ptr->number_of_hosts; x++)
297 {
298 memcached_return temp_return;
299 temp_return= memcached_stats_fetch(ptr, stats+x, args, x);
300 if (temp_return != MEMCACHED_SUCCESS)
301 rc= MEMCACHED_SOME_ERRORS;
302 }
303
304 *error= rc;
305 return stats;
306 }
307
308 memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args,
309 char *hostname, unsigned int port)
310 {
311 memcached_return rc;
312 memcached_st memc;
313
314 memcached_create(&memc);
315
316 memcached_server_add(&memc, hostname, port);
317
318 rc= memcached_stats_fetch(&memc, stat, args, 0);
319
320 memcached_free(&memc);
321
322 return rc;
323 }
324
325 /*
326 We make a copy of the keys since at some point in the not so distant future
327 we will add support for "found" keys.
328 */
329 char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat,
330 memcached_return *error)
331 {
332 char **list= (char **)malloc(sizeof(memcached_stat_keys));
333
334 memset(list, 0, sizeof(memcached_stat_keys));
335
336 if (!list)
337 {
338 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
339 return NULL;
340 }
341
342 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
343
344 *error= MEMCACHED_SUCCESS;
345
346 return list;
347 }
348
349 void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat)
350 {
351 free(stat);
352 }