1e3c425b0df693e27180a7335e1cd50f4e0cdcab
[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[HUGE_STRING_LEN];
226 size_t send_length, sent_length;
227
228 rc= memcached_connect(ptr);
229
230 if (rc != MEMCACHED_SUCCESS)
231 return rc;
232
233 if (args)
234 send_length= snprintf(buffer, HUGE_STRING_LEN,
235 "stats %s\r\n", args);
236 else
237 send_length= snprintf(buffer, HUGE_STRING_LEN,
238 "stats\r\n");
239
240 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
241 return MEMCACHED_WRITE_FAILURE;
242
243 sent_length= send(ptr->hosts[server_key].fd, buffer, send_length, 0);
244
245 if (sent_length == -1 || sent_length != send_length)
246 return MEMCACHED_WRITE_FAILURE;
247
248 rc= memcached_response(ptr, buffer, HUGE_STRING_LEN, 0);
249
250 if (rc == MEMCACHED_SUCCESS)
251 {
252 char *string_ptr, *end_ptr;
253 char *key, *value;
254
255 string_ptr= buffer;
256 while (1)
257 {
258 if (memcmp(string_ptr, "STAT ", 5))
259 break;
260 string_ptr+= 5;
261 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
262 key= string_ptr;
263 key[(size_t)(end_ptr-string_ptr)]= 0;
264
265 string_ptr= end_ptr + 1;
266 for (end_ptr= string_ptr; *end_ptr != '\r'; end_ptr++);
267 value= string_ptr;
268 value[(size_t)(end_ptr-string_ptr)]= 0;
269 string_ptr= end_ptr + 2;
270 set_data(stat, key, value);
271 }
272 }
273
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 rc= memcached_connect(ptr);
284 if (rc != MEMCACHED_SUCCESS)
285 {
286 *error= rc;
287 return NULL;
288 }
289
290 stats= (memcached_stat_st *)malloc(sizeof(memcached_st)*(ptr->number_of_hosts+1));
291 if (!stats)
292 {
293 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
294 free(stats);
295 return NULL;
296 }
297 memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts+1));
298
299 for (x= 0; x < ptr->number_of_hosts; x++)
300 {
301 rc= memcached_stats_fetch(ptr, stats+x, args, x);
302 if (rc != MEMCACHED_SUCCESS)
303 rc= MEMCACHED_SOME_ERRORS;
304 }
305
306 *error= rc;
307 return stats;
308 }
309
310 memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args,
311 char *hostname, unsigned int port)
312 {
313 memcached_return rc;
314 memcached_st memc;
315
316 memcached_init(&memc);
317
318 memcached_server_add(&memc, hostname, port);
319
320 rc= memcached_connect(&memc);
321
322 if (rc != MEMCACHED_SUCCESS)
323 return rc;
324
325 rc= memcached_stats_fetch(&memc, stat, args, 0);
326
327 memcached_deinit(&memc);
328
329 return rc;
330 }
331
332 /*
333 We make a copy of the keys since at some point in the not so distant future
334 we will add support for "found" keys.
335 */
336 char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat,
337 memcached_return *error)
338 {
339 char **list= (char **)malloc(sizeof(memcached_stat_keys));
340
341 memset(list, 0, sizeof(memcached_stat_keys));
342
343 if (!list)
344 {
345 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
346 return NULL;
347 }
348
349 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
350
351 *error= MEMCACHED_SUCCESS;
352
353 return list;
354 }