a2dc17a3e0e56fc0a444893c103fa673b3eb996e
[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 if (!memcmp("pid", key, strlen("pid")))
156 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid);
157 else if (!memcmp("uptime", key, strlen("uptime")))
158 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->uptime);
159 else if (!memcmp("time", key, strlen("time")))
160 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->time);
161 else if (!memcmp("version", key, strlen("version")))
162 snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version);
163 else if (!memcmp("pointer_size", key, strlen("pointer_size")))
164 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size);
165 else if (!memcmp("rusage_user", key, strlen("rusage_user")))
166 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user);
167 else if (!memcmp("rusage_system", key, strlen("rusage_system")))
168 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system);
169 else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds")))
170 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user_seconds);
171 else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds")))
172 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user_microseconds);
173 else if (!memcmp("rusage_system_seconds", key, strlen("rusage_system_seconds")))
174 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system_seconds);
175 else if (!memcmp("rusage_system_microseconds", key, strlen("rusage_system_microseconds")))
176 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system_microseconds);
177 else if (!memcmp("curr_items", key, strlen("curr_items")))
178 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_items);
179 else if (!memcmp("total_items", key, strlen("total_items")))
180 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items);
181 else if (!memcmp("bytes", key, strlen("bytes")))
182 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes);
183 else if (!memcmp("curr_connections", key, strlen("curr_connections")))
184 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections);
185 else if (!memcmp("total_connections", key, strlen("total_connections")))
186 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_connections);
187 else if (!memcmp("connection_structures", key, strlen("connection_structures")))
188 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures);
189 else if (!memcmp("cmd_get", key, strlen("cmd_get")))
190 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->cmd_get);
191 else if (!memcmp("cmd_set", key, strlen("cmd_set")))
192 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->cmd_set);
193 else if (!memcmp("get_hits", key, strlen("get_hits")))
194 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->get_hits);
195 else if (!memcmp("get_misses", key, strlen("get_misses")))
196 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->get_misses);
197 else if (!memcmp("evictions", key, strlen("evictions")))
198 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->evictions);
199 else if (!memcmp("bytes_read", key, strlen("bytes_read")))
200 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes_read);
201 else if (!memcmp("bytes_written", key, strlen("bytes_written")))
202 snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes_written);
203 else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
204 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->limit_maxbytes);
205 else if (!memcmp("threads", key, strlen("threads")))
206 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads);
207 else
208 {
209 *error= MEMCACHED_NOTFOUND;
210 return NULL;
211 }
212
213 return strdup(buffer);
214 }
215
216 static memcached_return memcached_stats_fetch(memcached_st *ptr,
217 memcached_stat_st *stat,
218 char *args,
219 unsigned int server_key)
220 {
221 memcached_return rc;
222 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
223 size_t send_length;
224
225 rc= memcached_connect(ptr, server_key);
226
227 if (rc != MEMCACHED_SUCCESS)
228 return rc;
229
230 if (args)
231 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
232 "stats %s\r\n", args);
233 else
234 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
235 "stats\r\n");
236
237 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
238 return MEMCACHED_WRITE_FAILURE;
239
240 rc= memcached_do(ptr, server_key, buffer, send_length, 1);
241 if (rc != MEMCACHED_SUCCESS)
242 goto error;
243
244 while (1)
245 {
246 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
247
248 if (rc == MEMCACHED_STAT)
249 {
250 char *string_ptr, *end_ptr;
251 char *key, *value;
252
253 string_ptr= buffer;
254 string_ptr+= 5; /* Move past STAT */
255 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
256 key= string_ptr;
257 key[(size_t)(end_ptr-string_ptr)]= 0;
258
259 string_ptr= end_ptr + 1;
260 for (end_ptr= string_ptr; *end_ptr != '\r'; end_ptr++);
261 value= string_ptr;
262 value[(size_t)(end_ptr-string_ptr)]= 0;
263 string_ptr= end_ptr + 2;
264 set_data(stat, key, value);
265 }
266 else
267 break;
268 }
269
270 error:
271 if (rc == MEMCACHED_END)
272 return MEMCACHED_SUCCESS;
273 else
274 return rc;
275 }
276
277 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error)
278 {
279 unsigned int x;
280 memcached_return rc;
281 memcached_stat_st *stats;
282
283 stats= (memcached_stat_st *)malloc(sizeof(memcached_st)*(ptr->number_of_hosts+1));
284 if (!stats)
285 {
286 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
287 free(stats);
288 return NULL;
289 }
290 memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts+1));
291
292 rc= MEMCACHED_SUCCESS;
293 for (x= 0; x < ptr->number_of_hosts; x++)
294 {
295 memcached_return temp_return;
296 temp_return= memcached_stats_fetch(ptr, stats+x, args, x);
297 if (temp_return != MEMCACHED_SUCCESS)
298 rc= MEMCACHED_SOME_ERRORS;
299 }
300
301 *error= rc;
302 return stats;
303 }
304
305 memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args,
306 char *hostname, unsigned int port)
307 {
308 memcached_return rc;
309 memcached_st memc;
310
311 memcached_create(&memc);
312
313 memcached_server_add(&memc, hostname, port);
314
315 rc= memcached_stats_fetch(&memc, stat, args, 0);
316
317 memcached_free(&memc);
318
319 return rc;
320 }
321
322 /*
323 We make a copy of the keys since at some point in the not so distant future
324 we will add support for "found" keys.
325 */
326 char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat,
327 memcached_return *error)
328 {
329 char **list= (char **)malloc(sizeof(memcached_stat_keys));
330
331 memset(list, 0, sizeof(memcached_stat_keys));
332
333 if (!list)
334 {
335 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
336 return NULL;
337 }
338
339 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
340
341 *error= MEMCACHED_SUCCESS;
342
343 return list;
344 }
345
346 void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat)
347 {
348 free(stat);
349 }